Files
Villa/src/main/java/de/foxgalaxy/villa/VillaMod.java
T
2026-05-27 00:10:13 +02:00

109 lines
4.5 KiB
Java

package de.foxgalaxy.villa;
import com.mojang.serialization.Codec;
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.util.Util;
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;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.TimeUnit;
public class VillaMod implements ModInitializer {
public static final String MOD_ID = "villa";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
public static final TagKey<Item> SOULBOUND = TagKey.create(Registries.ITEM, Identifier.fromNamespaceAndPath(MOD_ID, "soulbound"));
public static final AttachmentType<Boolean> HAS_JOINED = AttachmentRegistry.create(Identifier.fromNamespaceAndPath(MOD_ID, "joined"), builder ->
builder.initializer(() -> false)
.persistent(Codec.BOOL)
.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(shouldInsertEmeralds(server)) {
insertEmeralds(server);
}
});
}
public static void giveEquipment(ServerPlayer player) {
Inventory inv = player.getInventory();
inv.add(new ItemStack(Items.IRON_PICKAXE));
inv.add(new ItemStack(Items.IRON_AXE));
inv.add(new ItemStack(Items.IRON_SHOVEL));
inv.add(new ItemStack(Items.BUCKET));
inv.add(new ItemStack(Items.BREAD, 32));
inv.add(new ItemStack(Items.EMERALD, 64));
inv.add(new ItemStack(Items.EMERALD, 32));
player.setItemSlot(EquipmentSlot.OFFHAND, new ItemStack(Items.SHIELD));
player.setItemSlot(EquipmentSlot.HEAD, new ItemStack(Items.IRON_HELMET));
player.setItemSlot(EquipmentSlot.CHEST, new ItemStack(Items.IRON_CHESTPLATE));
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();
}
}
}
}
public static boolean shouldInsertEmeralds(MinecraftServer server) {
if(server.clockManager().isAtTimeMarker(server.registryAccess().getOrThrow(WorldClocks.OVERWORLD), ClockTimeMarkers.DAY)) {
if(server.getPlayerList().getPlayerCount() > 0) {
for(ServerPlayer player : server.getPlayerList().getPlayers()) {
if(Util.getMillis() - player.getLastActionTime() < TimeUnit.MINUTES.toMillis(5)) {
return true;
}
}
}
}
return false;
}
}