Enable ForgeLootTableProvider.

This commit is contained in:
LexManos committed 2023-09-20 17:46:22 -07:00
1 parent af6e848227
commit be60ddbfca
6 files changed
+69 -46

No files matched your search

@@ -1,7 +1,7 @@
{
"type": "minecraft:crafting_shaped",
"category": "misc",
"group": "wooden_custom_fence",
"group": "wooden_fence",
"key": {
"#": {
"tag": "forge:rods/wooden"
@@ -1,7 +1,7 @@
{
"type": "minecraft:crafting_shaped",
"category": "redstone",
"group": "wooden_custom_fence_gate",
"group": "wooden_fence_gate",
"key": {
"#": {
"tag": "forge:rods/wooden"
+1 -1
View File
@@ -3,6 +3,6 @@
"description": {
"translate": "pack.forge.description"
},
"pack_format": 15
"pack_format": 18
}
}
@@ -47,6 +47,7 @@ import net.minecraftforge.client.extensions.common.IClientFluidTypeExtensions;
import net.minecraftforge.common.data.ExistingFileHelper;
import net.minecraftforge.common.data.ForgeBiomeTagsProvider;
import net.minecraftforge.common.data.ForgeFluidTagsProvider;
import net.minecraftforge.common.data.ForgeLootTableProvider;
import net.minecraftforge.common.data.ForgeSpriteSourceProvider;
import net.minecraftforge.common.data.VanillaSoundDefinitionsProvider;
import net.minecraftforge.common.extensions.IForgeEntity;
@@ -504,7 +505,7 @@ public class ForgeMod {
gen.addProvider(event.includeServer(), new ForgeEntityTypeTagsProvider(packOutput, lookupProvider, existingFileHelper));
gen.addProvider(event.includeServer(), new ForgeFluidTagsProvider(packOutput, lookupProvider, existingFileHelper));
gen.addProvider(event.includeServer(), new ForgeRecipeProvider(packOutput));
//gen.addProvider(event.includeServer(), new ForgeLootTableProvider(packOutput));
gen.addProvider(event.includeServer(), new ForgeLootTableProvider(packOutput));
gen.addProvider(event.includeServer(), new ForgeBiomeTagsProvider(packOutput, lookupProvider, existingFileHelper));
gen.addProvider(event.includeClient(), new ForgeSpriteSourceProvider(packOutput, existingFileHelper));
@@ -23,6 +23,8 @@ import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import org.jetbrains.annotations.ApiStatus;
public interface ICondition {
Codec<ICondition> CODEC = ExtraCodecs.lazyInitializedCodec(() -> ForgeRegistries.CONDITION_SERIALIZERS.get().getCodec().dispatch(ICondition::codec, Function.identity()));
String DEFAULT_FIELD = "forge:condition";
@@ -33,6 +35,8 @@ public interface ICondition {
Codec<? extends ICondition> codec();
interface IContext {
/* Key used to attach this context option to a DynamicOps instance */
@ApiStatus.Internal
public static ResourceLocation KEY = new ResourceLocation("forge", "condition_context");
IContext EMPTY = new IContext() {
@@ -4,7 +4,7 @@
*/
package net.minecraftforge.common.data;
/*
import net.minecraft.advancements.critereon.ItemPredicate;
import net.minecraft.data.PackOutput;
import net.minecraft.data.loot.LootTableProvider;
@@ -22,11 +22,14 @@ import net.minecraftforge.common.ToolActions;
import net.minecraftforge.common.loot.CanToolPerformAction;
import net.minecraftforge.fml.util.ObfuscationReflectionHelper;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import com.google.common.collect.ImmutableList;
import net.minecraft.world.level.storage.loot.LootPool;
import net.minecraft.world.level.storage.loot.LootTable;
import net.minecraft.world.level.storage.loot.ValidationContext;
@@ -35,8 +38,15 @@ import net.minecraft.world.level.storage.loot.entries.LootPoolEntryContainer;
/**
* Currently used only for replacing shears item to shears_dig tool action
* /
*/
public final class ForgeLootTableProvider extends LootTableProvider {
private static final String POOLS = "f_7915" + "6_"; // LootTable.Builder.pools
private static final String ENTRIES = "f_7902" + "3_"; // LootPool.entries
private static final String CONDITIONS = "f_7902" + "4_"; // LootPool.conditions
private static final String CHILDREN = "f_7942" + "8_"; // CompositeEntryBase.children
private static final String ENTRY_CONDITION = "f_7963" + "6_"; // LootPoolEntryContainer.conditions
private static final String TERMS = "f_28560" + "9_"; // CompositeLootItemCondition.terms
public ForgeLootTableProvider(PackOutput packOutput) {
super(packOutput, Set.of(), VanillaLootTableProvider.create(packOutput).getTables());
}
@@ -63,14 +73,14 @@ public final class ForgeLootTableProvider extends LootTableProvider {
}
private boolean findAndReplaceInLootTableBuilder(LootTable.Builder builder, Item from, ToolAction toolAction) {
List<LootPool> lootPools = ObfuscationReflectionHelper.getPrivateValue(LootTable.Builder.class, builder, "f_7915" + "6_");
ImmutableList.Builder<LootPool> lootPools = ObfuscationReflectionHelper.getPrivateValue(LootTable.Builder.class, builder, POOLS);
boolean found = false;
if (lootPools == null) {
throw new IllegalStateException(LootTable.Builder.class.getName() + " is missing field f_7915" + "6_");
throw new IllegalStateException(LootTable.Builder.class.getName() + " is missing field " + POOLS);
}
for (LootPool lootPool : lootPools) {
for (LootPool lootPool : lootPools.build()) {
if (findAndReplaceInLootPool(lootPool, from, toolAction)) {
found = true;
}
@@ -80,39 +90,40 @@ public final class ForgeLootTableProvider extends LootTableProvider {
}
private boolean findAndReplaceInLootPool(LootPool lootPool, Item from, ToolAction toolAction) {
LootPoolEntryContainer[] lootEntries = ObfuscationReflectionHelper.getPrivateValue(LootPool.class, lootPool, "f_7902" +"3_");
LootItemCondition[] lootConditions = ObfuscationReflectionHelper.getPrivateValue(LootPool.class, lootPool, "f_7902" + "4_");
List<LootPoolEntryContainer> lootEntries = ObfuscationReflectionHelper.getPrivateValue(LootPool.class, lootPool, ENTRIES);
List<LootItemCondition> lootConditions = ObfuscationReflectionHelper.getPrivateValue(LootPool.class, lootPool, CONDITIONS);
boolean found = false;
if (lootEntries == null) {
throw new IllegalStateException(LootPool.class.getName() + " is missing field f_7902" + "3_");
}
if (lootEntries == null)
throw new IllegalStateException(LootPool.class.getName() + " is missing field " + ENTRIES);
for (LootPoolEntryContainer lootEntry : lootEntries) {
if (findAndReplaceInLootEntry(lootEntry, from, toolAction)) {
if (findAndReplaceInLootEntry(lootEntry, from, toolAction))
found = true;
}
if (lootEntry instanceof CompositeEntryBase) {
if (findAndReplaceInParentedLootEntry((CompositeEntryBase) lootEntry, from, toolAction)) {
if (findAndReplaceInParentedLootEntry((CompositeEntryBase) lootEntry, from, toolAction))
found = true;
}
}
}
if (lootConditions == null) {
throw new IllegalStateException(LootPool.class.getName() + " is missing field f_7902" + "4_");
if (lootConditions == null)
throw new IllegalStateException(LootPool.class.getName() + " is missing field " + CONDITIONS);
else {
lootConditions = new ArrayList<>(lootConditions);
ObfuscationReflectionHelper.setPrivateValue(LootPool.class, lootPool, lootConditions, CONDITIONS);
}
for (int i = 0; i < lootConditions.length; i++) {
LootItemCondition lootCondition = lootConditions[i];
if (lootCondition instanceof MatchTool && checkMatchTool((MatchTool) lootCondition, from)) {
lootConditions[i] = CanToolPerformAction.canToolPerformAction(toolAction).build();
for (int i = 0; i < lootConditions.size(); i++) {
LootItemCondition lootCondition = lootConditions.get(i);
if (lootCondition instanceof MatchTool matchTool && checkMatchTool(matchTool, from)) {
lootConditions.set(i, CanToolPerformAction.canToolPerformAction(toolAction).build());
found = true;
} else if (lootCondition instanceof InvertedLootItemCondition) {
LootItemCondition invLootCondition = ObfuscationReflectionHelper.getPrivateValue(InvertedLootItemCondition.class, (InvertedLootItemCondition) lootCondition, "f_8168" + "1_");
} else if (lootCondition instanceof InvertedLootItemCondition inverted) {
LootItemCondition invLootCondition = inverted.term();
if (invLootCondition instanceof MatchTool && checkMatchTool((MatchTool) invLootCondition, from)) {
lootConditions[i] = InvertedLootItemCondition.invert(CanToolPerformAction.canToolPerformAction(toolAction)).build();
if (invLootCondition instanceof MatchTool matchTool && checkMatchTool(matchTool, from)) {
lootConditions.set(i, InvertedLootItemCondition.invert(CanToolPerformAction.canToolPerformAction(toolAction)).build());
found = true;
} else if (invLootCondition instanceof CompositeLootItemCondition compositeLootItemCondition && findAndReplaceInComposite(compositeLootItemCondition, from, toolAction)) {
found = true;
@@ -120,15 +131,17 @@ public final class ForgeLootTableProvider extends LootTableProvider {
}
}
return found;
}
private boolean findAndReplaceInParentedLootEntry(CompositeEntryBase entry, Item from, ToolAction toolAction) {
LootPoolEntryContainer[] lootEntries = ObfuscationReflectionHelper.getPrivateValue(CompositeEntryBase.class, entry, "f_7942" + "8_");
List<LootPoolEntryContainer> lootEntries = ObfuscationReflectionHelper.getPrivateValue(CompositeEntryBase.class, entry, CHILDREN);
boolean found = false;
if (lootEntries == null) {
throw new IllegalStateException(CompositeEntryBase.class.getName() + " is missing field f_7942" + "8_");
throw new IllegalStateException(CompositeEntryBase.class.getName() + " is missing field " + CHILDREN);
}
for (LootPoolEntryContainer lootEntry : lootEntries) {
@@ -141,18 +154,22 @@ public final class ForgeLootTableProvider extends LootTableProvider {
}
private boolean findAndReplaceInLootEntry(LootPoolEntryContainer entry, Item from, ToolAction toolAction) {
LootItemCondition[] lootConditions = ObfuscationReflectionHelper.getPrivateValue(LootPoolEntryContainer.class, entry, "f_7963" + "6_");
List<LootItemCondition> lootConditions = ObfuscationReflectionHelper.getPrivateValue(LootPoolEntryContainer.class, entry, ENTRY_CONDITION);
boolean found = false;
if (lootConditions == null) {
if (lootConditions == null)
throw new IllegalStateException(LootPoolEntryContainer.class.getName() + " is missing field f_7963" + "6_");
else {
lootConditions = new ArrayList<>(lootConditions);
ObfuscationReflectionHelper.setPrivateValue(LootPoolEntryContainer.class, entry, lootConditions, ENTRY_CONDITION);
}
for (int i = 0; i < lootConditions.length; i++) {
if (lootConditions[i] instanceof CompositeLootItemCondition composite && findAndReplaceInComposite(composite, from, toolAction)) {
for (int i = 0; i < lootConditions.size(); i++) {
var condition = lootConditions.get(i);
if (condition instanceof CompositeLootItemCondition composite && findAndReplaceInComposite(composite, from, toolAction)) {
found = true;
} else if (lootConditions[i] instanceof MatchTool && checkMatchTool((MatchTool) lootConditions[i], from)) {
lootConditions[i] = CanToolPerformAction.canToolPerformAction(toolAction).build();
} else if (condition instanceof MatchTool matchTool && checkMatchTool(matchTool, from)) {
lootConditions.set(i, CanToolPerformAction.canToolPerformAction(toolAction).build());
found = true;
}
}
@@ -161,16 +178,19 @@ public final class ForgeLootTableProvider extends LootTableProvider {
}
private boolean findAndReplaceInComposite(CompositeLootItemCondition alternative, Item from, ToolAction toolAction) {
LootItemCondition[] lootConditions = ObfuscationReflectionHelper.getPrivateValue(CompositeLootItemCondition.class, alternative, "f_28560" + "9_");
List<LootItemCondition> lootConditions = ObfuscationReflectionHelper.getPrivateValue(CompositeLootItemCondition.class, alternative, TERMS);
boolean found = false;
if (lootConditions == null) {
throw new IllegalStateException(CompositeLootItemCondition.class.getName() + " is missing field f_28560" + "9_");
if (lootConditions == null)
throw new IllegalStateException(CompositeLootItemCondition.class.getName() + " is missing field " + TERMS);
else {
lootConditions = new ArrayList<>(lootConditions);
ObfuscationReflectionHelper.setPrivateValue(CompositeLootItemCondition.class, alternative, lootConditions, TERMS);
}
for (int i = 0; i < lootConditions.length; i++) {
if (lootConditions[i] instanceof MatchTool && checkMatchTool((MatchTool) lootConditions[i], from)) {
lootConditions[i] = CanToolPerformAction.canToolPerformAction(toolAction).build();
for (int i = 0; i < lootConditions.size(); i++) {
if (lootConditions.get(i) instanceof MatchTool matchTool && checkMatchTool(matchTool, from)) {
lootConditions.set(i, CanToolPerformAction.canToolPerformAction(toolAction).build());
found = true;
}
}
@@ -178,10 +198,8 @@ public final class ForgeLootTableProvider extends LootTableProvider {
return found;
}
@SuppressWarnings("deprecation")
private boolean checkMatchTool(MatchTool lootCondition, Item expected) {
ItemPredicate predicate = ObfuscationReflectionHelper.getPrivateValue(MatchTool.class, lootCondition, "f_8199" + "3_");
Set<Item> items = ObfuscationReflectionHelper.getPrivateValue(ItemPredicate.class, predicate, "f_15142" + "7_");
return items != null && items.contains(expected);
return lootCondition.predicate().flatMap(ItemPredicate::items).filter(s -> s.contains(expected.builtInRegistryHolder())).isPresent();
}
}
*/