Skip to content

Commit

Permalink
[1.21] Port Loot module. (#123)
Browse files Browse the repository at this point in the history
Co-authored-by: IThundxr <[email protected]>
  • Loading branch information
MerchantPug and IThundxr authored Aug 24, 2024
1 parent 4afbda8 commit c4c0cd8
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 15 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod_version = 3.1.0-beta
minecraft_version = 1.21
minecraft_dependency = 1.21
loader_version = 0.15.11
fabric_version = 0.100.2+1.21
fabric_version = 0.100.7+1.21

# Mixin Extra's
# https://github.com/LlamaLad7/MixinExtras
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* Implementation that defines what a global loot modifier must implement in order to be functional.
* {@link LootModifier} Supplies base functionality; most modders should only need to extend that.<br/>
* Requires a {@link Codec} to be registered: {@link PortingLibLoot#GLOBAL_LOOT_MODIFIER_SERIALIZERS}, and returned in {@link #codec()}
* Requires a {@link MapCodec} to be registered: {@link PortingLibLoot#GLOBAL_LOOT_MODIFIER_SERIALIZERS}, and returned in {@link #codec()}
* Individual instances of modifiers must be registered via json, see neoforge:loot_modifiers/global_loot_modifiers
*/
public interface IGlobalLootModifier {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,45 @@
import java.util.Map;
import java.util.function.Function;

import com.mojang.serialization.JsonOps;

import io.github.fabricators_of_create.porting_lib.conditions.ConditionalOps;
import io.github.fabricators_of_create.porting_lib.conditions.ICondition;
import io.github.fabricators_of_create.porting_lib.core.PortingLib;
import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.RegistryOps;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener;
import net.minecraft.util.GsonHelper;
import net.minecraft.util.ParticleUtils;
import net.minecraft.util.profiling.ProfilerFiller;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.ApiStatus;

public class LootModifierManager extends SimpleJsonResourceReloadListener {
public class LootModifierManager extends SimpleJsonResourceReloadListener implements IdentifiableResourceReloadListener {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
public static final Logger LOGGER = LogManager.getLogger();

public static final LootModifierManager INSTANCE = new LootModifierManager();

private HolderLookup.Provider registries;
private Map<ResourceLocation, IGlobalLootModifier> registeredLootModifiers = ImmutableMap.of();
private static final String folder = "loot_modifiers";
public static final ResourceLocation ID = PortingLib.id(folder);;

public LootModifierManager() {
super(GSON, folder);
}

public void injectContext(HolderLookup.Provider registries) {
this.registries = registries;
}

@Override
protected Map<ResourceLocation, JsonElement> prepare(ResourceManager resourceManager, ProfilerFiller profilerFiller) {
Map<ResourceLocation, JsonElement> map = super.prepare(resourceManager, profilerFiller);
Expand Down Expand Up @@ -70,7 +88,7 @@ protected Map<ResourceLocation, JsonElement> prepare(ResourceManager resourceMan

@Override
protected void apply(Map<ResourceLocation, JsonElement> resourceList, ResourceManager resourceManagerIn, ProfilerFiller profilerIn) {
DynamicOps<JsonElement> ops = this.makeConditionalOps();
DynamicOps<JsonElement> ops = new ConditionalOps<>(RegistryOps.create(JsonOps.INSTANCE, registries), ICondition.IContext.EMPTY);
Builder<ResourceLocation, IGlobalLootModifier> builder = ImmutableMap.builder();
for (Map.Entry<ResourceLocation, JsonElement> entry : resourceList.entrySet()) {
ResourceLocation location = entry.getKey();
Expand All @@ -91,4 +109,9 @@ protected void apply(Map<ResourceLocation, JsonElement> resourceList, ResourceMa
public Collection<IGlobalLootModifier> getAllLootMods() {
return registeredLootModifiers.values();
}

@Override
public ResourceLocation getFabricId() {
return ID;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;

import com.mojang.serialization.MapCodec;

Expand All @@ -12,7 +12,7 @@
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.loot.v2.LootTableEvents;
import net.fabricmc.fabric.api.loot.v3.LootTableEvents;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
Expand All @@ -29,12 +29,15 @@ public class PortingLibLoot implements ModInitializer {

@Override
public void onInitialize() {
ResourceManagerHelper.get(PackType.SERVER_DATA).registerReloadListener(LootModifierManager.INSTANCE);
ResourceManagerHelper.get(PackType.SERVER_DATA).registerReloadListener(LootModifierManager.ID, lookup -> {
LootModifierManager.INSTANCE.injectContext(lookup);
return LootModifierManager.INSTANCE;
});
Registry.register(BuiltInRegistries.LOOT_CONDITION_TYPE, PortingLib.id("loot_table_id"), LootTableIdCondition.LOOT_TABLE_ID);

LootTableEvents.MODIFY.addPhaseOrdering(Event.DEFAULT_PHASE, LAST);
LootTableEvents.MODIFY.register(LAST,
(key, builder, source) -> ((LootTableBuilderExtensions) builder).port_lib$setId(key.location())
(key, builder, source, provider) -> ((LootTableBuilderExtensions) builder).port_lib$setId(key.location())
);
}

Expand Down Expand Up @@ -72,7 +75,7 @@ public static List<ItemStack> modifyLoot(List<ItemStack> list, LootContext conte
*/
public static ObjectArrayList<ItemStack> modifyLoot(ResourceLocation lootTableId, ObjectArrayList<ItemStack> generatedLoot, LootContext context) {
context.setQueriedLootTableId(lootTableId); // In case the ID was set via copy constructor, this will be ignored: intended
LootModifierManager man = LootModifierManager.getLootModifierManager();
LootModifierManager man = LootModifierManager.INSTANCE;
for (IGlobalLootModifier mod : man.getAllLootMods()) {
generatedLoot = mod.apply(generatedLoot, context);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.github.fabricators_of_create.porting_lib.loot.mixin;

import com.google.gson.JsonElement;

import com.llamalad7.mixinextras.injector.ModifyReturnValue;

import com.mojang.serialization.DynamicOps;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.storage.loot.LootDataType;

Expand All @@ -17,7 +17,7 @@
@Mixin(LootDataType.class)
public class LootDataTypeMixin {
@ModifyReturnValue(method = "deserialize", at = @At("RETURN"))
private static <T> Optional<T> setLootTableId(Optional<T> optional, ResourceLocation id, JsonElement json) {
private <V, T> Optional<T> setLootTableId(Optional<T> optional, ResourceLocation id, DynamicOps<V> ops, V object) {
if (optional.isPresent() && optional.get() instanceof LootTable lootTable) {
lootTable.setLootTableId(id);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.fabricators_of_create.porting_lib.loot.mixin.loottable;
package io.github.fabricators_of_create.porting_lib.loot.mixin;

import java.util.Objects;
import java.util.Stack;
Expand Down
4 changes: 2 additions & 2 deletions modules/loot/src/main/resources/porting_lib_loot.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"LootDataTypeMixin",
"LootPoolMixin",
"LootPoolMixin$LootPoolBuilderMixin",
"loottable.LootTableMixin",
"loottable.LootTableMixin$BuilderMixin",
"LootTableMixin",
"LootTableMixin$BuilderMixin",
"loottable.LootTableMixin_Early",
"loottable.LootTableMixin_Late"
]
Expand Down

0 comments on commit c4c0cd8

Please sign in to comment.