Skip to content

Commit

Permalink
Backward trade rebalance (#9)
Browse files Browse the repository at this point in the history
- Reimplemented Trade-Rebalance support in a backward-compatible way.
  • Loading branch information
Estecka authored Jan 29, 2024
1 parent 77612c7 commit 2d358b7
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 27 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 1 addition & 19 deletions src/main/java/tk/estecka/shiftingwares/ShiftingWares.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<TradeOffers.Factory[]> 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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Factory[]> GetTradeLayout(VillagerEntity villager){
List<Factory[]> layout = new ArrayList<>();
VillagerProfession job = villager.getVillagerData().getProfession();
int jobLevel = villager.getVillagerData().getLevel();

Int2ObjectMap<Factory[]> 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);
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/tk/estecka/shiftingwares/TradeShuffler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<TradeOffers.Factory[]> 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(){
Expand Down
5 changes: 2 additions & 3 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit 2d358b7

Please sign in to comment.