Emerald Generator

This commit is contained in:
skippyall
2026-05-22 17:59:16 +02:00
parent dee168b0d0
commit 2e9c8337d4
2 changed files with 80 additions and 0 deletions
@@ -5,11 +5,21 @@ import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.attachment.v1.AttachmentRegistry;
import net.fabricmc.fabric.api.attachment.v1.AttachmentType;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage;
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
import net.fabricmc.fabric.api.transfer.v1.storage.Storage;
import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.Identifier;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.tags.TagKey;
import net.minecraft.world.clock.ClockTimeMarkers;
import net.minecraft.world.clock.WorldClocks;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.Item;
@@ -30,14 +40,23 @@ public class VillaMod implements ModInitializer {
.copyOnDeath()
);
public static final AttachmentType<Integer> EMERALD_GENERATOR_COUNT = AttachmentRegistry.createPersistent(Identifier.fromNamespaceAndPath(MOD_ID, "emerald_generator_count"), Codec.intRange(0, Integer.MAX_VALUE));
public static final AttachmentType<BlockPos> EMERALD_GENERATOR_POS = AttachmentRegistry.createPersistent(Identifier.fromNamespaceAndPath(MOD_ID, "emerald_generator_pos"), BlockPos.CODEC);
@Override
public void onInitialize() {
CommandRegistrationCallback.EVENT.register(VillaCommand::registerCommand);
ServerPlayerEvents.JOIN.register((player) -> {
if(!player.getAttachedOrCreate(HAS_JOINED)) {
player.setAttached(HAS_JOINED, true);
giveEquipment(player);
}
});
ServerTickEvents.END_SERVER_TICK.register(server -> {
if(server.overworld().clockManager().isAtTimeMarker(server.registryAccess().getOrThrow(WorldClocks.OVERWORLD), ClockTimeMarkers.DAY)) {
insertEmeralds(server);
}
});
}
public static void giveEquipment(ServerPlayer player) {
@@ -54,4 +73,19 @@ public class VillaMod implements ModInitializer {
player.setItemSlot(EquipmentSlot.LEGS, new ItemStack(Items.IRON_LEGGINGS));
player.setItemSlot(EquipmentSlot.FEET, new ItemStack(Items.IRON_BOOTS));
}
public static void insertEmeralds(MinecraftServer server) {
if(server.globalAttachments().hasAttached(EMERALD_GENERATOR_POS) && server.globalAttachments().hasAttached(EMERALD_GENERATOR_COUNT)) {
BlockPos pos = server.globalAttachments().getAttached(EMERALD_GENERATOR_POS);
int count = server.globalAttachments().getAttached(EMERALD_GENERATOR_COUNT);
Storage<ItemVariant> storage = ItemStorage.SIDED.find(server.overworld(), pos, null);
if(storage != null) {
try (Transaction t = Transaction.openOuter()) {
storage.insert(ItemVariant.of(Items.EMERALD), count, t);
t.commit();
}
}
}
}
}