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
@@ -0,0 +1,46 @@
package de.foxgalaxy.villa;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
import net.minecraft.network.chat.Component;
import net.minecraft.server.permissions.Permissions;
import static net.minecraft.commands.Commands.*;
public class VillaCommand {
public static void registerCommand(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext buildContext, Commands.CommandSelection selection) {
dispatcher.register(literal("villa")
.then(literal("emeraldgen")
.requires(source -> source.permissions().hasPermission(Permissions.COMMANDS_GAMEMASTER))
.then(literal("amount")
.executes(context -> {
context.getSource().sendSuccess(() -> Component.literal(String.valueOf(context.getSource().getServer().globalAttachments().getAttached(VillaMod.EMERALD_GENERATOR_COUNT))), false);
return 0;
})
.then(argument("amount", IntegerArgumentType.integer(0))
.executes(context -> {
context.getSource().getServer().globalAttachments().setAttached(VillaMod.EMERALD_GENERATOR_COUNT, IntegerArgumentType.getInteger(context, "amount"));
return 0;
})
)
)
.then(literal("pos")
.executes(context -> {
context.getSource().sendSuccess(() -> Component.literal(String.valueOf(context.getSource().getServer().globalAttachments().getAttached(VillaMod.EMERALD_GENERATOR_POS))), false);
return 0;
})
.then(argument("pos", BlockPosArgument.blockPos())
.executes(context -> {
context.getSource().getServer().globalAttachments().setAttached(VillaMod.EMERALD_GENERATOR_POS, BlockPosArgument.getBlockPos(context, "pos"));
return 0;
})
)
)
)
);
}
}
@@ -5,11 +5,21 @@ import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.attachment.v1.AttachmentRegistry; import net.fabricmc.fabric.api.attachment.v1.AttachmentRegistry;
import net.fabricmc.fabric.api.attachment.v1.AttachmentType; 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.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.core.registries.Registries;
import net.minecraft.resources.Identifier; import net.minecraft.resources.Identifier;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.tags.TagKey; 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.EquipmentSlot;
import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.Item; import net.minecraft.world.item.Item;
@@ -30,14 +40,23 @@ public class VillaMod implements ModInitializer {
.copyOnDeath() .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 @Override
public void onInitialize() { public void onInitialize() {
CommandRegistrationCallback.EVENT.register(VillaCommand::registerCommand);
ServerPlayerEvents.JOIN.register((player) -> { ServerPlayerEvents.JOIN.register((player) -> {
if(!player.getAttachedOrCreate(HAS_JOINED)) { if(!player.getAttachedOrCreate(HAS_JOINED)) {
player.setAttached(HAS_JOINED, true); player.setAttached(HAS_JOINED, true);
giveEquipment(player); 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) { 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.LEGS, new ItemStack(Items.IRON_LEGGINGS));
player.setItemSlot(EquipmentSlot.FEET, new ItemStack(Items.IRON_BOOTS)); 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();
}
}
}
}
} }