Merge pull request #15 from dragonruler1000/DEV

trying to fix problem with portal block.
This commit is contained in:
dragonruler1000 2025-05-15 11:07:25 -05:00 committed by GitHub
commit c43a75739f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 97 additions and 49 deletions

View file

@ -18,33 +18,54 @@ import java.util.function.Supplier;
import java.util.function.ToIntFunction; import java.util.function.ToIntFunction;
public class ModBlocks { public class ModBlocks {
public static ToIntFunction<BlockState> dustLightLevel = BlockState -> 10; // Dust Light Level // Function to determine the light level for the "dust" block
public static ToIntFunction<BlockState> PORTAL_LIGHT_LEVEL = BlockState ->15; public static ToIntFunction<BlockState> dustLightLevel = BlockState -> 10; // Fixed value of 10
// Function to set the light level for the "portal" block
public static ToIntFunction<BlockState> PORTAL_LIGHT_LEVEL = BlockState -> 15; // Fixed value of 15
// DeferredRegister to register blocks with Forge, using your mod ID
public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, Hdm_mod.MOD_ID); public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, Hdm_mod.MOD_ID);
// Registering a "block of dust" with specific properties
public static final RegistryObject<Block> DUST_BLOCK = registerBlock("block_of_dust", public static final RegistryObject<Block> DUST_BLOCK = registerBlock("block_of_dust",
() -> new Block(AbstractBlock.Properties.create(Material.ROCK).doesNotBlockMovement().harvestLevel(0) () -> new Block(
.hardnessAndResistance(500f, 100f).setLightLevel(dustLightLevel))); AbstractBlock.Properties.create(Material.ROCK) // Base material is rock
.doesNotBlockMovement() // Entities can move through this block
.harvestLevel(0) // Harvest level
.hardnessAndResistance(500f, 100f) // Hardness and blast resistance
.setLightLevel(dustLightLevel) // Provides light using the dustLightLevel function
)
);
// Registering a "portal" block called "window" with different properties
public static final RegistryObject<Block> PORTAL_BLOCK = registerBlock("window", public static final RegistryObject<Block> PORTAL_BLOCK = registerBlock("window",
() -> new Block(AbstractBlock.Properties.create(Material.PORTAL).doesNotBlockMovement().harvestLevel(10) () -> new Block(
.hardnessAndResistance(1000f, 1000f).setLightLevel(PORTAL_LIGHT_LEVEL))); AbstractBlock.Properties.create(Material.PORTAL) // Base material is portal
.doesNotBlockMovement() // Entities can move through this block
.harvestLevel(10) // High harvest level
.hardnessAndResistance(1000f, 1000f) // Very hard and blast resistant
.setLightLevel(PORTAL_LIGHT_LEVEL) // Maximum light level
)
);
// Helper method to register a block and its corresponding BlockItem at once
private static <T extends Block>RegistryObject<T> registerBlock(String name, Supplier<T> block) { private static <T extends Block> RegistryObject<T> registerBlock(String name, Supplier<T> block) {
RegistryObject<T> toReturn = BLOCKS.register(name, block); RegistryObject<T> toReturn = BLOCKS.register(name, block); // Register the block itself
registerBlockItem(name, toReturn); registerBlockItem(name, toReturn); // Register the block as an item
return toReturn; return toReturn;
} }
// Helper method to register the item form of a block, belonging to a custom item group
private static <T extends Block> void registerBlockItem(String name, RegistryObject<T> block) { private static <T extends Block> void registerBlockItem(String name, RegistryObject<T> block) {
ModItems.ITEMS.register(name, () -> new BlockItem(block.get(), ModItems.ITEMS.register(name, () -> new BlockItem(
new Item.Properties().group(ModItemGroup.HDM_BLOCK_GROUP))); block.get(),
new Item.Properties().group(ModItemGroup.HDM_BLOCK_GROUP) // Assigns to the HDM block group
));
} }
// This method is called to register all blocks with the mod event bus
public static void register(IEventBus eventBus){ public static void register(IEventBus eventBus){
BLOCKS.register(eventBus); BLOCKS.register(eventBus);
} }
} }

View file

@ -100,34 +100,31 @@ public class Window extends HorizontalBlock {
* @return Result of the interaction * @return Result of the interaction
*/ */
@Override @Override
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) { public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos,
String message = "blockActivated"; PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
ITextComponent msg = new StringTextComponent(message); // Client-side: Only show a status message
player.sendMessage(msg, player.getUniqueID()); if (worldIn.isRemote()) {
if (!worldIn.isRemote()) return super.onBlockActivated(state, worldIn, pos, player, handIn, hit); player.sendStatusMessage(new StringTextComponent("Client: Block activated!"), true);
if (player.isCrouching()) return super.onBlockActivated(state, worldIn, pos, player, handIn, hit); return ActionResultType.SUCCESS;
}
// Server-side logic below
player.sendMessage(new StringTextComponent("blockActivated"), player.getUniqueID());
// Prevent action if sneaking (crouching)
if (player.isCrouching()) {
return ActionResultType.PASS;
}
if (worldIn.getServer() == null) return super.onBlockActivated(state, worldIn, pos, player, handIn, hit);
MinecraftServer server = worldIn.getServer(); MinecraftServer server = worldIn.getServer();
if (worldIn.getDimensionKey() == ModDimensions.World1) { if (server == null) {
ServerWorld overWorld = server.getWorld(World.OVERWORLD); return ActionResultType.FAIL;
if (overWorld != null) {
player.changeDimension(overWorld, new SimpleTeleporter(pos, false));
} }
}
ServerWorld targetWorld;
boolean goingToCustom = worldIn.getDimensionKey() != ModDimensions.World1;
if (goingToCustom) { boolean goingToCustom = worldIn.getDimensionKey() != ModDimensions.World1;
targetWorld = server.getWorld(ModDimensions.World1); ServerWorld targetWorld = goingToCustom
} else { ? server.getWorld(ModDimensions.World1)
ServerWorld world1 = server.getWorld(ModDimensions.World1); : server.getWorld(World.OVERWORLD);
if (world1 != null) {
player.changeDimension(world1, new SimpleTeleporter(pos, true));
}
targetWorld = server.getWorld(World.OVERWORLD);
}
if (targetWorld != null) { if (targetWorld != null) {
SimpleTeleporter teleporter = new SimpleTeleporter(pos, goingToCustom); SimpleTeleporter teleporter = new SimpleTeleporter(pos, goingToCustom);
@ -142,7 +139,7 @@ public class Window extends HorizontalBlock {
return ActionResultType.SUCCESS; return ActionResultType.SUCCESS;
} }
return super.onBlockActivated(state, worldIn, pos, player, handIn, hit); return ActionResultType.FAIL;
} }

View file

@ -1,7 +1,5 @@
package us.minecraftchest2.hdm_mod.item.custom; package us.minecraftchest2.hdm_mod.item.custom;
import net.minecraft.block.Blocks;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
@ -9,13 +7,7 @@ import net.minecraft.item.ItemUseContext;
import net.minecraft.util.ActionResultType; import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction; import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.ScoreTextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.client.MinecraftForgeClient;
import us.minecraftchest2.hdm_mod.block.ModBlocks; import us.minecraftchest2.hdm_mod.block.ModBlocks;

View file

@ -1,5 +1,7 @@
package us.minecraftchest2.hdm_mod.world.dimension; package us.minecraftchest2.hdm_mod.world.dimension;
// Imports omitted for brevity
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.material.Material; import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
@ -10,27 +12,48 @@ import net.minecraftforge.common.util.ITeleporter;
import us.minecraftchest2.hdm_mod.block.ModBlocks; import us.minecraftchest2.hdm_mod.block.ModBlocks;
import us.minecraftchest2.hdm_mod.block.custom.Window; import us.minecraftchest2.hdm_mod.block.custom.Window;
import java.util.function.Function; import java.util.function.Function;
/**
* Handles teleportation of entities between dimensions, with custom logic for portals and safety.
*/
public class SimpleTeleporter implements ITeleporter { public class SimpleTeleporter implements ITeleporter {
// The destination position for the teleport
public static BlockPos thisPos = BlockPos.ZERO; public static BlockPos thisPos = BlockPos.ZERO;
// True if teleporting into the custom dimension, false if to overworld
public static boolean insideDimension = true; public static boolean insideDimension = true;
// Records if the most recent teleport was successful
private boolean success = false; private boolean success = false;
/**
* @return True if the last teleportation was successful (a safe landing spot found), false otherwise
*/
public boolean wasSuccessful() { public boolean wasSuccessful() {
return success; return success;
} }
/**
* Constructor sets the target position and whether we're inside our custom dimension.
* @param pos Destination position
* @param insideDim Are we inside the custom dimension after teleport?
*/
public SimpleTeleporter(BlockPos pos, boolean insideDim) { public SimpleTeleporter(BlockPos pos, boolean insideDim) {
thisPos = pos; thisPos = pos;
insideDimension = insideDim; insideDimension = insideDim;
} }
/**
* Checks if a block position is unsafe (solid, unreplaceable, lava, or fire).
* @param world The world to check in
* @param pos The position to check
* @return True if the position is unsafe, false if safe
*/
private boolean isUnsafe(ServerWorld world, BlockPos pos){ private boolean isUnsafe(ServerWorld world, BlockPos pos){
BlockState state = world.getBlockState(pos); BlockState state = world.getBlockState(pos);
Material material = state.getMaterial(); Material material = state.getMaterial();
// Not air AND not easily replaced (by water/etc) OR is lava or fire is considered unsafe
return material != Material.AIR && return material != Material.AIR &&
!state.isReplaceable(Fluids.WATER) && !state.isReplaceable(Fluids.WATER) &&
!material.isReplaceable() || !material.isReplaceable() ||
@ -38,32 +61,46 @@ public class SimpleTeleporter implements ITeleporter {
material == Material.FIRE; material == Material.FIRE;
} }
/**
* Handles the actual placing of the entity when teleporting between dimensions.
* Will try to find a safe spot upwards, up to 25 tries.
* Also, optionally places a portal block at the destination.
*/
@Override @Override
public Entity placeEntity(Entity entity, ServerWorld currentWorld, ServerWorld destinationWorld, public Entity placeEntity(Entity entity, ServerWorld currentWorld, ServerWorld destinationWorld,
float yaw, Function<Boolean, Entity> repositionEntity) { float yaw, Function<Boolean, Entity> repositionEntity) {
// Move entity to the origin position
entity = repositionEntity.apply(false); entity = repositionEntity.apply(false);
// Default target y coordinate for the destination
double y = 61; double y = 61;
// If not going inside the custom dimension, use the saved Y
if (!insideDimension) { if (!insideDimension) {
y = thisPos.getY(); y = thisPos.getY();
} }
// Calculate the intended destination block position
BlockPos destinationPos = new BlockPos(thisPos.getX(), y, thisPos.getZ()); BlockPos destinationPos = new BlockPos(thisPos.getX(), y, thisPos.getZ());
// Try to find a safe spot (air, replaceable, not lava/fire) by moving up, max 25 attempts
int tries = 0; int tries = 0;
while ((isUnsafe(destinationWorld, destinationPos) || isUnsafe(destinationWorld, destinationPos.up())) && tries < 25) { while ((isUnsafe(destinationWorld, destinationPos) || isUnsafe(destinationWorld, destinationPos.up())) && tries < 25) {
destinationPos = destinationPos.up(2); destinationPos = destinationPos.up(2);
tries++; tries++;
} }
// If it couldn't find a safe spot after 25 attempts, fail teleport
if (tries >= 25) { if (tries >= 25) {
this.success = false; this.success = false;
return entity; return entity;
} }
// Move entity to found safe location
entity.setPositionAndUpdate(destinationPos.getX(), destinationPos.getY(), destinationPos.getZ()); entity.setPositionAndUpdate(destinationPos.getX(), destinationPos.getY(), destinationPos.getZ());
this.success = true; this.success = true;
// When entering a custom dimension, make a portal block at the destination unless one already exists nearby
if (insideDimension) { if (insideDimension) {
boolean doSetBlock = true; boolean doSetBlock = true;
for (BlockPos checkPos : BlockPos.getAllInBoxMutable(destinationPos.down(10).west(10), destinationPos.up(10).east(10))) { for (BlockPos checkPos : BlockPos.getAllInBoxMutable(destinationPos.down(10).west(10), destinationPos.up(10).east(10))) {

View file

@ -5,5 +5,6 @@
"item.hdm_mod.subtle_knife": "Subtle Knife", "item.hdm_mod.subtle_knife": "Subtle Knife",
"item.hdm_mod.omelet": "Omelet", "item.hdm_mod.omelet": "Omelet",
"item.hdm_mod.dust": "Dust", "item.hdm_mod.dust": "Dust",
"block.hdm_mod.block_of_dust": "Block of Dust" "block.hdm_mod.block_of_dust": "Block of Dust",
"block.hdm_mod.window": "Window"
} }