Compare commits

...

2 Commits

Author SHA1 Message Date
eb90d2f6ec lots of linter stuff mostly 2025-05-10 16:52:22 -04:00
d6f599e03b some cleanup 2025-05-10 16:12:30 -04:00
13 changed files with 153 additions and 259 deletions

8
README
View File

@ -1,4 +1,6 @@
Traveler's Suitcase [FORGE]
ORIGINAL: https://github.com/BennyBoops/PocketRepose
# Traveler's Suitcase [FORGE]
This is a forge port of the PocketRepose mod.
A Forge port of the [PocketRepose](https://github.com/BennyBoops/PocketRepose) mod. [Modrinth](https://modrinth.com/mod/pocket-repose)
## About
This mod allows players to create their own pocket dimensions through magical suitcases.

View File

@ -12,39 +12,18 @@ import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
// An example config class. This is not required, but it's a good idea to have one to keep your config organized.
// Demonstrates how to use Forge's config APIs
@Mod.EventBusSubscriber(modid = TravelersSuitcase.MODID, bus = Mod.EventBusSubscriber.Bus.MOD)
public class Config {
private static final ForgeConfigSpec.Builder BUILDER = new ForgeConfigSpec.Builder();
private static final ForgeConfigSpec.BooleanValue LOG_DIRT_BLOCK = BUILDER.comment("Whether to log the dirt block on common setup").define("logDirtBlock", true);
private static final ForgeConfigSpec.IntValue MAGIC_NUMBER = BUILDER.comment("A magic number").defineInRange("magicNumber", 42, 0, Integer.MAX_VALUE);
public static final ForgeConfigSpec.ConfigValue<String> MAGIC_NUMBER_INTRODUCTION = BUILDER.comment("What you want the introduction message to be for the magic number").define("magicNumberIntroduction", "The magic number is... ");
// a list of strings that are treated as resource locations for items
private static final ForgeConfigSpec.ConfigValue<List<? extends String>> ITEM_STRINGS = BUILDER.comment("A list of items to log on common setup.").defineListAllowEmpty("items", List.of("minecraft:iron_ingot"), Config::validateItemName);
static final ForgeConfigSpec SPEC = BUILDER.build();
public static boolean logDirtBlock;
public static int magicNumber;
public static String magicNumberIntroduction;
public static Set<Item> items;
private static boolean validateItemName(final Object obj) {
return obj instanceof final String itemName && ForgeRegistries.ITEMS.containsKey(new ResourceLocation(itemName));
}
@SubscribeEvent
static void onLoad(final ModConfigEvent event) {
logDirtBlock = LOG_DIRT_BLOCK.get();
magicNumber = MAGIC_NUMBER.get();
magicNumberIntroduction = MAGIC_NUMBER_INTRODUCTION.get();
// convert the list of strings into a set of items
items = ITEM_STRINGS.get().stream().map(itemName -> ForgeRegistries.ITEMS.getValue(new ResourceLocation(itemName))).collect(Collectors.toSet());
}
}

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

@ -2,10 +2,10 @@ package io.lampnet.travelerssuitcase;
import io.lampnet.travelerssuitcase.block.ModBlocks;
import io.lampnet.travelerssuitcase.block.entity.ModBlockEntities;
// import io.lampnet.travelerssuitcase.block.entity.SuitcaseBlockEntity; // Commented out for now
// import io.lampnet.travelerssuitcase.config.SuitcaseRegistryState; // Commented out for now
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;
@ -21,18 +21,14 @@ public class TravelersSuitcase {
public TravelersSuitcase() {
IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
// Register items, blocks, etc.
ModItems.register(modEventBus);
ModBlocks.register(modEventBus);
ModItemGroups.register(modEventBus);
ModBlockEntities.register(modEventBus);
// Register world load event
MinecraftForge.EVENT_BUS.addListener(this::onWorldLoad);
// Register server events
// MinecraftForge.EVENT_BUS.addListener(this::onServerStarting);
// MinecraftForge.EVENT_BUS.addListener(this::onServerStopping);
MinecraftForge.EVENT_BUS.addListener(this::onServerStarting);
MinecraftForge.EVENT_BUS.addListener(this::onServerStopping);
LOGGER.info("Initializing " + MODID);
}
@ -71,16 +67,14 @@ public class TravelersSuitcase {
}
}
/* Commented out for now
private void onServerStarting(net.minecraftforge.event.server.ServerStartingEvent event) {
SuitcaseRegistryState state = SuitcaseRegistryState.getState(event.getServer());
SuitcaseBlockEntity.initializeSuitcaseRegistry(state.registry);
SuitcaseBlockEntity.initializeSuitcaseRegistry(state.getRegistry());
}
private void onServerStopping(net.minecraftforge.event.server.ServerStoppingEvent event) {
SuitcaseRegistryState state = SuitcaseRegistryState.getState(event.getServer());
SuitcaseBlockEntity.saveSuitcaseRegistryTo(state.registry);
SuitcaseBlockEntity.saveSuitcaseRegistryTo(state.getRegistry());
state.setDirty();
}
*/
}

View File

@ -1,86 +0,0 @@
package io.lampnet.travelerssuitcase;
import io.lampnet.travelerssuitcase.block.ModBlocks;
import io.lampnet.travelerssuitcase.block.entity.ModBlockEntities;
// import io.lampnet.travelerssuitcase.block.entity.SuitcaseBlockEntity; // Commented out for now
// import io.lampnet.travelerssuitcase.config.SuitcaseRegistryState; // Commented out for now
import io.lampnet.travelerssuitcase.item.ModItemGroups;
import io.lampnet.travelerssuitcase.item.ModItems;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@Mod(TravelersSuitcase.MODID)
public class Travelerssuitcase {
public static final String MODID = "travelerssuitcase";
public static final Logger LOGGER = LogManager.getLogger(MODID);
public Travelerssuitcase() {
IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
// Register items, blocks, etc.
ModItems.register(modEventBus);
ModBlocks.register(modEventBus);
ModItemGroups.register(modEventBus);
ModBlockEntities.register(modEventBus);
// Register world load event
MinecraftForge.EVENT_BUS.addListener(this::onWorldLoad);
// Register server events
// MinecraftForge.EVENT_BUS.addListener(this::onServerStarting);
// MinecraftForge.EVENT_BUS.addListener(this::onServerStopping);
LOGGER.info("Initializing " + MODID);
}
private void onWorldLoad(net.minecraftforge.event.level.LevelEvent.Load event) {
if (event.getLevel() instanceof net.minecraft.server.level.ServerLevel world) {
if (world.dimension().location().getNamespace().equals(MODID)) {
String dimensionName = world.dimension().location().getPath();
java.nio.file.Path structureMarkerPath = world.getServer().getWorldPath(net.minecraft.world.level.storage.LevelResource.ROOT)
.resolve("data")
.resolve(MODID)
.resolve("pending_structures")
.resolve(dimensionName + ".txt");
if (java.nio.file.Files.exists(structureMarkerPath)) {
try {
net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate template = world.getServer().getStructureManager()
.get(new net.minecraft.resources.ResourceLocation(MODID, "pocket_island_01"))
.orElse(null);
if (template != null) {
net.minecraft.core.BlockPos pos = new net.minecraft.core.BlockPos(0, 64, 0);
template.placeInWorld(
world,
pos,
pos,
new net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings(),
world.getRandom(),
net.minecraft.world.level.block.Block.UPDATE_ALL
);
java.nio.file.Files.delete(structureMarkerPath);
}
} catch (java.io.IOException e) {
LOGGER.error("Failed to place structure", e);
}
}
}
}
}
/* Commented out for now
private void onServerStarting(net.minecraftforge.event.server.ServerStartingEvent event) {
SuitcaseRegistryState state = SuitcaseRegistryState.getState(event.getServer());
SuitcaseBlockEntity.initializeSuitcaseRegistry(state.registry);
}
private void onServerStopping(net.minecraftforge.event.server.ServerStoppingEvent event) {
SuitcaseRegistryState state = SuitcaseRegistryState.getState(event.getServer());
SuitcaseBlockEntity.saveSuitcaseRegistryTo(state.registry);
state.setDirty();
}
*/
}

View File

@ -8,7 +8,6 @@ import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
@ -22,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;
@ -37,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

@ -1,6 +1,5 @@
package io.lampnet.travelerssuitcase.block;
import io.lampnet.travelerssuitcase.TravelersSuitcase;
import io.lampnet.travelerssuitcase.block.entity.SuitcaseBlockEntity;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
@ -27,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<>();
@ -64,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;
}
@ -128,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();
@ -136,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;
@ -190,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;
@ -205,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;
@ -281,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

@ -21,7 +21,6 @@ import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.StringTag;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
@ -49,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");
@ -73,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);
}
@ -89,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);
@ -101,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);
}
}
}
@ -114,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) {
@ -157,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;
}
@ -231,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;
@ -247,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();
@ -264,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
@ -283,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);
}
@ -322,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);
}
@ -359,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

@ -6,12 +6,12 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerPlayer;
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.*;
@ -22,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);
@ -132,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);
@ -150,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");
@ -175,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;
@ -215,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

@ -8,16 +8,16 @@ import net.minecraft.network.chat.Component;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.RegistryObject;
import net.minecraft.core.registries.Registries; // For DeferredRegister.create(Registries.CREATIVE_MODE_TAB...)
import net.minecraft.world.level.block.Blocks; // For Blocks.ANVIL
import net.minecraft.core.registries.Registries;
import net.minecraft.world.level.block.Blocks;
public class ModItemGroups {
public static final DeferredRegister<CreativeModeTab> CREATIVE_MODE_TABS =
DeferredRegister.create(Registries.CREATIVE_MODE_TAB, TravelersSuitcase.MODID);
public static final RegistryObject<CreativeModeTab> POCKET_GROUP = CREATIVE_MODE_TABS.register("pocket",
public static final RegistryObject<CreativeModeTab> SUITCASE_GROUP = CREATIVE_MODE_TABS.register("travelerssuitcase",
() -> CreativeModeTab.builder()
.title(Component.translatable("itemgroup.travelerssuitcase.pocket")) // Changed itemgroup name
.title(Component.translatable("itemgroup.travelerssuitcase"))
.icon(() -> new ItemStack(ModItems.KEYSTONE.get()))
.displayItems((displayParameters, output) -> {
output.accept(ModItems.KEYSTONE.get());
@ -45,7 +45,7 @@ public class ModItemGroups {
})
.build());
public static void register(IEventBus eventBus) { // Renamed from registerItemGroups
public static void register(IEventBus eventBus) {
CREATIVE_MODE_TABS.register(eventBus);
TravelersSuitcase.LOGGER.info("Registering Creative Mode Tabs for " + TravelersSuitcase.MODID);
}

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"
}