From f7e7467d5f7602a846d2124bfe89271af89c3ac5 Mon Sep 17 00:00:00 2001 From: Leviathan143 Date: Wed, 4 Oct 2017 15:28:56 +1300 Subject: [PATCH 1/5] Add missing data to LootContexts --- .../advancements/AdvancementRewards.java.patch | 11 +++++++++++ .../item/EntityMinecartContainer.java.patch | 16 +++++++++++++++- .../entity/projectile/EntityFishHook.java.patch | 6 ++++-- .../tileentity/TileEntityLockableLoot.java.patch | 11 +++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 patches/minecraft/net/minecraft/advancements/AdvancementRewards.java.patch create mode 100644 patches/minecraft/net/minecraft/tileentity/TileEntityLockableLoot.java.patch diff --git a/patches/minecraft/net/minecraft/advancements/AdvancementRewards.java.patch b/patches/minecraft/net/minecraft/advancements/AdvancementRewards.java.patch new file mode 100644 index 00000000000..782e7931e9b --- /dev/null +++ b/patches/minecraft/net/minecraft/advancements/AdvancementRewards.java.patch @@ -0,0 +1,11 @@ +--- ../src-base/minecraft/net/minecraft/advancements/AdvancementRewards.java ++++ ../src-work/minecraft/net/minecraft/advancements/AdvancementRewards.java +@@ -49,7 +49,7 @@ + public void func_192113_a(final EntityPlayerMP p_192113_1_) + { + p_192113_1_.func_71023_q(this.field_192115_b); +- LootContext lootcontext = (new LootContext.Builder(p_192113_1_.func_71121_q())).func_186472_a(p_192113_1_).func_186471_a(); ++ LootContext lootcontext = (new LootContext.Builder(p_192113_1_.func_71121_q())).func_186472_a(p_192113_1_).func_186470_a(p_192113_1_).func_186469_a(p_192113_1_.func_184817_da()).func_186471_a(); // Forge: add player & luck to LootContext + boolean flag = false; + + for (ResourceLocation resourcelocation : this.field_192116_c) diff --git a/patches/minecraft/net/minecraft/entity/item/EntityMinecartContainer.java.patch b/patches/minecraft/net/minecraft/entity/item/EntityMinecartContainer.java.patch index ec3b8b0e66a..7d385d55d0c 100644 --- a/patches/minecraft/net/minecraft/entity/item/EntityMinecartContainer.java.patch +++ b/patches/minecraft/net/minecraft/entity/item/EntityMinecartContainer.java.patch @@ -8,7 +8,21 @@ if (!this.field_70170_p.field_72995_K) { p_184230_1_.func_71007_a(this); -@@ -281,6 +282,26 @@ +@@ -270,17 +271,37 @@ + random = new Random(this.field_184291_d); + } + +- LootContext.Builder lootcontext$builder = new LootContext.Builder((WorldServer)this.field_70170_p); +- ++ LootContext.Builder lootcontext$builder = new LootContext.Builder((WorldServer)this.field_70170_p).func_186472_a(this); // Forge: add looted entity to LootContext ++ + if (p_184288_1_ != null) + { +- lootcontext$builder.func_186469_a(p_184288_1_.func_184817_da()); ++ lootcontext$builder.func_186469_a(p_184288_1_.func_184817_da()).func_186470_a(p_184288_1_); // Forge: add player to LootContext + } + + loottable.func_186460_a(this, random, lootcontext$builder.func_186471_a()); } } diff --git a/patches/minecraft/net/minecraft/entity/projectile/EntityFishHook.java.patch b/patches/minecraft/net/minecraft/entity/projectile/EntityFishHook.java.patch index 1a66d5787a2..c47db9a76a4 100644 --- a/patches/minecraft/net/minecraft/entity/projectile/EntityFishHook.java.patch +++ b/patches/minecraft/net/minecraft/entity/projectile/EntityFishHook.java.patch @@ -19,10 +19,12 @@ if (this.field_146043_c != null) { this.func_184527_k(); -@@ -516,8 +517,16 @@ +@@ -515,9 +516,17 @@ + else if (this.field_146045_ax > 0) { LootContext.Builder lootcontext$builder = new LootContext.Builder((WorldServer)this.field_70170_p); - lootcontext$builder.func_186469_a((float)this.field_191518_aw + this.field_146042_b.func_184817_da()); +- lootcontext$builder.func_186469_a((float)this.field_191518_aw + this.field_146042_b.func_184817_da()); ++ lootcontext$builder.func_186469_a((float)this.field_191518_aw + this.field_146042_b.func_184817_da()).func_186470_a(this.field_146042_b).func_186472_a(this); // Forge: add player & looted entity to LootContext + List result = this.field_70170_p.func_184146_ak().func_186521_a(LootTableList.field_186387_al).func_186462_a(this.field_70146_Z, lootcontext$builder.func_186471_a()); + event = new net.minecraftforge.event.entity.player.ItemFishedEvent(result, this.field_146051_au ? 2 : 1, this); + net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(event); diff --git a/patches/minecraft/net/minecraft/tileentity/TileEntityLockableLoot.java.patch b/patches/minecraft/net/minecraft/tileentity/TileEntityLockableLoot.java.patch new file mode 100644 index 00000000000..3ec94d9c69c --- /dev/null +++ b/patches/minecraft/net/minecraft/tileentity/TileEntityLockableLoot.java.patch @@ -0,0 +1,11 @@ +--- ../src-base/minecraft/net/minecraft/tileentity/TileEntityLockableLoot.java ++++ ../src-work/minecraft/net/minecraft/tileentity/TileEntityLockableLoot.java +@@ -73,7 +73,7 @@ + + if (p_184281_1_ != null) + { +- lootcontext$builder.func_186469_a(p_184281_1_.func_184817_da()); ++ lootcontext$builder.func_186469_a(p_184281_1_.func_184817_da()).func_186470_a(p_184281_1_); // Forge: add player to LootContext + } + + loottable.func_186460_a(this, random, lootcontext$builder.func_186471_a()); From 1b28e226c5bbb849a2aec7b8e85aabd36fee953c Mon Sep 17 00:00:00 2001 From: Leviathan143 Date: Wed, 4 Oct 2017 16:20:17 +1300 Subject: [PATCH 2/5] Add test mod --- .../debug/LootContextTweaksTest.java | 105 ++++++++++++++++++ .../advancements/test/root.json | 23 ++++ 2 files changed, 128 insertions(+) create mode 100644 src/test/java/net/minecraftforge/debug/LootContextTweaksTest.java create mode 100644 src/test/resources/assets/loot_context_tweaks_test/advancements/test/root.json diff --git a/src/test/java/net/minecraftforge/debug/LootContextTweaksTest.java b/src/test/java/net/minecraftforge/debug/LootContextTweaksTest.java new file mode 100644 index 00000000000..c2a26bbe9fa --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/LootContextTweaksTest.java @@ -0,0 +1,105 @@ +package net.minecraftforge.debug; + +import java.util.Random; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonObject; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSyntaxException; + +import net.minecraft.init.Biomes; +import net.minecraft.init.Items; +import net.minecraft.util.ResourceLocation; +import net.minecraft.world.biome.Biome; +import net.minecraft.world.storage.loot.LootContext; +import net.minecraft.world.storage.loot.LootEntryItem; +import net.minecraft.world.storage.loot.LootPool; +import net.minecraft.world.storage.loot.LootTableList; +import net.minecraft.world.storage.loot.RandomValueRange; +import net.minecraft.world.storage.loot.conditions.EntityHasProperty; +import net.minecraft.world.storage.loot.conditions.LootCondition; +import net.minecraft.world.storage.loot.conditions.LootConditionManager; +import net.minecraft.world.storage.loot.functions.LootFunction; +import net.minecraft.world.storage.loot.functions.SetCount; +import net.minecraft.world.storage.loot.properties.EntityOnFire; +import net.minecraft.world.storage.loot.properties.EntityProperty; +import net.minecraftforge.event.LootTableLoadEvent; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.Mod.EventBusSubscriber; +import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.registry.ForgeRegistries; + +@EventBusSubscriber +@Mod(modid = LootContextTweaksTest.MODID, name = "LootContextTweaksTest", version = "1.0", acceptableRemoteVersions = "*") +public class LootContextTweaksTest +{ + public static final String MODID = "loot_context_tweaks_test"; + public static final boolean ENABLED = false; + + @Mod.EventHandler + public void preInit(FMLPreInitializationEvent event) + { + if(!ENABLED) return; + LootConditionManager.registerCondition(new InBiome.Serialiser()); + } + + @SubscribeEvent + public static void onLootTableLoad(LootTableLoadEvent event) + { + if(!ENABLED) return; + if(event.getName().equals(LootTableList.GAMEPLAY_FISHING)) + { + LootPool main = event.getTable().getPool("main"); + main.addEntry(new LootEntryItem(Items.ACACIA_BOAT, 100, 1, new LootFunction[0], new LootCondition[] {new InBiome(Biomes.SAVANNA)}, "fishing_test")); + } + else if(event.getName().equals(LootTableList.CHESTS_SIMPLE_DUNGEON)) + { + LootPool main = event.getTable().getPool("main"); + LootCondition onFire = new EntityHasProperty(new EntityProperty[] {new EntityOnFire(true)}, LootContext.EntityTarget.KILLER_PLAYER); + main.addEntry(new LootEntryItem(Items.BLAZE_POWDER, 100, 1, new LootFunction[] {new SetCount(new LootCondition[0], new RandomValueRange(64))}, new LootCondition[] {onFire}, "minecart_test")); + } + } + + private static class InBiome implements LootCondition + { + private final Biome requiredBiome; + + public InBiome(Biome requiredBiome) + { + this.requiredBiome = requiredBiome; + } + + @Override + public boolean testCondition(Random rand, LootContext context) + { + if(context.getLootedEntity() == null) return false; + Biome biome = context.getWorld().getBiome(context.getLootedEntity().getPosition()); + return biome == requiredBiome; + } + + private static class Serialiser extends LootCondition.Serializer + { + protected Serialiser() + { + super(new ResourceLocation(MODID, "in_biome"), InBiome.class); + } + + @Override + public void serialize(JsonObject json, InBiome value, JsonSerializationContext context) + { + json.addProperty("biome", value.requiredBiome.getRegistryName().toString()); + } + + @Override + public InBiome deserialize(JsonObject json, JsonDeserializationContext context) + { + if(!json.has("biome")) throw new JsonSyntaxException("Missing biome tag, expected to find a biome registry name"); + ResourceLocation biomeResLoc = new ResourceLocation(json.get("biome").getAsString()); + Biome biome = ForgeRegistries.BIOMES.getValue(biomeResLoc); + if(biome == null) throw new JsonSyntaxException("Invalid biome tag. " + biomeResLoc + " does not exist in the biome registry."); + return new InBiome(biome); + } + } + } +} diff --git a/src/test/resources/assets/loot_context_tweaks_test/advancements/test/root.json b/src/test/resources/assets/loot_context_tweaks_test/advancements/test/root.json new file mode 100644 index 00000000000..f2998664aaa --- /dev/null +++ b/src/test/resources/assets/loot_context_tweaks_test/advancements/test/root.json @@ -0,0 +1,23 @@ +{ + "display": + { + "icon": { + "item": "minecraft:stone" + }, + "description": "From LootContextTweaksTest", + "title": "Test", + "background": "minecraft:textures/blocks/stone.png" + }, + "criteria": + { + "no": + { + "trigger": "minecraft:impossible" + } + }, + "rewards": + { + "loot": ["minecraft:chests/simple_dungeon"] + }, + "requirements": [["no"]] +} \ No newline at end of file From 630c9df499c9c38a2105ccaaa5c90953b2ebfc42 Mon Sep 17 00:00:00 2001 From: Leviathan143 Date: Wed, 4 Oct 2017 16:23:59 +1300 Subject: [PATCH 3/5] Fix some small style issues --- .../debug/LootContextTweaksTest.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/test/java/net/minecraftforge/debug/LootContextTweaksTest.java b/src/test/java/net/minecraftforge/debug/LootContextTweaksTest.java index c2a26bbe9fa..0dfc854b51c 100644 --- a/src/test/java/net/minecraftforge/debug/LootContextTweaksTest.java +++ b/src/test/java/net/minecraftforge/debug/LootContextTweaksTest.java @@ -40,20 +40,20 @@ public class LootContextTweaksTest @Mod.EventHandler public void preInit(FMLPreInitializationEvent event) { - if(!ENABLED) return; + if (!ENABLED) return; LootConditionManager.registerCondition(new InBiome.Serialiser()); } @SubscribeEvent public static void onLootTableLoad(LootTableLoadEvent event) { - if(!ENABLED) return; - if(event.getName().equals(LootTableList.GAMEPLAY_FISHING)) + if (!ENABLED) return; + if (event.getName().equals(LootTableList.GAMEPLAY_FISHING)) { LootPool main = event.getTable().getPool("main"); main.addEntry(new LootEntryItem(Items.ACACIA_BOAT, 100, 1, new LootFunction[0], new LootCondition[] {new InBiome(Biomes.SAVANNA)}, "fishing_test")); } - else if(event.getName().equals(LootTableList.CHESTS_SIMPLE_DUNGEON)) + else if (event.getName().equals(LootTableList.CHESTS_SIMPLE_DUNGEON)) { LootPool main = event.getTable().getPool("main"); LootCondition onFire = new EntityHasProperty(new EntityProperty[] {new EntityOnFire(true)}, LootContext.EntityTarget.KILLER_PLAYER); @@ -73,7 +73,7 @@ public InBiome(Biome requiredBiome) @Override public boolean testCondition(Random rand, LootContext context) { - if(context.getLootedEntity() == null) return false; + if (context.getLootedEntity() == null) return false; Biome biome = context.getWorld().getBiome(context.getLootedEntity().getPosition()); return biome == requiredBiome; } @@ -94,10 +94,10 @@ public void serialize(JsonObject json, InBiome value, JsonSerializationContext c @Override public InBiome deserialize(JsonObject json, JsonDeserializationContext context) { - if(!json.has("biome")) throw new JsonSyntaxException("Missing biome tag, expected to find a biome registry name"); + if (!json.has("biome")) throw new JsonSyntaxException("Missing biome tag, expected to find a biome registry name"); ResourceLocation biomeResLoc = new ResourceLocation(json.get("biome").getAsString()); Biome biome = ForgeRegistries.BIOMES.getValue(biomeResLoc); - if(biome == null) throw new JsonSyntaxException("Invalid biome tag. " + biomeResLoc + " does not exist in the biome registry."); + if (biome == null) throw new JsonSyntaxException("Invalid biome tag. " + biomeResLoc + " does not exist in the biome registry."); return new InBiome(biome); } } From 0dd462634a4370e5cbe3bd90d3574ccfb79323dc Mon Sep 17 00:00:00 2001 From: Leviathan143 Date: Wed, 4 Oct 2017 16:35:39 +1300 Subject: [PATCH 4/5] Remove unnecessary patch line --- .../minecraft/entity/item/EntityMinecartContainer.java.patch | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/patches/minecraft/net/minecraft/entity/item/EntityMinecartContainer.java.patch b/patches/minecraft/net/minecraft/entity/item/EntityMinecartContainer.java.patch index 7d385d55d0c..26501188263 100644 --- a/patches/minecraft/net/minecraft/entity/item/EntityMinecartContainer.java.patch +++ b/patches/minecraft/net/minecraft/entity/item/EntityMinecartContainer.java.patch @@ -13,9 +13,8 @@ } - LootContext.Builder lootcontext$builder = new LootContext.Builder((WorldServer)this.field_70170_p); -- + LootContext.Builder lootcontext$builder = new LootContext.Builder((WorldServer)this.field_70170_p).func_186472_a(this); // Forge: add looted entity to LootContext -+ + if (p_184288_1_ != null) { - lootcontext$builder.func_186469_a(p_184288_1_.func_184817_da()); From 92fc5f946206fb6b894d1b6e1bf2528777a7e022 Mon Sep 17 00:00:00 2001 From: Leviathan143 Date: Sat, 11 Nov 2017 09:06:50 +1300 Subject: [PATCH 5/5] Remove unnecessary requirements tag --- .../loot_context_tweaks_test/advancements/test/root.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/resources/assets/loot_context_tweaks_test/advancements/test/root.json b/src/test/resources/assets/loot_context_tweaks_test/advancements/test/root.json index f2998664aaa..029261093b4 100644 --- a/src/test/resources/assets/loot_context_tweaks_test/advancements/test/root.json +++ b/src/test/resources/assets/loot_context_tweaks_test/advancements/test/root.json @@ -18,6 +18,5 @@ "rewards": { "loot": ["minecraft:chests/simple_dungeon"] - }, - "requirements": [["no"]] + } } \ No newline at end of file