From 2d358b7f69754cdf9eab03afdd82bb799f415a75 Mon Sep 17 00:00:00 2001 From: Estecka Date: Mon, 29 Jan 2024 12:52:47 +0100 Subject: [PATCH] Backward trade rebalance (#9) - Reimplemented Trade-Rebalance support in a backward-compatible way. --- changelog.md | 3 +++ gradle.properties | 8 ++++---- .../estecka/shiftingwares/ShiftingWares.java | 20 +------------------ .../TradeLayouts/VanillaTradeLayout.java | 17 ++++++++++++++++ .../estecka/shiftingwares/TradeShuffler.java | 20 ++++++++++++++++++- src/main/resources/fabric.mod.json | 5 ++--- 6 files changed, 46 insertions(+), 27 deletions(-) diff --git a/changelog.md b/changelog.md index cb1e100..d09455c 100644 --- a/changelog.md +++ b/changelog.md @@ -19,5 +19,8 @@ Initial release - Added a gamerules that allows cartographers to regenerate maps that have been sold at least once. # v2 +## 2.0.0 - Made some methods and types available through an API. - Added an entrypoint to define villager trades from outside mods +## 2.1.0 +- Reimplemented Trade-Rebalance support in a backward-compatible way diff --git a/gradle.properties b/gradle.properties index f66c702..00306f4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,14 +4,14 @@ org.gradle.parallel=true # Fabric Properties # check these on https://fabricmc.net/develop -minecraft_version=1.20.1 -yarn_mappings=1.20.1+build.10 +minecraft_version=1.20.2 +yarn_mappings=1.20.2+build.4 loader_version=0.15.3 #Fabric api -fabric_version=0.91.0+1.20.1 +fabric_version=0.91.2+1.20.2 # Mod Properties -mod_version=2.0.0 +mod_version=2.1.0 maven_group=tk.estecka.shiftingwares archives_base_name=shifting-wares diff --git a/src/main/java/tk/estecka/shiftingwares/ShiftingWares.java b/src/main/java/tk/estecka/shiftingwares/ShiftingWares.java index 12f1631..0591ca7 100644 --- a/src/main/java/tk/estecka/shiftingwares/ShiftingWares.java +++ b/src/main/java/tk/estecka/shiftingwares/ShiftingWares.java @@ -3,16 +3,11 @@ import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.gamerule.v1.GameRuleFactory; import net.fabricmc.fabric.api.gamerule.v1.GameRuleRegistry; -import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.entity.passive.VillagerEntity; import net.minecraft.item.ItemStack; import net.minecraft.village.TradeOffer; -import net.minecraft.village.TradeOffers; import net.minecraft.world.GameRules; import net.minecraft.world.GameRules.BooleanRule; -import tk.estecka.shiftingwares.api.ITradeLayoutProvider; import tk.estecka.shiftingwares.TradeLayouts.VanillaTradeLayout; -import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,22 +22,9 @@ public class ShiftingWares static public final TradeOffer PLACEHOLDER_TRADE = new TradeOffer(ItemStack.EMPTY, ItemStack.EMPTY, ItemStack.EMPTY, 0, 0, 0, 0, 0); - static public final ITradeLayoutProvider VANILLA_LAYOUT = new VanillaTradeLayout(); - - static public List GetTradeLayout(VillagerEntity villager){ - var providers = FabricLoader.getInstance().getEntrypoints("shifting-wares", ITradeLayoutProvider.class); - - for (var p : providers) { - var layout = p.GetTradeLayout(villager); - if (layout != null) - return layout; - } - - return VANILLA_LAYOUT.GetTradeLayout(villager); - } - @Override public void onInitialize() { // Static initialization + LOGGER.info("Trade-Rebalance support is {} for this version of minecraft.", VanillaTradeLayout.IS_EXP_TRADE_AVAILABLE?"enabled":"disabled"); } } diff --git a/src/main/java/tk/estecka/shiftingwares/TradeLayouts/VanillaTradeLayout.java b/src/main/java/tk/estecka/shiftingwares/TradeLayouts/VanillaTradeLayout.java index 6b9b96c..22f68ef 100644 --- a/src/main/java/tk/estecka/shiftingwares/TradeLayouts/VanillaTradeLayout.java +++ b/src/main/java/tk/estecka/shiftingwares/TradeLayouts/VanillaTradeLayout.java @@ -4,6 +4,8 @@ import java.util.List; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import net.minecraft.entity.passive.VillagerEntity; +import net.minecraft.resource.featuretoggle.FeatureFlags; +import net.minecraft.util.Identifier; import net.minecraft.village.TradeOffers; import net.minecraft.village.VillagerData; import net.minecraft.village.VillagerProfession; @@ -14,12 +16,27 @@ public class VanillaTradeLayout implements ITradeLayoutProvider { + static public final Identifier TRADE_REBALANCE_FLAGID = new Identifier("minecraft", "trade_rebalance"); + static public final boolean IS_EXP_TRADE_AVAILABLE; + + static { + var featureSet = FeatureFlags.FEATURE_MANAGER.getFeatureSet(); + var featureIds = FeatureFlags.FEATURE_MANAGER.toId(featureSet); + IS_EXP_TRADE_AVAILABLE = featureIds.contains(TRADE_REBALANCE_FLAGID); + } + public List GetTradeLayout(VillagerEntity villager){ List layout = new ArrayList<>(); VillagerProfession job = villager.getVillagerData().getProfession(); int jobLevel = villager.getVillagerData().getLevel(); Int2ObjectMap jobPool = TradeOffers.PROFESSION_TO_LEVELED_TRADE.get(job); + if (IS_EXP_TRADE_AVAILABLE + && villager.getWorld().getEnabledFeatures().contains(FeatureFlags.TRADE_REBALANCE) + && TradeOffers.REBALANCED_PROFESSION_TO_LEVELED_TRADE.containsKey(job) + ) { + jobPool = TradeOffers.REBALANCED_PROFESSION_TO_LEVELED_TRADE.get(job); + } if (jobPool == null){ ShiftingWares.LOGGER.error("No trade pool for job {}.", job); diff --git a/src/main/java/tk/estecka/shiftingwares/TradeShuffler.java b/src/main/java/tk/estecka/shiftingwares/TradeShuffler.java index fc25638..241f44a 100644 --- a/src/main/java/tk/estecka/shiftingwares/TradeShuffler.java +++ b/src/main/java/tk/estecka/shiftingwares/TradeShuffler.java @@ -3,15 +3,21 @@ import java.util.ArrayList; import java.util.IdentityHashMap; import java.util.List; +import net.fabricmc.loader.api.FabricLoader; import net.minecraft.entity.passive.VillagerEntity; import net.minecraft.util.math.random.Random; import net.minecraft.village.TradeOffer; import net.minecraft.village.TradeOfferList; +import net.minecraft.village.TradeOffers; import net.minecraft.village.TradeOffers.Factory; +import tk.estecka.shiftingwares.TradeLayouts.VanillaTradeLayout; +import tk.estecka.shiftingwares.api.ITradeLayoutProvider; import net.minecraft.village.VillagerProfession; public class TradeShuffler { + static public final ITradeLayoutProvider VANILLA_LAYOUT = new VanillaTradeLayout(); + private final VillagerEntity villager; private final boolean depletedOnly; @@ -32,7 +38,19 @@ public TradeShuffler(VillagerEntity villager, boolean depletedOnly) this.random = villager.getRandom(); this.tradeCache = ((IVillagerEntityDuck)villager).shiftingwares$GetItemCache(); - this.tradeLayout = ShiftingWares.GetTradeLayout(villager); + this.tradeLayout = GetTradeLayout(villager); + } + + static public List GetTradeLayout(VillagerEntity villager){ + var providers = FabricLoader.getInstance().getEntrypoints("shifting-wares", ITradeLayoutProvider.class); + + for (var p : providers) { + var layout = p.GetTradeLayout(villager); + if (layout != null) + return layout; + } + + return VANILLA_LAYOUT.GetTradeLayout(villager); } public void Reroll(){ diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index aabe81f..9112155 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -24,11 +24,10 @@ ], "depends": { "fabricloader": ">=0.15.3", - "minecraft": ">=1.19.4", "java": ">=17", "fabric-api": "*" }, - "suggests": { - "minecraft": "<1.20.2" + "recommends": { + "minecraft": "^1.19.4" } }