() Loot entry extensions

This commit is contained in:
Juuxel 2019-01-25 23:53:11 +01:00 committed by asie
parent 2c3e78a357
commit b88969cf13
7 changed files with 228 additions and 1 deletions
src
main
test
java/net/fabricmc/fabric/loot
resources
data/minecraft/loot_tables/blocks
mod.json

View file

@ -0,0 +1,37 @@
/*
* Copyright (c) 2016, 2017, 2018 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.api.loot;
import net.fabricmc.fabric.impl.loot.LootEntryRegistryImpl;
import net.minecraft.world.loot.entry.LootEntry;
/**
* Fabric's extensions to {@code net.minecraft.world.loot.entry.LootEntries} for registering
* custom loot entry types.
*
* @see #registerType
*/
public interface LootEntryRegistry {
LootEntryRegistry INSTANCE = LootEntryRegistryImpl.INSTANCE;
/**
* Registers a loot entry type by its serializer.
*
* @param serializer the loot entry serializer
*/
void registerType(LootEntry.Serializer<?> serializer);
}

View file

@ -0,0 +1,47 @@
/*
* Copyright (c) 2016, 2017, 2018 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.impl.loot;
import net.fabricmc.fabric.api.loot.LootEntryRegistry;
import net.minecraft.world.loot.entry.LootEntries;
import net.minecraft.world.loot.entry.LootEntry;
import java.util.function.Consumer;
public final class LootEntryRegistryImpl implements LootEntryRegistry {
private static Consumer<LootEntry.Serializer<?>> registerFunction;
public static final LootEntryRegistryImpl INSTANCE = new LootEntryRegistryImpl();
static {
loadLootEntries();
}
private LootEntryRegistryImpl() {}
@Override
public void registerType(LootEntry.Serializer<?> serializer) {
registerFunction.accept(serializer);
}
public static void setRegisterFunction(Consumer<LootEntry.Serializer<?>> registerFunction) {
LootEntryRegistryImpl.registerFunction = registerFunction;
}
private static void loadLootEntries() {
try { Class.forName(LootEntries.class.getCanonicalName()); } catch (ClassNotFoundException e) {}
}
}

View file

@ -0,0 +1,37 @@
/*
* Copyright (c) 2016, 2017, 2018 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.mixin.loot;
import net.fabricmc.fabric.impl.loot.LootEntryRegistryImpl;
import net.minecraft.world.loot.entry.LootEntries;
import net.minecraft.world.loot.entry.LootEntry;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(LootEntries.class)
public class MixinLootEntries {
@Shadow
private static void register(LootEntry.Serializer<?> lootEntry$Serializer_1) {}
@Inject(method = "<clinit>", at = @At("RETURN"))
private static void onClinit(CallbackInfo info) {
LootEntryRegistryImpl.setRegisterFunction(MixinLootEntries::register);
}
}

View file

@ -19,6 +19,7 @@
"events.tick.MixinWorld", "events.tick.MixinWorld",
"item.MixinAbstractFurnaceBlockEntity", "item.MixinAbstractFurnaceBlockEntity",
"itemgroup.MixinItemGroup", "itemgroup.MixinItemGroup",
"loot.MixinLootEntries",
"misc.MixinCrashReport", "misc.MixinCrashReport",
"networking.MixinServerPlayNetworkHandler", "networking.MixinServerPlayNetworkHandler",
"networking.MixinSPacketCustomPayload", "networking.MixinSPacketCustomPayload",

View file

@ -0,0 +1,60 @@
/*
* Copyright (c) 2016, 2017, 2018 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.loot;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.loot.LootEntryRegistry;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.world.loot.condition.LootCondition;
import net.minecraft.world.loot.entry.LootEntry;
import net.minecraft.world.loot.entry.TagEntry;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class LootEntryMod implements ModInitializer {
private static final Logger LOGGER = LogManager.getLogger();
@Override
public void onInitialize() {
LootEntryRegistry.INSTANCE.registerType(new TestSerializer());
}
private static class TestSerializer extends LootEntry.Serializer<TagEntry> {
private static final TagEntry.Serializer SERIALIZER = new TagEntry.Serializer();
public TestSerializer() {
super(new Identifier("fabric", "extended_tag"), TagEntry.class);
}
@Override
public void toJson(JsonObject obj, TagEntry entry, JsonSerializationContext context) {
SERIALIZER.method_451(obj, entry, context);
obj.addProperty("fabric", true);
}
@Override
public TagEntry fromJson(JsonObject var1, JsonDeserializationContext var2, LootCondition[] var3) {
LOGGER.info("Is this a Fabric loot entry? " + JsonHelper.getBoolean(var1, "fabric", true));
return SERIALIZER.fromJson(var1, var2, var3);
}
}
}

View file

@ -0,0 +1,44 @@
{
"type": "minecraft:block",
"pools": [
{
"rolls": 1,
"entries": [
{
"type": "minecraft:alternatives",
"children": [
{
"type": "minecraft:item",
"conditions": [
{
"condition": "minecraft:match_tool",
"predicate": {
"enchantments": [
{
"enchantment": "minecraft:silk_touch",
"levels": {
"min": 1
}
}
]
}
}
],
"name": "minecraft:stone"
},
{
"type": "fabric:extended_tag",
"conditions": [
{
"condition": "minecraft:survives_explosion"
}
],
"name": "minecraft:wool",
"expand": false
}
]
}
]
}
]
}

View file

@ -9,6 +9,7 @@
"net.fabricmc.fabric.colormapper.ColorProviderMod", "net.fabricmc.fabric.colormapper.ColorProviderMod",
"net.fabricmc.fabric.events.ServerEventMod", "net.fabricmc.fabric.events.ServerEventMod",
"net.fabricmc.fabric.containers.ContainerMod", "net.fabricmc.fabric.containers.ContainerMod",
"net.fabricmc.fabric.containers.ContainerModClient" "net.fabricmc.fabric.containers.ContainerModClient",
"net.fabricmc.fabric.loot.LootEntryMod"
] ]
} }