-
Notifications
You must be signed in to change notification settings - Fork 754
/
WorldEvents.java
175 lines (162 loc) · 7.68 KB
/
WorldEvents.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package slimeknights.tconstruct.world;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.monster.Creeper;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.SkullBlock;
import net.minecraft.world.level.storage.loot.LootPool;
import net.minecraft.world.level.storage.loot.entries.LootItem;
import net.minecraft.world.level.storage.loot.entries.LootPoolEntryContainer;
import net.minecraft.world.level.storage.loot.functions.SetItemCountFunction;
import net.minecraft.world.level.storage.loot.providers.number.UniformGenerator;
import net.minecraftforge.event.LootTableLoadEvent;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
import net.minecraftforge.event.entity.living.LivingEvent.LivingVisibilityEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fml.common.Mod;
import slimeknights.mantle.loot.function.SetFluidLootFunction;
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.common.config.Config;
import slimeknights.tconstruct.fluids.TinkerFluids;
import slimeknights.tconstruct.library.json.loot.AddToolDataFunction;
import slimeknights.tconstruct.library.materials.RandomMaterial;
import slimeknights.tconstruct.library.recipe.FluidValues;
import slimeknights.tconstruct.smeltery.TinkerSmeltery;
import slimeknights.tconstruct.tools.TinkerTools;
import slimeknights.tconstruct.world.block.FoliageType;
import java.util.Arrays;
@SuppressWarnings("unused")
@Mod.EventBusSubscriber(modid = TConstruct.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class WorldEvents {
/* Loot injection */
/**
* Injects an entry into a loot pool
* @param event Loot table event
* @param poolName Pool name
* @param entries Entry to inject
*/
private static void injectInto(LootTableLoadEvent event, String poolName, LootPoolEntryContainer... entries) {
LootPool pool = event.getTable().getPool(poolName);
//noinspection ConstantConditions method is annotated wrongly
if (pool != null) {
int oldLength = pool.entries.length;
pool.entries = Arrays.copyOf(pool.entries, oldLength + entries.length);
System.arraycopy(entries, 0, pool.entries, oldLength, entries.length);
}
}
/** Makes a seed injection loot entry */
private static LootPoolEntryContainer makeSeed(FoliageType type, int weight) {
return LootItem.lootTableItem(TinkerWorld.slimeGrassSeeds.get(type)).setWeight(weight)
.apply(SetItemCountFunction.setCount(UniformGenerator.between(2, 4))).build();
}
/** Makes a sapling injection loot entry */
private static LootPoolEntryContainer makeSapling(FoliageType type, int weight) {
return LootItem.lootTableItem(TinkerWorld.slimeSapling.get(type)).setWeight(weight).build();
}
@SubscribeEvent
static void onLootTableLoad(LootTableLoadEvent event) {
ResourceLocation name = event.getName();
if ("minecraft".equals(name.getNamespace())) {
switch (name.getPath()) {
// sky
case "chests/simple_dungeon":
if (Config.COMMON.slimyLootChests.get()) {
injectInto(event, "pool1", makeSeed(FoliageType.EARTH, 3), makeSeed(FoliageType.SKY, 7));
injectInto(event, "main", makeSapling(FoliageType.EARTH, 3), makeSapling(FoliageType.SKY, 7));
}
break;
// ichor
case "chests/nether_bridge":
if (Config.COMMON.slimyLootChests.get()) {
injectInto(event, "main", makeSeed(FoliageType.BLOOD, 5));
}
break;
case "chests/bastion_bridge":
if (Config.COMMON.slimyLootChests.get()) {
injectInto(event, "pool2", makeSapling(FoliageType.BLOOD, 1));
}
break;
// ender
case "chests/end_city_treasure":
if (Config.COMMON.slimyLootChests.get()) {
injectInto(event, "main", makeSeed(FoliageType.ENDER, 5), makeSapling(FoliageType.ENDER, 3));
}
break;
// barter for molten blaze lanterns
case "gameplay/piglin_bartering": {
int weight = Config.COMMON.barterBlazingBlood.get();
if (weight > 0) {
injectInto(event, "main", LootItem.lootTableItem(TinkerSmeltery.scorchedLantern).setWeight(weight)
.apply(SetFluidLootFunction.builder(new FluidStack(TinkerFluids.blazingBlood.get(), FluidValues.LANTERN_CAPACITY)))
.apply(SetItemCountFunction.setCount(UniformGenerator.between(1, 4)))
.build());
}
break;
}
// randomly swap vanilla tool for a tinkers tool
case "chests/spawn_bonus_chest": {
int weight = Config.COMMON.tinkerToolBonusChest.get();
if (weight > 0) {
RandomMaterial randomTier1 = RandomMaterial.random().tier(1).build();
RandomMaterial firstWithStat = RandomMaterial.firstWithStat(); // should be wood
injectInto(event, "main", LootItem.lootTableItem(TinkerTools.handAxe.get())
.setWeight(weight)
.apply(AddToolDataFunction.builder()
.addMaterial(randomTier1)
.addMaterial(firstWithStat)
.addMaterial(randomTier1))
.build());
injectInto(event, "pool1", LootItem.lootTableItem(TinkerTools.pickaxe.get())
.setWeight(weight)
.apply(AddToolDataFunction.builder()
.addMaterial(randomTier1)
.addMaterial(firstWithStat)
.addMaterial(randomTier1))
.build());
}
break;
}
}
}
}
/* Heads */
@SubscribeEvent
static void livingVisibility(LivingVisibilityEvent event) {
Entity lookingEntity = event.getLookingEntity();
if (lookingEntity == null) {
return;
}
LivingEntity entity = event.getEntity();
ItemStack helmet = entity.getItemBySlot(EquipmentSlot.HEAD);
Item item = helmet.getItem();
if (item != Items.AIR && TinkerWorld.headItems.contains(item)) {
if (lookingEntity.getType() == ((TinkerHeadType)((SkullBlock)((BlockItem)item).getBlock()).getType()).getType()) {
event.modifyVisibility(0.5f);
}
}
}
@SubscribeEvent
static void creeperKill(LivingDropsEvent event) {
DamageSource source = event.getSource();
if (source != null) {
Entity entity = source.getEntity();
if (entity instanceof Creeper creeper) {
if (creeper.canDropMobsSkull()) {
LivingEntity dying = event.getEntity();
TinkerHeadType headType = TinkerHeadType.fromEntityType(dying.getType());
if (headType != null && Config.COMMON.headDrops.get(headType).get()) {
creeper.increaseDroppedSkulls();
event.getDrops().add(dying.spawnAtLocation(TinkerWorld.heads.get(headType)));
}
}
}
}
}
}