From eb90d2f6ecc2e6fdea7d178e2330edac320a8549 Mon Sep 17 00:00:00 2001 From: candle Date: Sat, 10 May 2025 16:52:22 -0400 Subject: [PATCH] lots of linter stuff mostly --- .../SuitcaseRegistryState.java | 37 ++++++---- .../travelerssuitcase/TravelersSuitcase.java | 15 ++++ .../travelerssuitcase/block/ModBlocks.java | 39 +++++------ .../block/PocketPortalBlock.java | 47 +++++++------ .../block/SuitcaseBlock.java | 52 +++++++------- .../block/entity/SuitcaseBlockEntity.java | 69 +++++++++---------- .../travelerssuitcase/item/KeystoneItem.java | 17 +++-- src/main/resources/META-INF/mods.toml | 2 +- .../assets/travelerssuitcase/lang/en_us.json | 2 +- 9 files changed, 152 insertions(+), 128 deletions(-) diff --git a/src/main/java/io/lampnet/travelerssuitcase/SuitcaseRegistryState.java b/src/main/java/io/lampnet/travelerssuitcase/SuitcaseRegistryState.java index 243d719..e5ca29f 100644 --- a/src/main/java/io/lampnet/travelerssuitcase/SuitcaseRegistryState.java +++ b/src/main/java/io/lampnet/travelerssuitcase/SuitcaseRegistryState.java @@ -8,9 +8,11 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.world.level.saveddata.SavedData; import net.minecraft.core.BlockPos; import net.minecraft.world.level.Level; +import org.jetbrains.annotations.NotNull; import java.util.HashMap; import java.util.Map; +import java.util.Objects; public class SuitcaseRegistryState extends SavedData { private static final String REGISTRY_KEY = "travelers_suitcase_registry"; @@ -21,22 +23,12 @@ public class SuitcaseRegistryState extends SavedData { } @Override - public CompoundTag save(CompoundTag nbt) { + public @NotNull CompoundTag save(@NotNull CompoundTag nbt) { ListTag keystonesList = new ListTag(); for (Map.Entry> keystoneEntry : registry.entrySet()) { CompoundTag keystoneNbt = new CompoundTag(); keystoneNbt.putString("KeystoneName", keystoneEntry.getKey()); - ListTag playersList = new ListTag(); - for (Map.Entry playerEntry : keystoneEntry.getValue().entrySet()) { - CompoundTag playerNbt = new CompoundTag(); - playerNbt.putString("UUID", playerEntry.getKey()); - BlockPos pos = playerEntry.getValue(); - playerNbt.putInt("X", pos.getX()); - playerNbt.putInt("Y", pos.getY()); - playerNbt.putInt("Z", pos.getZ()); - - playersList.add(playerNbt); - } + ListTag playersList = getPlayersList(keystoneEntry); keystoneNbt.put("Players", playersList); keystonesList.add(keystoneNbt); } @@ -44,6 +36,21 @@ public class SuitcaseRegistryState extends SavedData { return nbt; } + private static @NotNull ListTag getPlayersList(Map.Entry> keystoneEntry) { + ListTag playersList = new ListTag(); + for (Map.Entry playerEntry : keystoneEntry.getValue().entrySet()) { + CompoundTag playerNbt = new CompoundTag(); + playerNbt.putString("UUID", playerEntry.getKey()); + BlockPos pos = playerEntry.getValue(); + playerNbt.putInt("X", pos.getX()); + playerNbt.putInt("Y", pos.getY()); + playerNbt.putInt("Z", pos.getZ()); + + playersList.add(playerNbt); + } + return playersList; + } + public static SuitcaseRegistryState load(CompoundTag nbt) { SuitcaseRegistryState state = new SuitcaseRegistryState(); if (nbt.contains("SuitcaseRegistry")) { @@ -70,7 +77,11 @@ public class SuitcaseRegistryState extends SavedData { } public static SuitcaseRegistryState getState(MinecraftServer server) { - return server.getLevel(Level.OVERWORLD).getDataStorage() + return Objects.requireNonNull(server.getLevel(Level.OVERWORLD)).getDataStorage() .computeIfAbsent(SuitcaseRegistryState::load, SuitcaseRegistryState::new, REGISTRY_KEY); } + + public Map> getRegistry() { + return this.registry; + } } \ No newline at end of file diff --git a/src/main/java/io/lampnet/travelerssuitcase/TravelersSuitcase.java b/src/main/java/io/lampnet/travelerssuitcase/TravelersSuitcase.java index 45068bf..f15586e 100644 --- a/src/main/java/io/lampnet/travelerssuitcase/TravelersSuitcase.java +++ b/src/main/java/io/lampnet/travelerssuitcase/TravelersSuitcase.java @@ -4,6 +4,8 @@ import io.lampnet.travelerssuitcase.block.ModBlocks; import io.lampnet.travelerssuitcase.block.entity.ModBlockEntities; import io.lampnet.travelerssuitcase.item.ModItemGroups; import io.lampnet.travelerssuitcase.item.ModItems; +import io.lampnet.travelerssuitcase.block.entity.SuitcaseBlockEntity; +import io.lampnet.travelerssuitcase.SuitcaseRegistryState; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.fml.common.Mod; @@ -25,6 +27,8 @@ public class TravelersSuitcase { ModBlockEntities.register(modEventBus); MinecraftForge.EVENT_BUS.addListener(this::onWorldLoad); + MinecraftForge.EVENT_BUS.addListener(this::onServerStarting); + MinecraftForge.EVENT_BUS.addListener(this::onServerStopping); LOGGER.info("Initializing " + MODID); } @@ -62,4 +66,15 @@ public class TravelersSuitcase { } } } + + private void onServerStarting(net.minecraftforge.event.server.ServerStartingEvent event) { + SuitcaseRegistryState state = SuitcaseRegistryState.getState(event.getServer()); + SuitcaseBlockEntity.initializeSuitcaseRegistry(state.getRegistry()); + } + + private void onServerStopping(net.minecraftforge.event.server.ServerStoppingEvent event) { + SuitcaseRegistryState state = SuitcaseRegistryState.getState(event.getServer()); + SuitcaseBlockEntity.saveSuitcaseRegistryTo(state.getRegistry()); + state.setDirty(); + } } \ No newline at end of file diff --git a/src/main/java/io/lampnet/travelerssuitcase/block/ModBlocks.java b/src/main/java/io/lampnet/travelerssuitcase/block/ModBlocks.java index da2629f..223797a 100644 --- a/src/main/java/io/lampnet/travelerssuitcase/block/ModBlocks.java +++ b/src/main/java/io/lampnet/travelerssuitcase/block/ModBlocks.java @@ -21,11 +21,8 @@ public class ModBlocks { public static final DeferredRegister BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, TravelersSuitcase.MODID); - private static ToIntFunction getLuminance(boolean openCheck) { - if (openCheck) { - return (state) -> state.getValue(SuitcaseBlock.OPEN) ? 8 : 0; - } - return (state) -> 0; + private static ToIntFunction getLuminance() { + return (state) -> state.getValue(SuitcaseBlock.OPEN) ? 8 : 0; } private static ToIntFunction getPortalLuminance() { return (state) -> 10; @@ -36,37 +33,37 @@ public class ModBlocks { .sound(SoundType.WOOL) .strength(0.2f) .noOcclusion() - .lightLevel(getLuminance(true)))); + .lightLevel(getLuminance()))); public static final RegistryObject WHITE_SUITCASE = registerBlock("white_suitcase", - () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.WHITE_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance(true)))); + () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.WHITE_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance()))); public static final RegistryObject BLACK_SUITCASE = registerBlock("black_suitcase", - () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.BLACK_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance(true)))); + () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.BLACK_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance()))); public static final RegistryObject LIGHT_GRAY_SUITCASE = registerBlock("light_gray_suitcase", - () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.LIGHT_GRAY_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance(true)))); + () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.LIGHT_GRAY_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance()))); public static final RegistryObject GRAY_SUITCASE = registerBlock("gray_suitcase", - () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.GRAY_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance(true)))); + () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.GRAY_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance()))); public static final RegistryObject RED_SUITCASE = registerBlock("red_suitcase", - () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.RED_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance(true)))); + () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.RED_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance()))); public static final RegistryObject ORANGE_SUITCASE = registerBlock("orange_suitcase", - () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.ORANGE_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance(true)))); + () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.ORANGE_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance()))); public static final RegistryObject YELLOW_SUITCASE = registerBlock("yellow_suitcase", - () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.YELLOW_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance(true)))); + () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.YELLOW_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance()))); public static final RegistryObject LIME_SUITCASE = registerBlock("lime_suitcase", - () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.LIME_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance(true)))); + () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.LIME_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance()))); public static final RegistryObject GREEN_SUITCASE = registerBlock("green_suitcase", - () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.GREEN_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance(true)))); + () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.GREEN_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance()))); public static final RegistryObject CYAN_SUITCASE = registerBlock("cyan_suitcase", - () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.CYAN_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance(true)))); + () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.CYAN_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance()))); public static final RegistryObject LIGHT_BLUE_SUITCASE = registerBlock("light_blue_suitcase", - () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.LIGHT_BLUE_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance(true)))); + () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.LIGHT_BLUE_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance()))); public static final RegistryObject BLUE_SUITCASE = registerBlock("blue_suitcase", - () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.BLUE_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance(true)))); + () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.BLUE_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance()))); public static final RegistryObject PURPLE_SUITCASE = registerBlock("purple_suitcase", - () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.PURPLE_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance(true)))); + () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.PURPLE_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance()))); public static final RegistryObject MAGENTA_SUITCASE = registerBlock("magenta_suitcase", - () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.MAGENTA_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance(true)))); + () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.MAGENTA_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance()))); public static final RegistryObject PINK_SUITCASE = registerBlock("pink_suitcase", - () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.PINK_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance(true)))); + () -> new SuitcaseBlock(BlockBehaviour.Properties.copy(Blocks.PINK_WOOL).sound(SoundType.WOOL).strength(0.2f).noOcclusion().lightLevel(getLuminance()))); public static final RegistryObject PORTAL = registerBlock("portal", () -> new PocketPortalBlock(BlockBehaviour.Properties.copy(Blocks.NETHER_PORTAL) diff --git a/src/main/java/io/lampnet/travelerssuitcase/block/PocketPortalBlock.java b/src/main/java/io/lampnet/travelerssuitcase/block/PocketPortalBlock.java index 75789db..b0b0417 100644 --- a/src/main/java/io/lampnet/travelerssuitcase/block/PocketPortalBlock.java +++ b/src/main/java/io/lampnet/travelerssuitcase/block/PocketPortalBlock.java @@ -26,10 +26,12 @@ import net.minecraft.world.level.chunk.LevelChunk; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.nbt.Tag; +import org.jetbrains.annotations.NotNull; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; public class PocketPortalBlock extends Block { private static final Map LAST_KNOWN_POSITIONS = new HashMap<>(); @@ -63,7 +65,7 @@ public class PocketPortalBlock extends Block { } private boolean attemptPlayerInventorySuitcaseTeleport(Level world, ServerLevel overworld, ServerPlayer player, String keystoneName) { - for (ServerPlayer serverPlayer : world.getServer().getPlayerList().getPlayers()) { + for (ServerPlayer serverPlayer : Objects.requireNonNull(world.getServer()).getPlayerList().getPlayers()) { if (scanPlayerInventoryForSuitcase(serverPlayer, player, keystoneName, world, overworld)) { return true; } @@ -127,7 +129,7 @@ public class PocketPortalBlock extends Block { } @Override - public void entityInside(BlockState state, Level world, BlockPos pos, Entity entity) { + public void entityInside(@NotNull BlockState state, Level world, @NotNull BlockPos pos, @NotNull Entity entity) { if (!world.isClientSide && entity instanceof ServerPlayer player) { ResourceLocation dimensionLocation = world.dimension().location(); String currentDimensionPath = dimensionLocation.getPath(); @@ -135,7 +137,7 @@ public class PocketPortalBlock extends Block { String keystoneName = currentDimensionPath.replace("pocket_dimension_", ""); preparePlayerForTeleport(player); world.playSound(null, pos, SoundEvents.BUNDLE_DROP_CONTENTS, SoundSource.PLAYERS, 2.0f, 1.0f); - ServerLevel overworld = world.getServer().getLevel(Level.OVERWORLD); + ServerLevel overworld = Objects.requireNonNull(world.getServer()).getLevel(Level.OVERWORLD); if (overworld == null) return; boolean teleported = false; @@ -189,8 +191,8 @@ public class PocketPortalBlock extends Block { if (targetEntity instanceof SuitcaseBlockEntity suitcase) { SuitcaseBlockEntity.EnteredPlayerData exitData = suitcase.getExitPosition(player.getUUID().toString()); if (exitData != null) { - teleportToPosition(world, player, overworld, exitData.x, exitData.y, exitData.z, exitData.yaw, player.getXRot()); - world.getServer().execute(() -> { + teleportToPosition(world, player, overworld, exitData.x(), exitData.y(), exitData.z(), exitData.yaw(), player.getXRot()); + Objects.requireNonNull(world.getServer()).execute(() -> { overworld.setChunkForced(suitcaseChunkPos.x, suitcaseChunkPos.z, false); }); return true; @@ -204,7 +206,7 @@ public class PocketPortalBlock extends Block { // Method 2: Try to find the suitcase as an item entity in the world private boolean attemptSuitcaseItemTeleport(Level world, ServerLevel overworld, ServerPlayer player, String keystoneName) { - BlockPos searchCenter = null; + BlockPos searchCenter; BlockPos suitcasePos = SuitcaseBlockEntity.findSuitcasePosition(keystoneName, player.getUUID().toString()); if (suitcasePos != null) { searchCenter = suitcasePos; @@ -280,23 +282,26 @@ public class PocketPortalBlock extends Block { } private void updateItemLore(ItemStack stack, int playerCount) { - if (stack.hasTag() && stack.getTag().contains("display")) { - CompoundTag display = stack.getTag().getCompound("display"); - if (display != null && display.contains("Lore", Tag.TAG_LIST)) { - ListTag lore = display.getList("Lore", Tag.TAG_STRING); - ListTag newLore = new ListTag(); - for (int i = 0; i < lore.size(); i++) { - String loreStr = lore.getString(i); - if (!loreStr.contains("traveler")) { - newLore.add(lore.get(i)); + if (stack.hasTag()) { + assert stack.getTag() != null; + if (stack.getTag().contains("display")) { + CompoundTag display = stack.getTag().getCompound("display"); + if (display.contains("Lore", Tag.TAG_LIST)) { + ListTag lore = display.getList("Lore", Tag.TAG_STRING); + ListTag newLore = new ListTag(); + for (int i = 0; i < lore.size(); i++) { + String loreStr = lore.getString(i); + if (!loreStr.contains("traveler")) { + newLore.add(lore.get(i)); + } } + if (playerCount > 0) { + Component warningText = Component.literal("§c⚠ Contains " + playerCount + " traveler(s)!") + .withStyle(ChatFormatting.RED); + newLore.add(0, StringTag.valueOf(Component.Serializer.toJson(warningText))); + } + display.put("Lore", newLore); } - if (playerCount > 0) { - Component warningText = Component.literal("§c⚠ Contains " + playerCount + " traveler(s)!") - .withStyle(ChatFormatting.RED); - newLore.add(0, StringTag.valueOf(Component.Serializer.toJson(warningText))); - } - display.put("Lore", newLore); } } } diff --git a/src/main/java/io/lampnet/travelerssuitcase/block/SuitcaseBlock.java b/src/main/java/io/lampnet/travelerssuitcase/block/SuitcaseBlock.java index 9a65a0b..b9b74b3 100644 --- a/src/main/java/io/lampnet/travelerssuitcase/block/SuitcaseBlock.java +++ b/src/main/java/io/lampnet/travelerssuitcase/block/SuitcaseBlock.java @@ -48,11 +48,10 @@ import net.minecraft.world.level.Level; import net.minecraft.world.Containers; import net.minecraft.world.item.DyeColor; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; + +import java.util.*; public class SuitcaseBlock extends BaseEntityBlock { public static final BooleanProperty OPEN = BooleanProperty.create("open"); @@ -72,13 +71,13 @@ public class SuitcaseBlock extends BaseEntityBlock { } @Override - public RenderShape getRenderShape(BlockState pState) { + public @NotNull RenderShape getRenderShape(@NotNull BlockState pState) { return RenderShape.MODEL; } @Nullable @Override - public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) { + public BlockEntity newBlockEntity(@NotNull BlockPos pPos, @NotNull BlockState pState) { return new SuitcaseBlockEntity(pPos, pState); } @@ -88,7 +87,7 @@ public class SuitcaseBlock extends BaseEntityBlock { } @Override - public void setPlacedBy(Level world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack itemStack) { + public void setPlacedBy(@NotNull Level world, @NotNull BlockPos pos, @NotNull BlockState state, @Nullable LivingEntity placer, @NotNull ItemStack itemStack) { super.setPlacedBy(world, pos, state, placer, itemStack); if (!world.isClientSide()) { BlockEntity blockEntity = world.getBlockEntity(pos); @@ -100,11 +99,11 @@ public class SuitcaseBlock extends BaseEntityBlock { if (keystoneName != null) { List players = suitcase.getEnteredPlayers(); for (SuitcaseBlockEntity.EnteredPlayerData player : players) { - suitcase.updatePlayerSuitcasePosition(player.uuid, pos); + suitcase.updatePlayerSuitcasePosition(player.uuid(), pos); Map suitcases = SuitcaseBlockEntity.SUITCASE_REGISTRY.computeIfAbsent( keystoneName, k -> new HashMap<>() ); - suitcases.put(player.uuid, pos); + suitcases.put(player.uuid(), pos); } } } @@ -113,7 +112,7 @@ public class SuitcaseBlock extends BaseEntityBlock { } @Override - public void onRemove(BlockState state, Level world, BlockPos pos, BlockState newState, boolean isMoving) { + public void onRemove(BlockState state, @NotNull Level world, @NotNull BlockPos pos, BlockState newState, boolean isMoving) { if (!state.is(newState.getBlock())) { BlockEntity blockEntity = world.getBlockEntity(pos); if (blockEntity instanceof SuitcaseBlockEntity suitcase) { @@ -156,7 +155,7 @@ public class SuitcaseBlock extends BaseEntityBlock { } @Override - public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { + public @NotNull InteractionResult use(@NotNull BlockState state, Level world, @NotNull BlockPos pos, @NotNull Player player, @NotNull InteractionHand hand, @NotNull BlockHitResult hit) { if (world.isClientSide()) { return InteractionResult.SUCCESS; } @@ -230,7 +229,7 @@ public class SuitcaseBlock extends BaseEntityBlock { } @Override - public void entityInside(BlockState state, Level world, BlockPos pos, Entity entity) { + public void entityInside(@NotNull BlockState state, Level world, @NotNull BlockPos pos, @NotNull Entity entity) { if (!world.isClientSide() && entity instanceof ServerPlayer player) { if (!state.getValue(OPEN) || !player.isShiftKeyDown()) { return; @@ -246,7 +245,7 @@ public class SuitcaseBlock extends BaseEntityBlock { String dimensionName = "pocket_dimension_" + keystoneName; ResourceLocation dimensionRL = new ResourceLocation(TravelersSuitcase.MODID, dimensionName); ResourceKey dimensionKey = ResourceKey.create(Registries.DIMENSION, dimensionRL); - ServerLevel targetWorld = world.getServer().getLevel(dimensionKey); + ServerLevel targetWorld = Objects.requireNonNull(world.getServer()).getLevel(dimensionKey); if (targetWorld != null) { suitcase.playerEntered(player); player.stopRiding(); @@ -263,14 +262,13 @@ public class SuitcaseBlock extends BaseEntityBlock { } @Override - public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext context) { - switch(state.getValue(FACING)) { - case NORTH: return SHAPE_N; - case SOUTH: return SHAPE_S; - case EAST: return SHAPE_E; - case WEST: return SHAPE_W; - default: return SHAPE_N; - } + public @NotNull VoxelShape getShape(BlockState state, @NotNull BlockGetter world, @NotNull BlockPos pos, @NotNull CollisionContext context) { + return switch (state.getValue(FACING)) { + case SOUTH -> SHAPE_S; + case EAST -> SHAPE_E; + case WEST -> SHAPE_W; + default -> SHAPE_N; + }; } @Nullable @@ -282,13 +280,13 @@ public class SuitcaseBlock extends BaseEntityBlock { } @Override - public ItemStack getCloneItemStack(BlockGetter world, BlockPos pos, BlockState state) { + public @NotNull ItemStack getCloneItemStack(@NotNull BlockGetter world, @NotNull BlockPos pos, @NotNull BlockState state) { ItemStack stack = super.getCloneItemStack(world, pos, state); BlockEntity blockEntity = world.getBlockEntity(pos); if (blockEntity instanceof SuitcaseBlockEntity suitcase) { CompoundTag beNbt = new CompoundTag(); suitcase.saveAdditional(beNbt); - if (beNbt != null && !beNbt.isEmpty()) { + if (!beNbt.isEmpty()) { BlockItem.setBlockEntityData(stack, ModBlockEntities.SUITCASE_BLOCK_ENTITY.get(), beNbt); } @@ -321,13 +319,13 @@ public class SuitcaseBlock extends BaseEntityBlock { } @Override - public List getDrops(BlockState state, LootParams.Builder builder) { + public @NotNull List getDrops(@NotNull BlockState state, LootParams.Builder builder) { ItemStack stack = new ItemStack(this); BlockEntity blockEntity = builder.getOptionalParameter(LootContextParams.BLOCK_ENTITY); if (blockEntity instanceof SuitcaseBlockEntity suitcase) { CompoundTag beNbt = new CompoundTag(); suitcase.saveAdditional(beNbt); - if (beNbt != null && !beNbt.isEmpty()) { + if (!beNbt.isEmpty()) { BlockItem.setBlockEntityData(stack, ModBlockEntities.SUITCASE_BLOCK_ENTITY.get(), beNbt); } @@ -358,9 +356,9 @@ public class SuitcaseBlock extends BaseEntityBlock { } @Override - public boolean triggerEvent(BlockState state, Level world, BlockPos pos, int type, int data) { + public boolean triggerEvent(@NotNull BlockState state, @NotNull Level world, @NotNull BlockPos pos, int type, int data) { super.triggerEvent(state, world, pos, type, data); BlockEntity blockentity = world.getBlockEntity(pos); - return blockentity != null ? blockentity.triggerEvent(type, data) : false; + return blockentity != null && blockentity.triggerEvent(type, data); } } \ No newline at end of file diff --git a/src/main/java/io/lampnet/travelerssuitcase/block/entity/SuitcaseBlockEntity.java b/src/main/java/io/lampnet/travelerssuitcase/block/entity/SuitcaseBlockEntity.java index c285873..ae1026b 100644 --- a/src/main/java/io/lampnet/travelerssuitcase/block/entity/SuitcaseBlockEntity.java +++ b/src/main/java/io/lampnet/travelerssuitcase/block/entity/SuitcaseBlockEntity.java @@ -11,6 +11,7 @@ import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.*; @@ -21,44 +22,38 @@ public class SuitcaseBlockEntity extends BlockEntity { private boolean dimensionLocked = true; private final List enteredPlayers = new ArrayList<>(); - public static class EnteredPlayerData { - public final String uuid; - public final double x, y, z; - public final float pitch, yaw; - public final BlockPos suitcasePos; - - public EnteredPlayerData(String uuid, double x, double y, double z, float pitch, float yaw, BlockPos suitcasePos) { - this.uuid = uuid; this.x = x; this.y = y; this.z = z; - this.pitch = pitch; this.yaw = yaw; - this.suitcasePos = suitcasePos; - } + public record EnteredPlayerData(String uuid, double x, double y, double z, float pitch, float yaw, + BlockPos suitcasePos) { public CompoundTag toNbt() { - CompoundTag nbt = new CompoundTag(); - nbt.putString("UUID", uuid); - nbt.putDouble("X", x); nbt.putDouble("Y", y); nbt.putDouble("Z", z); - nbt.putFloat("Pitch", pitch); nbt.putFloat("Yaw", yaw); - if (suitcasePos != null) { - nbt.putInt("SuitcaseX", suitcasePos.getX()); - nbt.putInt("SuitcaseY", suitcasePos.getY()); - nbt.putInt("SuitcaseZ", suitcasePos.getZ()); + CompoundTag nbt = new CompoundTag(); + nbt.putString("UUID", uuid); + nbt.putDouble("X", x); + nbt.putDouble("Y", y); + nbt.putDouble("Z", z); + nbt.putFloat("Pitch", pitch); + nbt.putFloat("Yaw", yaw); + if (suitcasePos != null) { + nbt.putInt("SuitcaseX", suitcasePos.getX()); + nbt.putInt("SuitcaseY", suitcasePos.getY()); + nbt.putInt("SuitcaseZ", suitcasePos.getZ()); + } + return nbt; } - return nbt; - } - public static EnteredPlayerData fromNbt(CompoundTag nbt) { - BlockPos sPos = null; - if (nbt.contains("SuitcaseX") && nbt.contains("SuitcaseY") && nbt.contains("SuitcaseZ")) { - sPos = new BlockPos(nbt.getInt("SuitcaseX"), nbt.getInt("SuitcaseY"), nbt.getInt("SuitcaseZ")); + public static EnteredPlayerData fromNbt(CompoundTag nbt) { + BlockPos sPos = null; + if (nbt.contains("SuitcaseX") && nbt.contains("SuitcaseY") && nbt.contains("SuitcaseZ")) { + sPos = new BlockPos(nbt.getInt("SuitcaseX"), nbt.getInt("SuitcaseY"), nbt.getInt("SuitcaseZ")); + } + return new EnteredPlayerData( + nbt.getString("UUID"), + nbt.getDouble("X"), nbt.getDouble("Y"), nbt.getDouble("Z"), + nbt.getFloat("Pitch"), nbt.getFloat("Yaw"), + sPos + ); } - return new EnteredPlayerData( - nbt.getString("UUID"), - nbt.getDouble("X"), nbt.getDouble("Y"), nbt.getDouble("Z"), - nbt.getFloat("Pitch"), nbt.getFloat("Yaw"), - sPos - ); } - } public SuitcaseBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SUITCASE_BLOCK_ENTITY.get(), pos, state); @@ -131,7 +126,7 @@ public class SuitcaseBlockEntity extends BlockEntity { } @Override - public void saveAdditional(CompoundTag nbt) { + public void saveAdditional(@NotNull CompoundTag nbt) { super.saveAdditional(nbt); if (boundKeystoneName != null) { nbt.putString("BoundKeystone", boundKeystoneName); @@ -149,7 +144,7 @@ public class SuitcaseBlockEntity extends BlockEntity { } @Override - public void load(CompoundTag nbt) { + public void load(@NotNull CompoundTag nbt) { super.load(nbt); if (nbt.contains("BoundKeystone", Tag.TAG_STRING)) { boundKeystoneName = nbt.getString("BoundKeystone"); @@ -174,7 +169,7 @@ public class SuitcaseBlockEntity extends BlockEntity { } @Override - public CompoundTag getUpdateTag() { + public @NotNull CompoundTag getUpdateTag() { CompoundTag nbt = new CompoundTag(); this.saveAdditional(nbt); return nbt; @@ -214,8 +209,8 @@ public class SuitcaseBlockEntity extends BlockEntity { public static void saveSuitcaseRegistryTo(Map> destination) { destination.clear(); SUITCASE_REGISTRY.forEach((key, value) -> { - Map players = destination.computeIfAbsent(key, k -> new HashMap<>()); - players.putAll(value); + Map players = destination.computeIfAbsent(key, k -> new HashMap<>()); + players.putAll(value); }); } diff --git a/src/main/java/io/lampnet/travelerssuitcase/item/KeystoneItem.java b/src/main/java/io/lampnet/travelerssuitcase/item/KeystoneItem.java index 580a770..f47afc8 100644 --- a/src/main/java/io/lampnet/travelerssuitcase/item/KeystoneItem.java +++ b/src/main/java/io/lampnet/travelerssuitcase/item/KeystoneItem.java @@ -18,6 +18,7 @@ import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.level.Level; import net.minecraft.world.level.storage.LevelResource; import net.minecraft.resources.ResourceLocation; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import net.minecraft.world.item.ItemStack.TooltipPart; @@ -36,7 +37,7 @@ public class KeystoneItem extends Item { } @Override - public InteractionResultHolder use(Level world, Player player, InteractionHand hand) { + public @NotNull InteractionResultHolder use(@NotNull Level world, Player player, @NotNull InteractionHand hand) { ItemStack stack = player.getItemInHand(hand); String keystoneName = stack.hasCustomHoverName() ? stack.getHoverName().getString().toLowerCase() : ""; String defaultName = "item.travelerssuitcase.keystone"; @@ -70,7 +71,7 @@ public class KeystoneItem extends Item { } @Override - public void appendHoverText(ItemStack stack, @Nullable Level world, List tooltip, TooltipFlag flag) { + public void appendHoverText(ItemStack stack, @Nullable Level world, @NotNull List tooltip, @NotNull TooltipFlag flag) { String keystoneName = stack.hasCustomHoverName() ? stack.getHoverName().getString().toLowerCase() : ""; String defaultNameKey = "item.travelerssuitcase.keystone"; if (!stack.hasCustomHoverName() || keystoneName.isEmpty() || @@ -80,7 +81,7 @@ public class KeystoneItem extends Item { } @Override - public void inventoryTick(ItemStack stack, Level world, Entity entity, int slot, boolean selected) { + public void inventoryTick(ItemStack stack, @NotNull Level world, @NotNull Entity entity, int slot, boolean selected) { String keystoneName = stack.hasCustomHoverName() ? stack.getHoverName().getString().toLowerCase() : ""; String defaultNameKey = "item.travelerssuitcase.keystone"; if (!stack.hasCustomHoverName() || keystoneName.isEmpty() || @@ -120,9 +121,11 @@ public class KeystoneItem extends Item { .resolve("dimension_registry"); Files.createDirectories(dimensionRegistryPath); Path dimensionRegistryFile = dimensionRegistryPath.resolve("registry.txt"); - Set registeredDimensionsInFile = new HashSet<>(); + Set registeredDimensionsInFile; if (Files.exists(dimensionRegistryFile)) { - registeredDimensionsInFile = new HashSet<>(Files.readAllLines(dimensionRegistryFile)); + registeredDimensionsInFile = new HashSet<>(Files.readAllLines(dimensionRegistryFile)); + } else { + registeredDimensionsInFile = new HashSet<>(); } boolean isDimensionInRegistryFile = registeredDimensionsInFile.contains(dimensionName); @@ -162,12 +165,12 @@ public class KeystoneItem extends Item { } return false; } catch (IOException e) { - TravelersSuitcase.LOGGER.error("Failed to create dimension: " + dimensionName, e); + TravelersSuitcase.LOGGER.error("Failed to create dimension: {}", dimensionName, e); return false; } } - private void createPackMcmeta(Path datapackPath) throws IOException { + private void createPackMcmeta(@NotNull Path datapackPath) throws IOException { Path packMcmeta = datapackPath.resolve("pack.mcmeta"); if (!Files.exists(packMcmeta)) { String content = """ diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index a1fc7ec..2f70f54 100644 --- a/src/main/resources/META-INF/mods.toml +++ b/src/main/resources/META-INF/mods.toml @@ -6,7 +6,7 @@ license="MIT" modId="travelerssuitcase" version="${mod_version}" displayName="Traveler's Suitcase" -authors="BennyBoops, Candle" +authors="BennyBoops, candle" description=''' A mod that adds magical suitcases that can store player positions and create pocket dimensions. ''' diff --git a/src/main/resources/assets/travelerssuitcase/lang/en_us.json b/src/main/resources/assets/travelerssuitcase/lang/en_us.json index 75fcf0a..424713b 100644 --- a/src/main/resources/assets/travelerssuitcase/lang/en_us.json +++ b/src/main/resources/assets/travelerssuitcase/lang/en_us.json @@ -20,5 +20,5 @@ "block.travelerssuitcase.portal": "Portal Block", - "itemgroup.pocket": "Pocket Repose" + "itemgroup.travelerssuitcase": "Traveler's Suitcase" } \ No newline at end of file