Skip to content

Commit

Permalink
refactor trades and prepare more 1.21 stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
LLytho committed Jul 26, 2024
1 parent 3aa8bf5 commit 28d8882
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 123 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Changelog

## [0.11.0] - TBA
## [Unreleased]

- Update to 1.21

Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ plugins {
}

repositories {
mavenLocal()
maven("https://maven.saps.dev/minecraft")
maven("https://www.cursemaven.com")
}
Expand Down
52 changes: 50 additions & 2 deletions example_scripts/trading.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,55 @@ MoreJS.updateOffer((event) => {
});

MoreJS.updateOffer((event) => {
if (event.isProfession("minecraft:cartographer")) {
if (event.isProfession("minecraft:cartographer") && event.random.nextDouble() < 0.7) {
const randomBiome = Registry.of("worldgen/biome").getValues("#minecraft:is_overworld").getRandom();
const trade = VillagerUtils.createBiomeMapTrade("minecraft:emerald_block", randomBiome).displayName(
"Random biome"
);
event.setOffer(trade);
}
});

MoreJS.updateOffer((event) => {
if (!event.isProfession("farmer")) {
return;
}
})

const tag = "#minecraft:is_savanna";
const registry = Registry.of("worldgen/biome");
const holders = registry.getValues(tag);
const biome = registry.getRandom();
const biomeId = registry.getId(biome);
const biome2 = holders.getRandom();
const biome2Id = registry.getId(biome2);
console.log("===============");
console.log(`All overworld biomes: ${holders.keys}`);
console.log(`Biome: ${biomeId} contains in ${tag}: ${holders.containsValue(biome)} / ${holders.contains(biomeId)}`);
console.log(
`Biome 2: ${biome2Id} contains in ${tag}: ${holders.containsValue(biome2)} / ${holders.contains(biome2Id)}`
);
console.log(`Biomes do exist: ${registry.containsValue(biome)} / ${registry.containsValue(biome2)}`);
console.log(registry.getValues(tag).size());
});

MoreJS.updateOffer((event) => {
if (!event.isProfession("farmer")) {
return;
}

const ItemAttributeModifiers = Java.loadClass("net.minecraft.world.item.component.ItemAttributeModifiers");

const attributes = ItemAttributeModifiers.builder()
.add(
"minecraft:generic.attack_damage",
{
id: "minecraft:base_attack_damage",
operation: "add_value",
amount: 4.0,
},
"mainhand"
)
.build();
const item = Item.of("minecraft:stick").set("minecraft:attribute_modifiers", attributes);
event.offer.output = item;
});
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ modDescription = More KubeJS stuff.
# Common
minecraftVersion = 1.21
neoforgeVersion = 21.0.98-beta
kubejsVersion = 2100.7.0-build.96
kubejsVersion = 2100.7.0-build.109

# Github
githubUser = AlmostReliable
Expand Down
33 changes: 24 additions & 9 deletions src/main/java/com/almostreliable/morejs/MoreJSBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import com.almostreliable.morejs.features.villager.IntRange;
import com.almostreliable.morejs.features.villager.TradeItem;
import com.almostreliable.morejs.util.LevelUtils;
import com.almostreliable.morejs.util.ResourceOrTag;
import com.almostreliable.morejs.util.Utils;
import com.almostreliable.morejs.util.WeightedList;
import com.mojang.datafixers.util.Pair;
import dev.latvian.mods.kubejs.item.ItemStackJS;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import dev.latvian.mods.kubejs.util.UtilsJS;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.Registries;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderSet;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.levelgen.structure.Structure;
Expand All @@ -20,15 +20,30 @@

public class MoreJSBinding {
@Nullable
public static BlockPos findStructure(BlockPos position, ServerLevel level, String structure, int chunkRadius) {
ResourceOrTag<Structure> rot = ResourceOrTag.get(structure, Registries.STRUCTURE);
return LevelUtils.findStructure(position, level, rot, chunkRadius);
public static BlockPos findStructure(BlockPos position, ServerLevel level, HolderSet<Structure> structures, int chunkRadius) {
var result = level
.getChunkSource()
.getGenerator()
.findNearestMapStructure(level, structures, position, chunkRadius, true);
if (result == null) {
return null;
}

return result.getFirst();
}

@Nullable
public static BlockPos findBiome(BlockPos position, ServerLevel level, String biome, int chunkRadius) {
ResourceOrTag<Biome> rot = ResourceOrTag.get(biome, Registries.BIOME);
return LevelUtils.findBiome(position, level, rot, chunkRadius);
public static BlockPos findBiome(BlockPos position, ServerLevel level, HolderSet<Biome> biomes, int chunkRadius) {
Pair<BlockPos, Holder<Biome>> nearestBiome = level.findClosestBiome3d(biomes::contains,
position,
chunkRadius * 16,
32,
64);
if (nearestBiome != null) {
return nearestBiome.getFirst();
}

return null;
}

public static WeightedList.Builder<Object> weightedList() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.almostreliable.morejs.features.villager;

import com.almostreliable.morejs.MoreJSBinding;
import com.almostreliable.morejs.features.villager.trades.*;
import com.almostreliable.morejs.util.WeightedList;
import com.almostreliable.morejs.util.BlockPosFinder;
import com.google.common.collect.ImmutableList;
import net.minecraft.core.HolderSet;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -11,6 +13,8 @@
import net.minecraft.world.entity.npc.VillagerProfession;
import net.minecraft.world.entity.npc.VillagerTrades;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.levelgen.structure.Structure;

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
Expand Down Expand Up @@ -66,15 +70,17 @@ public static CustomTrade createCustomTrade(TransformableTrade.Transformer trans
return new CustomTrade(transformer);
}

public static TreasureMapTrade createStructureMapTrade(TradeItem[] inputs, WeightedList<Object> structures) {
return TreasureMapTrade.forStructure(inputs, structures);
public static TreasureMapTrade createStructureMapTrade(TradeItem[] inputs, HolderSet<Structure> structures) {
return new TreasureMapTrade(inputs,
(level, entity) -> MoreJSBinding.findStructure(entity.blockPosition(), level, structures, 100));
}

public static TreasureMapTrade createBiomeMapTrade(TradeItem[] inputs, WeightedList<Object> biomes) {
return TreasureMapTrade.forBiome(inputs, biomes);
public static TreasureMapTrade createBiomeMapTrade(TradeItem[] inputs, HolderSet<Biome> biomes) {
return new TreasureMapTrade(inputs,
(level, entity) -> MoreJSBinding.findBiome(entity.blockPosition(), level, biomes, 250));
}

public static TreasureMapTrade createCustomMapTrade(TradeItem[] inputs, MapPosInfo.Provider func) {
public static TreasureMapTrade createCustomMapTrade(TradeItem[] inputs, BlockPosFinder func) {
return new TreasureMapTrade(inputs, func);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.almostreliable.morejs.features.villager.trades;

import com.almostreliable.morejs.features.villager.TradeItem;
import com.almostreliable.morejs.util.LevelUtils;
import com.almostreliable.morejs.util.ResourceOrTag;
import com.almostreliable.morejs.util.WeightedList;
import net.minecraft.core.BlockPos;
import com.almostreliable.morejs.util.BlockPosFinder;
import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
Expand All @@ -22,50 +18,16 @@
import javax.annotation.Nullable;

public class TreasureMapTrade extends TransformableTrade<TreasureMapTrade> {
protected final MapPosInfo.Provider destinationPositionFunc;
protected final BlockPosFinder blockPosFinder;
@Nullable protected Component displayName;
protected Holder<MapDecorationType> destinationType = MapDecorationTypes.RED_X;
private boolean renderBiomePreviewMap = true;

private byte mapViewScale = 2;

public TreasureMapTrade(TradeItem[] inputs, MapPosInfo.Provider destinationPositionFunc) {
public TreasureMapTrade(TradeItem[] inputs, BlockPosFinder blockPosFinder) {
super(inputs);
this.destinationPositionFunc = destinationPositionFunc;
}

public static TreasureMapTrade forStructure(TradeItem[] input, WeightedList<Object> entries) {
var list = entries.map(o -> {
if (o == null) {
return null;
}
return ResourceOrTag.get(o.toString(), Registries.STRUCTURE);
});

MapPosInfo.Provider func = (level, entity) -> {
var roll = list.roll(level.random);
BlockPos pos = LevelUtils.findStructure(entity.blockPosition(), level, roll, 100);
if (pos == null) return null;
return new MapPosInfo(pos, roll.getName());
};
return new TreasureMapTrade(input, func);
}

public static TreasureMapTrade forBiome(TradeItem[] input, WeightedList<Object> entries) {
var list = entries.map(o -> {
if (o == null) {
return null;
}
return ResourceOrTag.get(o.toString(), Registries.BIOME);
});

MapPosInfo.Provider func = (level, entity) -> {
var roll = list.roll(level.random);
BlockPos pos = LevelUtils.findBiome(entity.blockPosition(), level, roll, 250);
if (pos == null) return null;
return new MapPosInfo(pos, roll.getName());
};
return new TreasureMapTrade(input, func);
this.blockPosFinder = blockPosFinder;
}

public TreasureMapTrade displayName(Component name) {
Expand All @@ -92,13 +54,16 @@ public TreasureMapTrade scale(byte scale) {
@Nullable
public MerchantOffer createOffer(Entity trader, RandomSource random) {
if (trader.level() instanceof ServerLevel level) {
MapPosInfo info = destinationPositionFunc.apply(level, trader);
if (info == null) return null;
var pos = blockPosFinder.findPosition(level, trader);
if (pos == null) return null;

ItemStack map = MapItem.create(level, info.pos().getX(), info.pos().getZ(), this.mapViewScale, true, true);
ItemStack map = MapItem.create(level, pos.getX(), pos.getZ(), this.mapViewScale, true, true);
if (renderBiomePreviewMap) MapItem.renderBiomePreviewMap(level, map);
MapItemSavedData.addTargetDecoration(map, info.pos(), "+", destinationType);
map.set(DataComponents.CUSTOM_NAME, displayName == null ? info.name() : displayName);
MapItemSavedData.addTargetDecoration(map, pos, "+", destinationType);
if (displayName != null) {
map.set(DataComponents.CUSTOM_NAME, displayName);
}

return createOffer(map, random);
}

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/almostreliable/morejs/util/BlockPosFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.almostreliable.morejs.util;

import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;

import javax.annotation.Nullable;

@FunctionalInterface
public interface BlockPosFinder {

@Nullable
BlockPos findPosition(ServerLevel level, Entity entity);
}
41 changes: 0 additions & 41 deletions src/main/java/com/almostreliable/morejs/util/LevelUtils.java

This file was deleted.

0 comments on commit 28d8882

Please sign in to comment.