Skip to content

Commit

Permalink
Merge pull request #1 from Linguardium/dev
Browse files Browse the repository at this point in the history
Add config
add capability to disable vanilla wandering trader
fix item parsing and add support for nbt laden itemstacks
  • Loading branch information
Linguardium authored Apr 9, 2020
2 parents 9ff7622 + 585d332 commit cb4ab9a
Show file tree
Hide file tree
Showing 14 changed files with 303 additions and 79 deletions.
21 changes: 19 additions & 2 deletions carlsjr.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
var TraderTweaker = libcd.require("Tradesmen.TraderTweaker");
var TweakerUtils = libcd.require("libcd.util.TweakerUtils");
var newPickStack = TweakerUtils.createItemStack("minecraft:diamond_pickaxe");
newPickStack = TweakerUtils.setDamage(newPickStack, 1430);
newPickStack = TweakerUtils.setName(newPickStack, "Patched Pickaxe");
newPickStack = TweakerUtils.enchant(newPickStack, "minecraft:fortune", 1);

TraderTweaker.addTrader("tradesmen:beef_trader","entity.tradesmen.carltwo.name", "tradesmen:textures/entity/taco_man.png", "minecraft:cow",[
TraderTweaker.makeTrade("minecraft:rotten_flesh",1,4,4,1)
TraderTweaker.makeTrade("minecraft:rotten_flesh@4",1,4,1)
],
[
TraderTweaker.makeTrade("minecraft:cooked_beef",1,4,4,1),
TraderTweaker.makeTrade("minecraft:cooked_beef@2",[ "minecraft:emerald","minecraft:rotten_flesh@4"],4,1),
]);
TraderTweaker.addTrader("tradesmen:fish_trader","Sad Fishman", "", "minecraft:cod",[
TraderTweaker.makeTrade(newPickStack,1,4,4,1)
],
[
TraderTweaker.makeTrade("minecraft:cooked_cod@1",newPickStack,4,1),
]);
TraderTweaker.addTrader("tradesmen:pig_trader","Wigby", "", "minecraft:pig",[
TraderTweaker.makeTrade("minecraft:porkchop",1,4,4,1)
],
[
TraderTweaker.makeTrade("minecraft:cooked_porkchop",1,4,4,1),
]);
9 changes: 8 additions & 1 deletion src/main/java/mod/linguardium/tradesmen/Tradesmen.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package mod.linguardium.tradesmen;

import io.github.cottonmc.libcd.api.tweaker.TweakerManager;
import me.sargunvohra.mcmods.autoconfig1u.AutoConfig;
import me.sargunvohra.mcmods.autoconfig1u.serializer.JanksonConfigSerializer;
import mod.linguardium.tradesmen.api.TraderTweaker;
import mod.linguardium.tradesmen.config.ModConfig;
import mod.linguardium.tradesmen.entities.InitEntities;
import net.fabricmc.api.ModInitializer;

Expand All @@ -26,10 +29,14 @@ public class Tradesmen implements ModInitializer {
@Override
public void onInitialize() {
log(Level.INFO, "Initializing");
AutoConfig.register(ModConfig.class, JanksonConfigSerializer::new);
InitEntities.init();
TweakerManager.INSTANCE.addTweaker("Tradesmen.TraderTweaker", new TraderTweaker());
log(Level.INFO, "Welcome to The Tradesmen");
}
public static ModConfig getConfig() {
return AutoConfig.getConfigHolder(ModConfig.class).getConfig();
}

public static void log(Level level, String message){
LOGGER.log(level, "["+MOD_NAME+"] " + message);
}
Expand Down
100 changes: 67 additions & 33 deletions src/main/java/mod/linguardium/tradesmen/api/TraderTweaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@

import blue.endless.jankson.JsonObject;
import blue.endless.jankson.JsonPrimitive;
import io.github.cottonmc.libcd.api.CDSyntaxError;
import io.github.cottonmc.libcd.api.tweaker.Tweaker;
import io.github.cottonmc.libcd.api.tweaker.recipe.RecipeParser;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.recipe.Recipe;
import net.minecraft.resource.ResourceManager;
import net.minecraft.resource.ResourceNotFoundException;
import net.minecraft.util.Identifier;
Expand All @@ -26,7 +32,7 @@ public void applyReload(ResourceManager resourceManager, Executor executor) {

@Override
public String getApplyMessage() {
return String.valueOf(TradesmenManager.Traders.size()) + " traders";
return String.valueOf(TradesmenManager.Traders.size()-1) + " traders";
}

@Override
Expand All @@ -39,22 +45,24 @@ public void addTrader(String Id, String name, String texture, String animal, tra
TradesmenTradeOffers.SellItemFactory[] rareTrades = new TradesmenTradeOffers.SellItemFactory[rare.length];
int i=0;
for (tradeObject trade : common) {
Identifier ItemId = Identifier.tryParse(trade.get(String.class, "item"));
if (ItemId == null) {
throw(new ResourceNotFoundException(null,trade.get(String.class, "item")));
TradesmenTradeOffers.SellItemFactory sale=null;
if (!trade.saleItem.isEmpty() && !trade.priceItem[0].isEmpty()) {
sale = new TradesmenTradeOffers.SellItemFactory(trade.saleItem, trade.priceItem, trade.maxUses, trade.experience, trade.multiplier);
}
if (sale != null) {
commonTrades[i] = sale;
}
TradesmenTradeOffers.SellItemFactory sale = new TradesmenTradeOffers.SellItemFactory(Registry.ITEM.get(ItemId), trade.getInt("price", 1), trade.getInt("count", 1), trade.getInt("maxUses", 1), trade.getInt("experience", 1));
commonTrades[i] = sale;
i++;
}
i=0;
for (tradeObject trade : rare) {
Identifier ItemId = Identifier.tryParse(trade.get(String.class, "item"));
if (ItemId == null) {
throw(new ResourceNotFoundException(null,trade.get(String.class, "item")));
TradesmenTradeOffers.SellItemFactory sale=null;
if (!trade.saleItem.isEmpty() && !trade.priceItem[0].isEmpty()) {
sale = new TradesmenTradeOffers.SellItemFactory(trade.saleItem, trade.priceItem, trade.maxUses, trade.experience, trade.multiplier);
}
if (sale!=null) {
rareTrades[i] = sale;
}
TradesmenTradeOffers.SellItemFactory sale = new TradesmenTradeOffers.SellItemFactory(Registry.ITEM.get(ItemId), trade.getInt("price", 1), trade.getInt("count", 1), trade.getInt("maxUses", 1), trade.getInt("experience", 1));
rareTrades[i] = sale;
i++;
}

Expand All @@ -63,44 +71,70 @@ public void addTrader(String Id, String name, String texture, String animal, tra
public tradeObject makeTrade() {
return new tradeObject();
}
public tradeObject makeTrade(String item, int price, int count, int maxUses, int experience) {
tradeObject retObj = new tradeObject();
retObj.put("item",new JsonPrimitive(item));
retObj.put("price",new JsonPrimitive(price));
retObj.put("count",new JsonPrimitive(count));
retObj.put("maxUses",new JsonPrimitive(maxUses));
retObj.put("experience",new JsonPrimitive(experience));
public tradeObject makeTrade(Object item, Object[] price, int maxUses, int experience) throws CDSyntaxError {
tradeObject retObj = new tradeObject()
.item(item)
.price(price)
.maxUses(maxUses)
.experience(experience);
return retObj;
}
public JsonObject makeTrade(String item, int price, int count, int maxUses) {
return makeTrade(item,price,count,maxUses,1);
}
public JsonObject makeTrade(String item, int price, int count) {
return makeTrade(item,price,count,1,1);
public tradeObject makeTrade(Object item, Object price, int maxUses, int experience) throws CDSyntaxError {
tradeObject retObj = new tradeObject()
.item(item)
.price(price)
.maxUses(maxUses)
.experience(experience);
return retObj;
}
public JsonObject makeTrade(String item, int price) {
return makeTrade(item,price,1,1,1);
public tradeObject makeTrade(Object item, Object price, int count, int maxUses, int experience) throws CDSyntaxError {
tradeObject retObj = new tradeObject()
.item(item)
.count(count)
.price(price)
.maxUses(maxUses)
.experience(experience);
return retObj;
}

public class tradeObject extends JsonObject {
public tradeObject item(String item) {
this.put("item",new JsonPrimitive(item));
public class tradeObject {
ItemStack saleItem=ItemStack.EMPTY;
ItemStack[] priceItem= new ItemStack[]{new ItemStack((Item)null,0),new ItemStack((Item)null,0)};
Integer maxUses = 1;
Integer experience=0;
Float multiplier=0.05F;
public tradeObject item(Object item) throws CDSyntaxError {
saleItem = RecipeParser.processItemStack(item).copy();
return this;
}
public tradeObject price(Object[] price) throws CDSyntaxError {
for (int i = 0; i< price.length && i<2 ; i++) {
priceItem[i]=RecipeParser.processItemStack(price[i]).copy();
}
return this;
}
public tradeObject price(Object price) throws CDSyntaxError {
if (price instanceof Integer) {
this.priceItem[0] = new ItemStack(Items.EMERALD, (Integer) price);
}else {
this.priceItem[0] = RecipeParser.processItemStack(price).copy();
}
return this;
}
public tradeObject price(int price) {
this.put("price",new JsonPrimitive(price));
public tradeObject secondPriceStack(Object price) throws CDSyntaxError {
priceItem[1] = RecipeParser.processItemStack(price);
return this;
}
public tradeObject count(int count) {
this.put("count", new JsonPrimitive(count));
this.saleItem.setCount(count);
return this;
}
public tradeObject maxUses(int maxUses) {
this.put("maxUses", new JsonPrimitive(maxUses));
this.maxUses=maxUses;
return this;
}
public tradeObject experience(int experience) {
this.put("experience",new JsonPrimitive(experience));
this.experience=experience;
return this;
}
}
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/mod/linguardium/tradesmen/api/TradesmenManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
Expand All @@ -23,12 +24,12 @@
import net.minecraft.world.poi.PointOfInterestType;
import org.apache.logging.log4j.Level;

import java.io.DataOutput;
import java.io.IOException;
import java.util.*;

public class TradesmenManager implements WorldTickCallback {
private HashMap<World, WorldTradesmenManager> WorldManagers = new HashMap<>();
public static final int SPAWN_DELAY_CONSTANT = 24000; // 24000
public static final int SPAWN_CHANCE_CONSTANT = 25; // 25
public static HashMap<String, Trader> Traders = new HashMap<>();
public TradesmenManager() {
Tradesmen.log(Level.INFO,"Trader Manager Initialized");
Expand All @@ -47,11 +48,11 @@ public WorldTradesmenManager(ServerWorld world) {
this.world = world;
this.tickCount = 1200;
//LevelProperties levelProperties = world.getLevelProperties();
this.spawnDelay = TradesmenManager.SPAWN_DELAY_CONSTANT;//levelProperties.getWanderingTraderSpawnDelay();
this.spawnChance = TradesmenManager.SPAWN_CHANCE_CONSTANT;//levelProperties.getWanderingTraderSpawnChance();
this.spawnDelay = Tradesmen.getConfig().spawnDelay;//levelProperties.getWanderingTraderSpawnDelay();
this.spawnChance = Tradesmen.getConfig().spawnChance;//levelProperties.getWanderingTraderSpawnChance();
if (this.spawnDelay == 0 && this.spawnChance == 0) {
this.spawnDelay = TradesmenManager.SPAWN_DELAY_CONSTANT;
this.spawnChance = TradesmenManager.SPAWN_CHANCE_CONSTANT;
this.spawnDelay = Tradesmen.getConfig().spawnDelay;
this.spawnChance = Tradesmen.getConfig().spawnChance;
}

}
Expand All @@ -62,13 +63,13 @@ public void tick() {
this.tickCount = 1200;
this.spawnDelay -= 1200;
if (this.spawnDelay <= 0) {
this.spawnDelay = TradesmenManager.SPAWN_DELAY_CONSTANT;
this.spawnDelay = Tradesmen.getConfig().spawnDelay;
if (this.world.getGameRules().getBoolean(GameRules.DO_MOB_SPAWNING)) {
int i = this.spawnChance;
this.spawnChance = MathHelper.clamp(this.spawnChance + 25, 25, 75);
if (this.random.nextInt(100) <= i) {
if (this.spawnRoamingTrader()) {
this.spawnChance = TradesmenManager.SPAWN_CHANCE_CONSTANT;
this.spawnChance = Tradesmen.getConfig().spawnChance;
}

}
Expand Down Expand Up @@ -106,7 +107,7 @@ private boolean spawnRoamingTrader() {
this.SpawnAnimal(traderEntity.getTraderAnimal(),traderEntity, 4);

//this.world.getLevelProperties().setWanderingTraderId(wanderingTraderEntity.getUuid());
traderEntity.setDespawnDelay(SPAWN_DELAY_CONSTANT);
traderEntity.setDespawnDelay((int)(Tradesmen.getConfig().spawnDelay*0.75F));
traderEntity.setWanderTarget(blockPos2);
traderEntity.setPositionTarget(blockPos2, 16);
return true;
Expand All @@ -116,12 +117,13 @@ private boolean spawnRoamingTrader() {
return false;
}
}
private void SpawnAnimal(String AnimalId, LivingEntity ownerTrader, int distance) {
private void SpawnAnimal(String AnimalId, TradesmenEntity ownerTrader, int distance) {
BlockPos blockPos = this.getPosDistanceFrom(new BlockPos(ownerTrader), distance);
if (blockPos != null) {
MobEntity traderAnimalEntity = (MobEntity)(EntityType.get(AnimalId).orElse(EntityType.TRADER_LLAMA)).spawn(this.world, (CompoundTag)null, (Text)null, (PlayerEntity)null, blockPos, SpawnType.EVENT, false, false);
if (traderAnimalEntity != null) {
traderAnimalEntity.attachLeash(ownerTrader, true);
ownerTrader.setAnimalUUID(traderAnimalEntity.getUuidAsString());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class TradesmenTradeOffers {

static class SellItemFactory implements TradeOffers.Factory {
private ItemStack sell;
private int price;
private ItemStack[] price;
private int count;
private int maxUses;
private int experience;
Expand All @@ -29,34 +29,42 @@ public SellItemFactory(SellItemFactory from) {
this.multiplier=from.multiplier;
}
public SellItemFactory(Block block, int price, int count, int maxUses, int experience) {
this(new ItemStack(block), price, count, maxUses, experience);
this(new ItemStack(block, count), price, maxUses, experience);
}

public SellItemFactory(Item item, int price, int count, int experience) {
this((ItemStack) (new ItemStack(item)), price, count, 12, experience);
this(new ItemStack(item, count), price, 12, experience);
}

public SellItemFactory(Item item, int price, int count, int maxUses, int experience) {
this(new ItemStack(item), price, count, maxUses, experience);
this(new ItemStack(item, count), price, maxUses, experience);
}

public SellItemFactory(ItemStack itemStack, int price, int count, int maxUses, int experience) {
this(itemStack, price, count, maxUses, experience, 0.05F);
public SellItemFactory(ItemStack itemStack, int price, int maxUses, int experience) {
this(itemStack, price, maxUses, experience, 0.05F);
}

public SellItemFactory(ItemStack itemStack, int price, int count, int maxUses, int experience, float multiplier) {
public SellItemFactory(ItemStack itemStack, int price, int maxUses, int experience, float multiplier) {
this(itemStack,new ItemStack(Items.EMERALD, price), maxUses,experience,multiplier);
}
public SellItemFactory(ItemStack itemStack, ItemStack price, int maxUses, int experience) {
this(itemStack,price, maxUses,experience,0.05F);
}

public SellItemFactory(ItemStack itemStack, ItemStack price, int maxUses, int experience, float multiplier) {
this(itemStack,new ItemStack[]{price,ItemStack.EMPTY},maxUses,experience,multiplier);
}
public SellItemFactory(ItemStack itemStack, ItemStack[] price, int maxUses, int experience, float multiplier) {
this.sell = itemStack;
this.price = price;
this.count = count;
this.maxUses = maxUses;
this.experience = experience;
this.multiplier = multiplier;
}


public TradeOffer create(Entity entity, Random random) {
return new TradeOffer(new ItemStack(Items.EMERALD, this.price),
new ItemStack(this.sell.getItem(), this.count), this.maxUses, this.experience, this.multiplier);
return new TradeOffer(this.price[0],this.price[1],
this.sell, this.maxUses, this.experience, this.multiplier);
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/mod/linguardium/tradesmen/config/ConfigMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package mod.linguardium.tradesmen.config;

import io.github.prospector.modmenu.api.ModMenuApi;
import me.sargunvohra.mcmods.autoconfig1u.AutoConfig;
import mod.linguardium.tradesmen.Tradesmen;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.screen.Screen;

import java.util.Optional;
import java.util.function.Supplier;

@Environment(EnvType.CLIENT)
public class ConfigMenu implements ModMenuApi {
@Override
public String getModId() {
return Tradesmen.MOD_ID;
}

@Override
public Optional<Supplier<Screen>> getConfigScreen(Screen screen) {
return Optional.of(AutoConfig.getConfigScreen(ModConfig.class, screen));
}
}
20 changes: 20 additions & 0 deletions src/main/java/mod/linguardium/tradesmen/config/ModConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package mod.linguardium.tradesmen.config;

import me.sargunvohra.mcmods.autoconfig1u.ConfigData;
import me.sargunvohra.mcmods.autoconfig1u.annotation.Config;
import me.sargunvohra.mcmods.autoconfig1u.annotation.ConfigEntry;

@Config.Gui.Background("minecraft:textures/block/orange_shulker_box.png")
@Config(name = "tradesmen")
public class ModConfig implements ConfigData {

public boolean disableWanderingTrader = false;
public boolean despawnAnimals = true;
@ConfigEntry.Gui.Tooltip
public int spawnDelay = 24000;

@ConfigEntry.BoundedDiscrete(max=100,min=1)
public int spawnChance = 25;


}
Loading

0 comments on commit cb4ab9a

Please sign in to comment.