lots of linter stuff mostly

This commit is contained in:
candle 2025-05-10 16:52:22 -04:00
parent d6f599e03b
commit eb90d2f6ec
9 changed files with 152 additions and 128 deletions

View File

@ -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<String, Map<String, BlockPos>> keystoneEntry : registry.entrySet()) {
CompoundTag keystoneNbt = new CompoundTag();
keystoneNbt.putString("KeystoneName", keystoneEntry.getKey());
ListTag playersList = new ListTag();
for (Map.Entry<String, BlockPos> 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<String, Map<String, BlockPos>> keystoneEntry) {
ListTag playersList = new ListTag();
for (Map.Entry<String, BlockPos> 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<String, Map<String, BlockPos>> getRegistry() {
return this.registry;
}
}

View File

@ -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();
}
}

View File

@ -21,11 +21,8 @@ public class ModBlocks {
public static final DeferredRegister<Block> BLOCKS =
DeferredRegister.create(ForgeRegistries.BLOCKS, TravelersSuitcase.MODID);
private static ToIntFunction<BlockState> getLuminance(boolean openCheck) {
if (openCheck) {
return (state) -> state.getValue(SuitcaseBlock.OPEN) ? 8 : 0;
}
return (state) -> 0;
private static ToIntFunction<BlockState> getLuminance() {
return (state) -> state.getValue(SuitcaseBlock.OPEN) ? 8 : 0;
}
private static ToIntFunction<BlockState> 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<Block> 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<Block> 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<Block> 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<Block> 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<Block> 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<Block> 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<Block> 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<Block> 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<Block> 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<Block> 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<Block> 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<Block> 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<Block> 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<Block> 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<Block> 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<Block> PORTAL = registerBlock("portal",
() -> new PocketPortalBlock(BlockBehaviour.Properties.copy(Blocks.NETHER_PORTAL)

View File

@ -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<String, PlayerPositionData> 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);
}
}
}

View File

@ -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<SuitcaseBlockEntity.EnteredPlayerData> players = suitcase.getEnteredPlayers();
for (SuitcaseBlockEntity.EnteredPlayerData player : players) {
suitcase.updatePlayerSuitcasePosition(player.uuid, pos);
suitcase.updatePlayerSuitcasePosition(player.uuid(), pos);
Map<String, BlockPos> 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<Level> 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<ItemStack> getDrops(BlockState state, LootParams.Builder builder) {
public @NotNull List<ItemStack> 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);
}
}

View File

@ -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<EnteredPlayerData> 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<String, Map<String, BlockPos>> destination) {
destination.clear();
SUITCASE_REGISTRY.forEach((key, value) -> {
Map<String, BlockPos> players = destination.computeIfAbsent(key, k -> new HashMap<>());
players.putAll(value);
Map<String, BlockPos> players = destination.computeIfAbsent(key, k -> new HashMap<>());
players.putAll(value);
});
}

View File

@ -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<ItemStack> use(Level world, Player player, InteractionHand hand) {
public @NotNull InteractionResultHolder<ItemStack> 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<Component> tooltip, TooltipFlag flag) {
public void appendHoverText(ItemStack stack, @Nullable Level world, @NotNull List<Component> 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<String> registeredDimensionsInFile = new HashSet<>();
Set<String> 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 = """

View File

@ -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.
'''

View File

@ -20,5 +20,5 @@
"block.travelerssuitcase.portal": "Portal Block",
"itemgroup.pocket": "Pocket Repose"
"itemgroup.travelerssuitcase": "Traveler's Suitcase"
}