Skip to content

Commit dd24d15

Browse files
authored
Add files via upload
trader allowable dimension filter trader invincibility option allow more than 1 animal to spawn (defaults to 1)
1 parent d0838c9 commit dd24d15

File tree

7 files changed

+278
-75
lines changed

7 files changed

+278
-75
lines changed

examples/hell_armorer.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var TraderTweaker = libcd.require("Tradesmen.TraderTweaker");
2+
3+
TraderTweaker.addTrader("tradesmen:master_armorer", TraderTweaker.makeTrader().tiered()
4+
.name("Hell Armorer")
5+
.animal("minecraft:wolf", 5)
6+
.texture("minecraft:textures/entity/villager/villager.png")
7+
.clothes("minecraft:textures/entity/villager/profession/armorer.png")
8+
.clothes(0x960000)
9+
.hat("tradesmen:textures/entity/hats/eyepatch.png")
10+
.addWorld("minecraft:the_nether")
11+
.godMode()
12+
.setTrades([
13+
[TraderTweaker.makeTrade().item("minecraft:emerald").price("minecraft:coal@15").maxUses(16).experience(2),
14+
TraderTweaker.makeTrade().item("minecraft:iron_boots").price(4).maxUses(12).priceMultiplier(0.2),
15+
TraderTweaker.makeTrade().item("minecraft:iron_leggings").price(7).maxUses(12).priceMultiplier(0.2),
16+
TraderTweaker.makeTrade().item("minecraft:iron_helmet").price(5).maxUses(12).priceMultiplier(0.2),
17+
TraderTweaker.makeTrade().item("minecraft:iron_chestplate").price(9).maxUses(12).priceMultiplier(0.2)],
18+
19+
[TraderTweaker.makeTrade().item("minecraft:emerald").price("minecraft:iron_ingot@4").maxUses(12).experience(10),
20+
TraderTweaker.makeTrade().item("minecraft:bell").price(36).maxUses(12).priceMultiplier(0.2).experience(5),
21+
TraderTweaker.makeTrade().item("minecraft:chainmail_boots").price(1).maxUses(12).priceMultiplier(0.2).experience(5),
22+
TraderTweaker.makeTrade().item("minecraft:chainmail_leggings").price(3).maxUses(12).priceMultiplier(0.2).experience(5)],
23+
24+
[TraderTweaker.makeTrade().item("minecraft:emerald").price("minecraft:lava_bucket").maxUses(12).experience(20),
25+
TraderTweaker.makeTrade().item("minecraft:emerald").price("minecraft:diamond").maxUses(12).experience(20),
26+
TraderTweaker.makeTrade().item("minecraft:chainmail_helmet").price(1).maxUses(12).priceMultiplier(0.2).experience(10),
27+
TraderTweaker.makeTrade().item("minecraft:chainmail_chestplate").price(4).maxUses(12).priceMultiplier(0.2).experience(10),
28+
TraderTweaker.makeTrade().item("minecraft:shield").price(5).maxUses(12).priceMultiplier(0.2).experience(10)],
29+
30+
[TraderTweaker.makeTrade().randomEnchantmentItemTrade().item("minecraft:diamond_leggings").experience(15).randomPrice(14,64).maxUses(3).priceMultiplier(0.2),
31+
TraderTweaker.makeTrade().randomEnchantmentItemTrade().item("minecraft:diamond_boots").experience(15).randomPrice(8,64).maxUses(3).priceMultiplier(0.2)],
32+
33+
[TraderTweaker.makeTrade().randomEnchantmentItemTrade().item("minecraft:diamond_helmet").experience(30).randomPrice(8,64).maxUses(3).priceMultiplier(0.2),
34+
TraderTweaker.makeTrade().randomEnchantmentItemTrade().item("minecraft:diamond_chestplate").experience(30).randomPrice(16,64).maxUses(3).priceMultiplier(0.2)]
35+
],
36+
[2,2,2,2,2]));
37+

src/main/java/mod/linguardium/tradesmen/api/Trader.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ private static Int2ObjectMap<TradeOffers.Factory[]> copyToFastUtilMap(ImmutableM
2727
public Text name;
2828
public String textureId;
2929
public String animal;
30+
public int animalCount;
3031
public String hatTextureId;
3132
public float[] hatColor;
3233
public String clothesTextureId;
3334
public float[] clothesColor;
3435
public List<Integer> tierTradeCount=new ArrayList<>();
3536
public List<List<TradeOffers.Factory>> TRADES;
3637
public Boolean isTiered = false;
37-
38+
public List<String> allowedWorlds = new ArrayList<>();
39+
public boolean godMode;
3840
/*public Trader(String name, String TextureId, String clothesTextureId, Vector3f clothesColor, String hatTextureId, Vector3f hatColor, String animal, List<List<TradeOffers.Factory>> trades) {
3941
this(name,clothesTextureId,clothesTextureId,clothesColor,hatTextureId,hatColor,animal,trades, Lists.newArrayList(3,1),false);
4042
}
@@ -53,6 +55,8 @@ public Trader(String name, String TextureId, String clothesTextureId, float[] cl
5355
this.tierTradeCount = tradeCount;
5456
this.isTiered=tiered;
5557
this.TRADES=trades;
58+
this.godMode=false;
59+
this.animalCount=1;
5660
}
5761
public Trader() {
5862
this.name=new LiteralText("Tradesman");;
@@ -62,6 +66,9 @@ public Trader() {
6266
this.hatTextureId="";
6367
this.hatColor=WHITE_COLOR;
6468
this.animal="minecraft:trader_llama";
69+
this.allowedWorlds=new ArrayList<>();
70+
this.godMode=false;
71+
this.animalCount=1;
6572
}
6673
public Trader name(String trader_name) {
6774
this.name=new TranslatableText(trader_name);
@@ -97,13 +104,29 @@ public Trader animal(String animalId) {
97104
this.animal=animalId;
98105
return this;
99106
}
107+
public Trader animal(String animalId, Integer count) {
108+
this.animal=animalId;
109+
this.animalCount=count;
110+
return this;
111+
}
100112
public Trader tiered() {
101113
return tiered(true);
102114
}
103115
public Trader tiered(Boolean isTiered) {
104116
this.isTiered=isTiered;
105117
return this;
106118
}
119+
public Trader addWorld(String id) {
120+
this.allowedWorlds.add(id);
121+
return this;
122+
}
123+
public Trader godMode() {
124+
return godMode(true);
125+
}
126+
public Trader godMode(boolean b) {
127+
this.godMode=b;
128+
return this;
129+
}
107130
public Trader setTrades(List<List<tradeObject>> trades, List<Integer> tradeCounts) {
108131
List<List<TradeOffers.Factory>> tradeFactories = new ArrayList<List<TradeOffers.Factory>>();
109132
for (int i=0;i<trades.size();i++){

src/main/java/mod/linguardium/tradesmen/api/TraderTweaker.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import io.github.cottonmc.libcd.api.CDSyntaxError;
66
import io.github.cottonmc.libcd.api.tweaker.Tweaker;
77
import jdk.internal.jline.internal.Nullable;
8+
import mod.linguardium.tradesmen.Tradesmen;
89
import mod.linguardium.tradesmen.api.objects.tradeObject;
910
import net.minecraft.client.util.math.Vector3f;
1011
import net.minecraft.resource.ResourceManager;
1112
import net.minecraft.resource.ResourceNotFoundException;
1213
import net.minecraft.village.TradeOffers;
14+
import org.apache.logging.log4j.Level;
1315

1416
import java.util.ArrayList;
1517
import java.util.Collections;
@@ -35,7 +37,11 @@ public void applyReload(ResourceManager resourceManager, Executor executor) {
3537

3638
@Override
3739
public String getApplyMessage() {
38-
return String.valueOf(TradesmenManager.Traders.size()-1) + " traders";
40+
if (TradesmenManager.Traders.size() < 1) {
41+
Tradesmen.log(Level.ERROR,"Failed to load default trader. This will cause problems.");
42+
}
43+
return String.valueOf(TradesmenManager.Traders.size() - 1) + " traders";
44+
3945
}
4046

4147
@Override
@@ -44,6 +50,9 @@ public JsonObject getDebugInfo() {
4450
}
4551

4652
public void addTrader(String id, Trader t) {
53+
if (t.allowedWorlds.size()==0) {
54+
t.allowedWorlds.add("minecraft:overworld");
55+
}
4756
TradesmenManager.Traders.put(id,t);
4857
}
4958
public Trader makeTrader() {

src/main/java/mod/linguardium/tradesmen/api/TradesmenManager.java

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import net.minecraft.text.Text;
1616
import net.minecraft.util.math.BlockPos;
1717
import net.minecraft.util.math.MathHelper;
18+
import net.minecraft.util.registry.Registry;
1819
import net.minecraft.world.GameRules;
1920
import net.minecraft.world.Heightmap;
2021
import net.minecraft.world.SpawnHelper;
@@ -25,10 +26,7 @@
2526
import net.minecraft.world.poi.PointOfInterestType;
2627
import org.apache.logging.log4j.Level;
2728

28-
import java.util.HashMap;
29-
import java.util.Iterator;
30-
import java.util.Optional;
31-
import java.util.Random;
29+
import java.util.*;
3230

3331
public class TradesmenManager implements WorldTickCallback {
3432
public static final TradesmenManager INSTANCE = new TradesmenManager();
@@ -50,9 +48,8 @@ private class WorldTradesmenManager {
5048
public WorldTradesmenManager(ServerWorld world) {
5149
this.world = world;
5250
this.tickCount = 1200;
53-
//LevelProperties levelProperties = world.getLevelProperties();
54-
this.spawnDelay = Tradesmen.getConfig().spawnDelay;//levelProperties.getWanderingTraderSpawnDelay();
55-
this.spawnChance = Tradesmen.getConfig().spawnChance;//levelProperties.getWanderingTraderSpawnChance();
51+
this.spawnDelay = Tradesmen.getConfig().spawnDelay;
52+
this.spawnChance = Tradesmen.getConfig().spawnChance;
5653
if (this.spawnDelay == 0 && this.spawnChance == 0) {
5754
this.spawnDelay = Tradesmen.getConfig().spawnDelay;
5855
this.spawnChance = Tradesmen.getConfig().spawnChance;
@@ -69,7 +66,7 @@ public void tick() {
6966
this.spawnDelay = Tradesmen.getConfig().spawnDelay;
7067
if (this.world.getGameRules().getBoolean(GameRules.DO_MOB_SPAWNING)) {
7168
int i = this.spawnChance;
72-
this.spawnChance = MathHelper.clamp(this.spawnChance + 25, 25, 75);
69+
this.spawnChance = MathHelper.clamp(this.spawnChance, 0, 100);
7370
if (this.random.nextInt(100) <= i) {
7471
if (this.spawnRoamingTrader()) {
7572
this.spawnChance = Tradesmen.getConfig().spawnChance;
@@ -87,32 +84,44 @@ private boolean spawnRoamingTrader() {
8784
if (playerEntity == null) {
8885
return true;
8986
} else {
87+
List<String> allowedTraderTypes = new ArrayList<>();
88+
Traders.forEach((id,trader)->{
89+
if ( !id.equals("default:default_trader") &&
90+
(trader.allowedWorlds.contains("*") ||
91+
trader.allowedWorlds.contains(Registry.DIMENSION_TYPE.getId(world.dimension.getType()).toString()))) {
92+
allowedTraderTypes.add(id);
93+
}
94+
95+
});
96+
if (allowedTraderTypes.size() < 1) {
97+
return true;
98+
}
9099
BlockPos blockPos = playerEntity.getBlockPos();
91-
//int i = true;
100+
101+
// spawn a distance from the player or from a meeting POI type
102+
// currently only village bells
92103
PointOfInterestStorage pointOfInterestStorage = this.world.getPointOfInterestStorage();
93104
Optional<BlockPos> optional = pointOfInterestStorage.getPosition(PointOfInterestType.MEETING.getCompletionCondition(), (blockPosx) -> {
94105
return true;
95106
}, blockPos, 48, PointOfInterestStorage.OccupationStatus.ANY);
96107
BlockPos blockPos2 = (BlockPos)optional.orElse(blockPos);
97-
BlockPos blockPos3 = this.getPosDistanceFrom(blockPos2, 48);
98-
if (blockPos3 != null && this.method_23279(blockPos3)) {
108+
BlockPos blockPos3 = this.getPosDistanceFrom(blockPos2, 25);
109+
if (blockPos3 != null) {
99110
if (this.world.getBiome(blockPos3) == Biomes.THE_VOID) {
100111
return false;
101112
}
102113

103114
TradesmenEntity traderEntity = (TradesmenEntity) InitEntities.TRADESMEN_ENTITY_TYPE.spawn(this.world, (CompoundTag)null, (Text)null, (PlayerEntity)null, blockPos3, SpawnType.EVENT, false, false);
104115
if (traderEntity != null) {
105-
if (Traders.size() > 1) {
106-
traderEntity.setTraderType(
107-
Traders.keySet().stream().filter(id -> !("default:default_trader".equalsIgnoreCase(id))).toArray()[world.random.nextInt(Traders.size()-1)].toString()
108-
);
116+
traderEntity.setTraderType(allowedTraderTypes.get(world.random.nextInt(allowedTraderTypes.size())));
117+
for (int i=0;i<getTraderById(traderEntity.getTraderType()).animalCount; i++) {
118+
this.SpawnAnimal(traderEntity.getTraderAnimal(),traderEntity, 4);
109119
}
110-
this.SpawnAnimal(traderEntity.getTraderAnimal(),traderEntity, 4);
111120

112-
//this.world.getLevelProperties().setWanderingTraderId(wanderingTraderEntity.getUuid());
113121
traderEntity.setDespawnDelay((int)(Tradesmen.getConfig().spawnDelay*0.75F));
114122
traderEntity.setWanderTarget(blockPos2);
115123
traderEntity.setPositionTarget(blockPos2, 16);
124+
traderEntity.setInvulnerable(getTraderById(traderEntity.getTraderType()).godMode);
116125
return true;
117126
}
118127
}
@@ -121,32 +130,47 @@ private boolean spawnRoamingTrader() {
121130
}
122131
}
123132
private void SpawnAnimal(String AnimalId, TradesmenEntity ownerTrader, int distance) {
133+
if (AnimalId.isEmpty())
134+
return;
124135
BlockPos blockPos = this.getPosDistanceFrom(new BlockPos(ownerTrader), distance);
125136
if (blockPos != null) {
126137
MobEntity traderAnimalEntity = (MobEntity)(EntityType.get(AnimalId).orElse(EntityType.TRADER_LLAMA)).spawn(this.world, (CompoundTag)null, (Text)null, (PlayerEntity)null, blockPos, SpawnType.EVENT, false, false);
127138
if (traderAnimalEntity != null) {
128139
traderAnimalEntity.attachLeash(ownerTrader, true);
129-
ownerTrader.setAnimalUUID(traderAnimalEntity.getUuidAsString());
140+
ownerTrader.addAnimalUUID(traderAnimalEntity.getUuidAsString());
130141
}
131142
}
132143
}
133144
@Nullable
145+
private BlockPos getSpawnableHeight(BlockPos pos, int vDistance) {
146+
for (int y = 0; y < vDistance; y++) {
147+
if (SpawnHelper.canSpawn(SpawnRestriction.Location.ON_GROUND, this.world, pos.add(0,y,0), InitEntities.TRADESMEN_ENTITY_TYPE)) {
148+
return pos.add(0,y,0);
149+
}
150+
}
151+
for (int y = -1; y > -vDistance; y--) {
152+
if (SpawnHelper.canSpawn(SpawnRestriction.Location.ON_GROUND, this.world, pos.add(0,y,0), InitEntities.TRADESMEN_ENTITY_TYPE)) {
153+
return pos.add(0,y,0);
154+
}
155+
}
156+
return null;
157+
}
158+
@Nullable
134159
private BlockPos getPosDistanceFrom(BlockPos blockPos, int distance) {
135160
BlockPos blockPos2 = null;
136161

137162
for(int j = 0; j < 10; ++j) {
138163
int k = blockPos.getX() + this.random.nextInt(distance * 2) - distance;
139164
int l = blockPos.getZ() + this.random.nextInt(distance * 2) - distance;
140-
int m = this.world.getTopY(Heightmap.Type.WORLD_SURFACE, k, l);
141-
BlockPos blockPos3 = new BlockPos(k, m, l);
142-
if (SpawnHelper.canSpawn(SpawnRestriction.Location.ON_GROUND, this.world, blockPos3, InitEntities.TRADESMEN_ENTITY_TYPE)) {
143-
blockPos2 = blockPos3;
165+
blockPos2 = getSpawnableHeight(new BlockPos(k,blockPos.getY(),l),distance);
166+
if (blockPos2 != null) {
144167
break;
145168
}
146169
}
147170

148171
return blockPos2;
149172
}
173+
150174
private boolean method_23279(BlockPos blockPos) {
151175
Iterator var2 = BlockPos.iterate(blockPos, blockPos.add(1, 2, 1)).iterator();
152176

@@ -170,12 +194,10 @@ public void tick(World world) {
170194
return;
171195
}
172196
ServerWorld sWorld = (ServerWorld)world;
173-
if (sWorld.dimension.getType() == DimensionType.OVERWORLD) {
174-
if (!WorldManagers.containsKey(sWorld)) {
175-
WorldManagers.put(world, new WorldTradesmenManager(sWorld));
176-
}
177-
WorldManagers.get(sWorld).tick();
197+
if (!WorldManagers.containsKey(sWorld)) {
198+
WorldManagers.put(world, new WorldTradesmenManager(sWorld));
178199
}
200+
WorldManagers.get(sWorld).tick();
179201
}
180202

181203
}

0 commit comments

Comments
 (0)