package com.level314.teleportanchor; import net.minecraft.core.BlockPos; import net.minecraft.server.MinecraftServer; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.context.UseOnContext; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.UUID; public class TeleportAnchorBlockItem extends BlockItem { public TeleportAnchorBlockItem(final Block block, final Properties properties) { super(block, properties); } @Override public InteractionResult useOn(final UseOnContext context) { InteractionResult result = super.useOn(context); Level level = context.getLevel(); Player player = context.getPlayer(); if (result == InteractionResult.SUCCESS) { if (!level.isClientSide() && player != null) { BlockPos pos = context.getClickedPos().relative(context.getClickedFace()); if (level.getBlockState(pos).getBlock() == TeleportAnchor.TELEPORT_ANCHOR_BLOCK) { SavedAnchorData anchorData = SavedAnchorData.get(Objects.requireNonNull(level.getServer())); UUID anchorUuid = UUID.randomUUID(); while(anchorData.getAnchor(anchorUuid) != null){ TeleportAnchor.LOGGER.info("You are lucky! This program allocate a repeated UUID " + anchorUuid.toString()); // 这是彩蛋,但我不认为100年内能输出 anchorUuid = UUID.randomUUID(); } UUID ownerUuid = player.getUUID(); anchorData.putAnchor( anchorUuid, new AnchorRecord( level.dimension().identifier().toString(), pos.getX(), pos.getY(), pos.getZ(), System.currentTimeMillis(), new ArrayList<>(List.of(ownerUuid)), ownerUuid ) ); } } } return result; } }