mirror of
https://github.com/dragonruler1000/hdm-mod.git
synced 2025-06-29 08:29:33 -05:00
Refactor Window block interactions and register new block
Simplified the onBlockActivated logic for clarity and better separation of client and server behavior. Added null checks, improved crouching interaction handling, and adjusted dimension change logic. Registered the "window" block and updated the language file to include its name. Added comments so i can know what is going on later.
This commit is contained in:
parent
249c235220
commit
748ab1b988
3 changed files with 59 additions and 41 deletions
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -100,35 +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()) {
|
||||||
player.sendStatusMessage(new StringTextComponent("Client: Block activated!"), true);
|
player.sendStatusMessage(new StringTextComponent("Client: Block activated!"), true);
|
||||||
if (!worldIn.isRemote()) return super.onBlockActivated(state, worldIn, pos, player, handIn, hit);
|
return ActionResultType.SUCCESS;
|
||||||
if (player.isCrouching()) return super.onBlockActivated(state, worldIn, pos, player, handIn, hit);
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
@ -143,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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"
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue