-
Notifications
You must be signed in to change notification settings - Fork 308
Expand file tree
/
Copy pathGlobalLootModifier.java
More file actions
370 lines (328 loc) · 14.1 KB
/
GlobalLootModifier.java
File metadata and controls
370 lines (328 loc) · 14.1 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package wayoftime.bloodmagic.loot;
import com.google.common.base.Suppliers;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.item.crafting.SmeltingRecipe;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.LootParams;
import net.minecraft.world.level.storage.loot.LootTable;
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.loot.IGlobalLootModifier;
import net.minecraftforge.common.loot.LootModifier;
import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import org.jetbrains.annotations.NotNull;
import wayoftime.bloodmagic.BloodMagic;
import wayoftime.bloodmagic.anointment.AnointmentData;
import wayoftime.bloodmagic.anointment.AnointmentHolder;
import wayoftime.bloodmagic.common.tags.BloodMagicTags;
import wayoftime.bloodmagic.core.AnointmentRegistrar;
import wayoftime.bloodmagic.util.BMLog;
import javax.annotation.Nonnull;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
public class GlobalLootModifier
{
// public static final DeferredRegister<GlobalLootModifierSerializer<?>> GLM = DeferredRegister.create(ForgeRegistries.LOOT_MODIFIER_SERIALIZERS, BloodMagic.MODID);
public static final DeferredRegister<Codec<? extends IGlobalLootModifier>> GLM = DeferredRegister.create(ForgeRegistries.Keys.GLOBAL_LOOT_MODIFIER_SERIALIZERS, BloodMagic.MODID);
public static final RegistryObject<Codec<SilkTouchTestModifier>> SILKTOUCH = GLM.register("silk_touch_bamboo", SilkTouchTestModifier.CODEC);
public static final RegistryObject<Codec<FortuneModifier>> FORTUNE = GLM.register("fortune", FortuneModifier.CODEC);
public static final RegistryObject<Codec<LootingModifier>> LOOTING = GLM.register("looting", LootingModifier.CODEC);
public static final RegistryObject<Codec<SmeltingModifier>> SMELT = GLM.register("smelt", SmeltingModifier.CODEC);
public static final RegistryObject<Codec<VoidingModifier>> VOID = GLM.register("voiding", VoidingModifier.CODEC);
private static class SilkTouchTestModifier extends LootModifier
{
public static final Supplier<Codec<SilkTouchTestModifier>> CODEC = Suppliers.memoize(() -> RecordCodecBuilder.create(inst -> codecStart(inst).apply(inst, SilkTouchTestModifier::new)));
public SilkTouchTestModifier(LootItemCondition[] conditionsIn)
{
super(conditionsIn);
// System.out.println("Registering silk touch modifier");
}
@Nonnull
@Override
protected @NotNull ObjectArrayList<ItemStack> doApply(ObjectArrayList<ItemStack> generatedLoot, LootContext context) {
ItemStack ctxTool = context.getParamOrNull(LootContextParams.TOOL);
// charges use anointment differently
if (ctxTool.is(BloodMagicTags.CHARGES)) {
return generatedLoot;
}
BlockEntity be = context.getParamOrNull(LootContextParams.BLOCK_ENTITY);
// dont apply on BE's
if (be != null) {
return generatedLoot;
}
// return early if silk-touch is already applied (otherwise we'll get stuck in
// an infinite loop).
if (EnchantmentHelper.getEnchantments(ctxTool).containsKey(Enchantments.SILK_TOUCH)) {
return generatedLoot;
}
AnointmentHolder holder = AnointmentHolder.fromItemStack(ctxTool);
if (holder == null || holder.getAnointmentLevel(AnointmentRegistrar.ANOINTMENT_SILK_TOUCH.get()) <= 0)
{
return generatedLoot;
}
ItemStack fakeTool = ctxTool.copy();
fakeTool.enchant(Enchantments.SILK_TOUCH, 1);
ResourceLocation tableID = context.getQueriedLootTableId();
ServerLevel level = context.getLevel();
LootTable table = level.getServer().getLootData().getLootTable(tableID);
LootParams params = new LootParams.Builder(level)
.withParameter(LootContextParams.ORIGIN, context.getParam(LootContextParams.ORIGIN))
.withParameter(LootContextParams.BLOCK_STATE, context.getParam(LootContextParams.BLOCK_STATE))
.withParameter(LootContextParams.TOOL, fakeTool)
.withOptionalParameter(LootContextParams.THIS_ENTITY, context.getParamOrNull(LootContextParams.THIS_ENTITY))
.withLuck(context.getLuck())
.create(LootContextParamSets.BLOCK);
return table.getRandomItems(params);
}
@Override
public Codec<? extends IGlobalLootModifier> codec() {
return CODEC.get();
}
}
private static class FortuneModifier extends LootModifier
{
public static final Supplier<Codec<FortuneModifier>> CODEC = Suppliers.memoize(() -> RecordCodecBuilder.create(inst -> codecStart(inst).apply(inst, FortuneModifier::new)));
public FortuneModifier(LootItemCondition[] conditionsIn)
{
super(conditionsIn);
// System.out.println("Registering silk touch modifier");
}
// List<ItemStack> bufferList = new ArrayList<ItemStack>();
@Nonnull
@Override
protected @NotNull ObjectArrayList<ItemStack> doApply(ObjectArrayList<ItemStack> generatedLoot, LootContext context) {
ItemStack ctxTool = context.getParamOrNull(LootContextParams.TOOL);
// return early if silk-touch is already applied (otherwise we'll get stuck in
// an infinite loop).
if (ctxTool.getTag() != null && ctxTool.getTag().getBoolean("bloodmagic:checked_fortune"))
{
return generatedLoot;
}
if (ctxTool.is(BloodMagicTags.CHARGES)) {
return generatedLoot;
}
BlockEntity be = context.getParamOrNull(LootContextParams.BLOCK_ENTITY);
// dont apply on BE's
if (be != null) {
return generatedLoot;
}
if (EnchantmentHelper.getEnchantments(ctxTool).containsKey(Enchantments.SILK_TOUCH)) {
return generatedLoot;
}
AnointmentHolder holder = AnointmentHolder.fromItemStack(ctxTool);
if (holder == null)
{
return generatedLoot;
}
int additionalFortune = holder.getAnointmentLevel(AnointmentRegistrar.ANOINTMENT_FORTUNE.get());
if (additionalFortune <= 0)
{
return generatedLoot;
}
ItemStack fakeTool = ctxTool.copy();
fakeTool.getOrCreateTag().putBoolean("bloodmagic:checked_fortune", true);
int baseFortuneLevel = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.BLOCK_FORTUNE, ctxTool);
Map<Enchantment, Integer> enchants = EnchantmentHelper.getEnchantments(fakeTool);
enchants.put(Enchantments.BLOCK_FORTUNE, baseFortuneLevel + additionalFortune);
EnchantmentHelper.setEnchantments(enchants, fakeTool);
ResourceLocation tableID = context.getQueriedLootTableId();
ServerLevel level = context.getLevel();
LootTable table = level.getServer().getLootData().getLootTable(tableID);
LootParams params = new LootParams.Builder(level)
.withParameter(LootContextParams.ORIGIN, context.getParam(LootContextParams.ORIGIN))
.withParameter(LootContextParams.BLOCK_STATE, context.getParam(LootContextParams.BLOCK_STATE))
.withParameter(LootContextParams.TOOL, fakeTool)
.withOptionalParameter(LootContextParams.THIS_ENTITY, context.getParamOrNull(LootContextParams.THIS_ENTITY))
.withLuck(context.getLuck())
.create(LootContextParamSets.BLOCK);
return table.getRandomItems(params);
}
@Override
public Codec<? extends IGlobalLootModifier> codec() {
return CODEC.get();
}
}
private static class LootingModifier extends LootModifier
{
public static final Supplier<Codec<LootingModifier>> CODEC = Suppliers.memoize(() -> RecordCodecBuilder.create(inst -> codecStart(inst).apply(inst, LootingModifier::new)));
public LootingModifier(LootItemCondition[] conditionsIn)
{
super(conditionsIn);
}
@Nonnull
@Override
protected @NotNull ObjectArrayList<ItemStack> doApply(ObjectArrayList<ItemStack> generatedLoot, LootContext context) {
// System.out.println("Checking for looting");
// Entity killerEntity = context.get(LootParameters.KILLER_ENTITY);
// if (!(killerEntity instanceof PlayerEntity))
// {
return generatedLoot;
// }
// Entity killedEntity = context.get(LootParameters.THIS_ENTITY);
// if (!(killedEntity instanceof LivingEntity))
// {
// return generatedLoot;
// }
// ItemStack ctxTool = ((PlayerEntity) killerEntity).getHeldItemMainhand();
// // return early if silk-touch is already applied (otherwise we'll get stuck in
// // an infinite loop).
//
// System.out.println("Checking looting. ItemStack context: ");
//// EndermanEntity d;
// if (ctxTool.getTag() != null && ctxTool.getTag().getBoolean("bloodmagic:checked_looting"))
// {
// return generatedLoot;
// }
//
// if (EnchantmentHelper.getEnchantments(ctxTool).containsKey(Enchantments.SILK_TOUCH))
// return generatedLoot;
// AnointmentHolder holder = AnointmentHolder.fromItemStack(ctxTool);
// if (holder == null)
// {
// return generatedLoot;
// }
//
// int additionalLooting = holder.getAnointmentLevel(AnointmentRegistrar.ANOINTMENT_LOOTING.get()) * 20;
// if (additionalLooting <= 0)
// {
// return generatedLoot;
// }
//
//// if (holder.getAnointmentLevel(AnointmentRegistrar.ANOINTMENT_SILK_TOUCH.get()) > 0)
//// {
//// return generatedLoot;
//// }
//
// ItemStack fakeTool = ctxTool.copy();
// fakeTool.getOrCreateTag().putBoolean("bloodmagic:checked_looting", true);
// int baseLootingLevel = EnchantmentHelper.getEnchantmentLevel(Enchantments.LOOTING, ctxTool);
//
// fakeTool.addEnchantment(Enchantments.LOOTING, baseLootingLevel + additionalLooting);
// LootContext.Builder builder = new LootContext.Builder(context);
// builder.withParameter(LootParameters.TOOL, fakeTool);
// LootContext ctx = builder.build(LootParameterSets.ENTITY);
// ResourceLocation resource = ((LivingEntity) killedEntity).getLootTableResourceLocation();
// LootTable loottable = context.getWorld().getServer().getLootTableManager().getLootTableFromLocation(resource);
// return loottable.generate(ctx);
}
@Override
public Codec<? extends IGlobalLootModifier> codec() {
return CODEC.get();
}
}
private static class SmeltingModifier extends LootModifier
{
public static final Supplier<Codec<SmeltingModifier>> CODEC = Suppliers.memoize(() -> RecordCodecBuilder.create(inst -> codecStart(inst).apply(inst, SmeltingModifier::new)));
public SmeltingModifier(LootItemCondition[] conditionsIn)
{
super(conditionsIn);
}
@Nonnull
@Override
protected @NotNull ObjectArrayList<ItemStack> doApply(ObjectArrayList<ItemStack> generatedLoot, LootContext context) {
ItemStack ctxTool = context.getParamOrNull(LootContextParams.TOOL);
// return early if silk-touch is already applied (otherwise we'll get stuck in
// an infinite loop).
if (ctxTool.getTag() == null)
{
return generatedLoot;
}
if (ctxTool.is(BloodMagicTags.CHARGES)) {
return generatedLoot;
}
AnointmentHolder holder = AnointmentHolder.fromItemStack(ctxTool);
if (holder == null)
{
return generatedLoot;
}
int smeltingLevel = holder.getAnointmentLevel(AnointmentRegistrar.ANOINTMENT_SMELTING.get());
if (smeltingLevel <= 0)
{
return generatedLoot;
}
ObjectArrayList<ItemStack> ret = new ObjectArrayList<>();
generatedLoot.forEach((stack) -> ret.add(smelt(stack, context)));
return ret;
}
private static ItemStack smelt(ItemStack stack, LootContext context)
{
return context.getLevel().getRecipeManager().getRecipeFor(RecipeType.SMELTING, new SimpleContainer(stack), context.getLevel())
.map((a) -> a.getResultItem(context.getLevel().registryAccess()))
.filter(itemStack -> !itemStack.isEmpty())
.map(itemStack -> ItemHandlerHelper.copyStackWithSize(itemStack, stack.getCount() * itemStack.getCount()))
.orElse(stack);
}
@Override
public Codec<? extends IGlobalLootModifier> codec() {
return CODEC.get();
}
}
private static class VoidingModifier extends LootModifier
{
public static final Supplier<Codec<VoidingModifier>> CODEC = Suppliers.memoize(() -> RecordCodecBuilder.create(inst -> codecStart(inst).apply(inst, VoidingModifier::new)));
public VoidingModifier(LootItemCondition[] conditionsIn)
{
super(conditionsIn);
}
@Nonnull
@Override
protected @NotNull ObjectArrayList<ItemStack> doApply(ObjectArrayList<ItemStack> generatedLoot, LootContext context) {
BlockState blockState = context.getParamOrNull(LootContextParams.BLOCK_STATE);
if (blockState == null)
{
return generatedLoot;
}
if (!blockState.is(BloodMagicTags.Blocks.MUNDANE_BLOCK))
{
return generatedLoot;
}
ItemStack ctxTool = context.getParamOrNull(LootContextParams.TOOL);
// return early if silk-touch is already applied (otherwise we'll get stuck in
// an infinite loop).
if (ctxTool.getTag() == null)
{
return generatedLoot;
}
if (ctxTool.is(BloodMagicTags.CHARGES)) {
return generatedLoot;
}
AnointmentHolder holder = AnointmentHolder.fromItemStack(ctxTool);
if (holder == null)
{
return generatedLoot;
}
int voidingLevel = holder.getAnointmentLevel(AnointmentRegistrar.ANOINTMENT_VOIDING.get());
if (voidingLevel <= 0)
{
return generatedLoot;
}
ObjectArrayList<ItemStack> ret = new ObjectArrayList<>();
// generatedLoot.forEach((stack) -> ret.add(smelt(stack, context)));
return ret;
}
@Override
public Codec<? extends IGlobalLootModifier> codec() {
return CODEC.get();
}
}
}