Skip to content

Commit 908da47

Browse files
committed
Improved Mob Coin Drop System
- Can now use tags - Now refreshes when `/reload` is run, no longer requiring you to restart your game
1 parent 19506ce commit 908da47

3 files changed

Lines changed: 62 additions & 46 deletions

File tree

‎src/main/java/com/glisco/numismaticoverhaul/NumismaticOverhaul.java‎

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
package com.glisco.numismaticoverhaul;
22

3-
import com.glisco.numismaticoverhaul.block.NumismaticOverhaulBlocks;
4-
import com.glisco.numismaticoverhaul.block.PiggyBankScreenHandler;
5-
import com.glisco.numismaticoverhaul.block.ShopScreenHandler;
3+
import com.glisco.numismaticoverhaul.block.*;
64
import com.glisco.numismaticoverhaul.currency.MoneyBagLootEntry;
75
import com.glisco.numismaticoverhaul.item.*;
8-
import com.glisco.numismaticoverhaul.network.RequestPurseActionC2SPacket;
9-
import com.glisco.numismaticoverhaul.network.ShopScreenHandlerRequestC2SPacket;
10-
import com.glisco.numismaticoverhaul.network.UpdateShopScreenS2CPacket;
6+
import com.glisco.numismaticoverhaul.network.*;
117
import com.glisco.numismaticoverhaul.villagers.data.VillagerTradesResourceListener;
128
import com.glisco.numismaticoverhaul.villagers.json.VillagerTradesHandler;
139
import io.wispforest.owo.itemgroup.Icon;
@@ -29,24 +25,26 @@
2925
import net.minecraft.block.Block;
3026
import net.minecraft.component.ComponentType;
3127
import net.minecraft.entity.EntityType;
32-
import net.minecraft.loot.*;
28+
import net.minecraft.loot.LootPool;
29+
import net.minecraft.loot.LootTables;
3330
import net.minecraft.loot.condition.RandomChanceLootCondition;
3431
import net.minecraft.loot.entry.LootPoolEntryType;
3532
import net.minecraft.particle.BlockStateParticleEffect;
3633
import net.minecraft.particle.ParticleTypes;
37-
import net.minecraft.registry.Registries;
38-
import net.minecraft.registry.Registry;
39-
import net.minecraft.registry.RegistryKeys;
34+
import net.minecraft.registry.*;
4035
import net.minecraft.registry.tag.TagKey;
4136
import net.minecraft.resource.ResourceType;
4237
import net.minecraft.resource.featuretoggle.FeatureFlags;
4338
import net.minecraft.screen.ScreenHandlerType;
39+
import net.minecraft.server.MinecraftServer;
4440
import net.minecraft.sound.SoundEvent;
4541
import net.minecraft.util.Identifier;
4642
import net.minecraft.util.math.BlockPos;
4743
import net.minecraft.world.GameRules;
4844
import org.apache.logging.log4j.LogManager;
4945
import org.apache.logging.log4j.Logger;
46+
import java.util.HashMap;
47+
import java.util.Map;
5048

5149
public class NumismaticOverhaul implements ModInitializer {
5250

@@ -59,8 +57,8 @@ public class NumismaticOverhaul implements ModInitializer {
5957
ClientParticles.setParticleCount(6 * data);
6058
ClientParticles.randomizeVelocity(2);
6159
ClientParticles.spawnCenteredOnBlock(
62-
new BlockStateParticleEffect(ParticleTypes.BLOCK, NumismaticOverhaulBlocks.PIGGY_BANK.getDefaultState()),
63-
world, new BlockPos((int) pos.x, (int) pos.y, (int) pos.z), .75
60+
new BlockStateParticleEffect(ParticleTypes.BLOCK, NumismaticOverhaulBlocks.PIGGY_BANK.getDefaultState()),
61+
world, new BlockPos((int) pos.x, (int) pos.y, (int) pos.z), .75
6462
);
6563
});
6664

@@ -74,27 +72,31 @@ world, new BlockPos((int) pos.x, (int) pos.y, (int) pos.z), .75
7472
public static final TagKey<Block> VERY_HEAVY_BLOCKS = TagKey.of(RegistryKeys.BLOCK, id("very_heavy_blocks"));
7573

7674
public static final GameRules.Key<GameRules.IntRule> MONEY_DROP_PERCENTAGE
77-
= GameRuleRegistry.register("moneyDropPercentage", GameRules.Category.PLAYER, GameRuleFactory.createIntRule(10, 0, 100));
75+
= GameRuleRegistry.register("moneyDropPercentage", GameRules.Category.PLAYER, GameRuleFactory.createIntRule(10, 0, 100));
7876

7977
public static final GameRules.Key<GameRules.IntRule> MONEY_MOB_DROP_VARIANCE
8078
= GameRuleRegistry.register("moneyMobDropVariancePercentage", GameRules.Category.MOBS, GameRuleFactory.createIntRule(50, 0, 100));
8179

8280
public static final ComponentType<MoneyBagComponent> MONEY_BAG_COMPONENT = MoneyBagComponent.register();
8381

8482
public static final OwoItemGroup NUMISMATIC_GROUP = OwoItemGroup.builder(
85-
NumismaticOverhaul.id("main"),
86-
() -> Icon.of(MoneyBagItem.fromRawValue(100)))
87-
.initializer(group -> {
88-
group.addButton(ItemGroupButton.modrinth(group, "https://modrinth.com/mod/numismatic-overhaul"));
89-
group.addButton(ItemGroupButton.curseforge(group, "https://www.curseforge.com/minecraft/mc-mods/numismatic-overhaul"));
90-
group.addButton(ItemGroupButton.github(group, "https://github.com/wisp-forest/numismatic-overhaul"));
91-
group.addButton(ItemGroupButton.discord(group, "https://discord.gg/xrwHKktV2d"));
92-
}).build();
83+
NumismaticOverhaul.id("main"),
84+
() -> Icon.of(MoneyBagItem.fromRawValue(100)))
85+
.initializer(group -> {
86+
group.addButton(ItemGroupButton.modrinth(group, "https://modrinth.com/mod/numismatic-overhaul"));
87+
group.addButton(ItemGroupButton.curseforge(group, "https://www.curseforge.com/minecraft/mc-mods/numismatic-overhaul"));
88+
group.addButton(ItemGroupButton.github(group, "https://github.com/wisp-forest/numismatic-overhaul"));
89+
group.addButton(ItemGroupButton.discord(group, "https://discord.gg/xrwHKktV2d"));
90+
}).build();
91+
92+
public static final Map<EntityType<?>, Integer> MOBS_IN_BOURGEOISIE = new HashMap<>();
9393

9494
public static final com.glisco.numismaticoverhaul.NumismaticOverhaulConfig CONFIG = com.glisco.numismaticoverhaul.NumismaticOverhaulConfig.createAndLoad();
9595

9696
@Override
9797
public void onInitialize() {
98+
// this type of code truly feels like DH code
99+
ServerLifecycleEvents.SERVER_STARTED.register(NumismaticOverhaul::loadMobDropConfig);
98100

99101
FieldRegistrationHandler.register(NumismaticOverhaulItems.class, MOD_ID, false);
100102
FieldRegistrationHandler.register(NumismaticOverhaulBlocks.class, MOD_ID, false);
@@ -117,27 +119,28 @@ public void onInitialize() {
117119

118120
ServerLifecycleEvents.END_DATA_PACK_RELOAD.register((server, serverResourceManager, success) -> {
119121
VillagerTradesHandler.broadcastErrors(server);
122+
CONFIG.subscribeToMobsToBaseValues(NumismaticOverhaul::reloadMobDropConfig);
120123
});
121124

122125
NUMISMATIC_GROUP.initialize();
123126

124127
if (CONFIG.generateCurrencyInChests()) {
125128
LootOps.injectItem(NumismaticOverhaulItems.GOLD_COIN, .01f, LootTables.STRONGHOLD_LIBRARY_CHEST.getValue(), LootTables.BASTION_TREASURE_CHEST.getValue(), LootTables.STRONGHOLD_CORRIDOR_CHEST.getValue(),
126-
LootTables.PILLAGER_OUTPOST_CHEST.getValue(), LootTables.BURIED_TREASURE_CHEST.getValue(), LootTables.SIMPLE_DUNGEON_CHEST.getValue(), LootTables.ABANDONED_MINESHAFT_CHEST.getValue());
129+
LootTables.PILLAGER_OUTPOST_CHEST.getValue(), LootTables.BURIED_TREASURE_CHEST.getValue(), LootTables.SIMPLE_DUNGEON_CHEST.getValue(), LootTables.ABANDONED_MINESHAFT_CHEST.getValue());
127130

128131
LootTableEvents.MODIFY.register((key, tableBuilder, source, registries) -> {
129132
if (anyMatch(key.getValue(), LootTables.DESERT_PYRAMID_CHEST.getValue())) {
130133
tableBuilder.pool(LootPool.builder().with(MoneyBagLootEntry.builder(CONFIG.lootOptions.desertMinLoot(), CONFIG.lootOptions.desertMaxLoot()))
131-
.conditionally(RandomChanceLootCondition.builder(0.45f)));
134+
.conditionally(RandomChanceLootCondition.builder(0.45f)));
132135
} else if (anyMatch(key.getValue(), LootTables.SIMPLE_DUNGEON_CHEST.getValue(), LootTables.ABANDONED_MINESHAFT_CHEST.getValue())) {
133136
tableBuilder.pool(LootPool.builder().with(MoneyBagLootEntry.builder(CONFIG.lootOptions.dungeonMinLoot(), CONFIG.lootOptions.dungeonMaxLoot()))
134-
.conditionally(RandomChanceLootCondition.builder(0.75f)));
137+
.conditionally(RandomChanceLootCondition.builder(0.75f)));
135138
} else if (anyMatch(key.getValue(), LootTables.BASTION_TREASURE_CHEST.getValue(), LootTables.STRONGHOLD_CORRIDOR_CHEST.getValue(), LootTables.PILLAGER_OUTPOST_CHEST.getValue(), LootTables.BURIED_TREASURE_CHEST.getValue())) {
136139
tableBuilder.pool(LootPool.builder().with(MoneyBagLootEntry.builder(CONFIG.lootOptions.structureMinLoot(), CONFIG.lootOptions.structureMaxLoot()))
137-
.conditionally(RandomChanceLootCondition.builder(0.75f)));
140+
.conditionally(RandomChanceLootCondition.builder(0.75f)));
138141
} else if (anyMatch(key.getValue(), LootTables.STRONGHOLD_LIBRARY_CHEST.getValue())) {
139142
tableBuilder.pool(LootPool.builder().with(MoneyBagLootEntry.builder(CONFIG.lootOptions.strongholdLibraryMinLoot(), CONFIG.lootOptions.strongholdLibraryMaxLoot()))
140-
.conditionally(RandomChanceLootCondition.builder(0.85f)));
143+
.conditionally(RandomChanceLootCondition.builder(0.85f)));
141144
}
142145
});
143146
}
@@ -153,4 +156,25 @@ private static boolean anyMatch(Identifier target, Identifier... comparisons) {
153156
public static Identifier id(String path) {
154157
return Identifier.of(MOD_ID, path);
155158
}
159+
160+
private static void loadMobDropConfig(MinecraftServer ignored) {
161+
CONFIG.mobsToBaseValues().forEach((s, baseValue) -> {
162+
if (s.startsWith("#")) {
163+
Registries.ENTITY_TYPE.getEntryList(TagKey.of(RegistryKeys.ENTITY_TYPE, Identifier.of(s.split("#")[1]))).ifPresent(registryEntries -> {
164+
registryEntries.forEach(entityTypeRegistryEntry -> MOBS_IN_BOURGEOISIE.put(entityTypeRegistryEntry.value(), baseValue));
165+
});
166+
} else {
167+
var entityOpt = Registries.ENTITY_TYPE.getOrEmpty(Identifier.of(s));
168+
entityOpt.ifPresentOrElse(entityType -> MOBS_IN_BOURGEOISIE.put(entityType, baseValue), () -> {
169+
LOGGER.error("[Numismatic Overhaul] Could not find entity type or entity type tag '{}' for mob drops", s);
170+
});
171+
}
172+
});
173+
}
174+
175+
private static void reloadMobDropConfig(Map<String, Integer> ignored) {
176+
MOBS_IN_BOURGEOISIE.clear();
177+
CONFIG.load();
178+
loadMobDropConfig(null);
179+
}
156180
}

‎src/main/java/com/glisco/numismaticoverhaul/NumismaticOverhaulConfigModel.java‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import blue.endless.jankson.Comment;
44
import io.wispforest.owo.config.Option;
55
import io.wispforest.owo.config.annotation.*;
6-
import net.minecraft.util.Identifier;
76
import java.util.Map;
87

98
@SuppressWarnings("unused")
@@ -70,19 +69,22 @@ public enum MoneyMessageLocation {
7069
"mobsToBaseValues": {
7170
"minecraft:pillager": 80,
7271
}
73-
Requires a restart to reload correctly!
72+
73+
Another example which uses tags:
74+
"mobsToBaseValues": {
75+
"#numismatic-overhaul:the_bourgeoisie": 75
76+
}
77+
Run /reload when you are finished to reapply the config.
7478
""")
75-
@RestartRequired
76-
public Map<Identifier, Integer> mobsToBaseValues = Map.of();
79+
@Hook
80+
public Map<String, Integer> mobsToBaseValues = Map.of("#" + NumismaticOverhaul.THE_BOURGEOISIE.id().toString(), 75);
7781

78-
@RestartRequired
7982
@Comment("Scales the money dropped based on the mobs max health. Money dropped is multiplied by '(mob max health) / (20 * healthScaleReduction)'")
8083
public boolean scaleOnHealth = false;
8184

8285
@Comment("""
8386
Reduces the impact that health scaling has on money dropped from mobs.
8487
Requires a restart to reload correctly!
8588
""")
86-
@RestartRequired
8789
public float healthScaleReduction = 1.0f;
8890
}

‎src/main/java/com/glisco/numismaticoverhaul/mixin/LivingEntityMixin.java‎

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22

33
import com.glisco.numismaticoverhaul.NumismaticOverhaul;
44
import com.glisco.numismaticoverhaul.currency.CurrencyHelper;
5-
import com.glisco.numismaticoverhaul.item.NumismaticOverhaulItems;
6-
import net.minecraft.entity.Entity;
7-
import net.minecraft.entity.EntityType;
8-
import net.minecraft.entity.LivingEntity;
5+
import net.minecraft.entity.*;
96
import net.minecraft.entity.damage.DamageSource;
107
import net.minecraft.entity.player.PlayerEntity;
11-
import net.minecraft.item.ItemStack;
12-
import net.minecraft.registry.Registries;
138
import net.minecraft.util.math.MathHelper;
149
import net.minecraft.world.World;
1510
import org.jetbrains.annotations.Nullable;
@@ -36,9 +31,9 @@ public LivingEntityMixin(EntityType<?> type, World world) {
3631
public void injectCoins(DamageSource source, boolean causedByPlayer, CallbackInfo ci) {
3732
if (this.attackingPlayer == null) return;
3833
// Handle config dependent coin drops
39-
var entityId = Registries.ENTITY_TYPE.getId(this.getType());
40-
if (CONFIG.mobsToBaseValues().containsKey(entityId)) {
41-
long baseValue = CONFIG.mobsToBaseValues().get(entityId);
34+
var entityType = this.getType();
35+
if (NumismaticOverhaul.MOBS_IN_BOURGEOISIE.containsKey(entityType)) {
36+
long baseValue = NumismaticOverhaul.MOBS_IN_BOURGEOISIE.get(entityType);
4237
float variance = this.getWorld().getGameRules().get(NumismaticOverhaul.MONEY_MOB_DROP_VARIANCE).get() * .01f;
4338
if (variance > 0.02f) {
4439
variance = MathHelper.nextBetween(random, 1.0f - variance, 1.0f + variance);
@@ -52,11 +47,6 @@ public void injectCoins(DamageSource source, boolean causedByPlayer, CallbackInf
5247
var moneyStacks = CurrencyHelper.getAsStacks(finalValue, 4);
5348
moneyStacks.forEach(this::dropStack);
5449
}
55-
// Handle mod integration via tag
56-
if (!this.getType().isIn(NumismaticOverhaul.THE_BOURGEOISIE)) return;
57-
if (random.nextFloat() > .5f)
58-
dropStack(new ItemStack(NumismaticOverhaulItems.BRONZE_COIN, random.nextBetween(9, 35)));
59-
if (random.nextFloat() > .2f) dropStack(new ItemStack(NumismaticOverhaulItems.SILVER_COIN));
6050
}
6151

6252
}

0 commit comments

Comments
 (0)