diff --git a/src/main/java/de/foxgalaxy/villa/VillaCommand.java b/src/main/java/de/foxgalaxy/villa/VillaCommand.java new file mode 100644 index 0000000..dd4a11a --- /dev/null +++ b/src/main/java/de/foxgalaxy/villa/VillaCommand.java @@ -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 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; + }) + ) + ) + ) + ); + } +} diff --git a/src/main/java/de/foxgalaxy/villa/VillaMod.java b/src/main/java/de/foxgalaxy/villa/VillaMod.java index a157a26..c53e7e9 100644 --- a/src/main/java/de/foxgalaxy/villa/VillaMod.java +++ b/src/main/java/de/foxgalaxy/villa/VillaMod.java @@ -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 EMERALD_GENERATOR_COUNT = AttachmentRegistry.createPersistent(Identifier.fromNamespaceAndPath(MOD_ID, "emerald_generator_count"), Codec.intRange(0, Integer.MAX_VALUE)); + public static final AttachmentType 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 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(); + } + } + } + } } \ No newline at end of file