mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-20 20:22:28 +00:00
Let BaseRailBlock decide if the calculated RailShape for placement or connection to neighbors is valid (#8562)
This commit is contained in:
1 parent
9e6e789b73
commit
9db0d72232
8 files changed
+198
No files matched your search
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.debug.block;
|
||||
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.world.item.BlockItem;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.context.BlockPlaceContext;
|
||||
import net.minecraft.world.level.LevelReader;
|
||||
import net.minecraft.world.level.block.*;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.block.state.StateDefinition;
|
||||
import net.minecraft.world.level.block.state.properties.*;
|
||||
import net.minecraft.world.level.material.*;
|
||||
import net.minecraftforge.eventbus.api.IEventBus;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
||||
import net.minecraftforge.registries.*;
|
||||
|
||||
@Mod(ValidRailShapeTest.MOD_ID)
|
||||
public class ValidRailShapeTest
|
||||
{
|
||||
public static final String MOD_ID = "valid_railshape_test";
|
||||
public static final boolean ENABLED = true;
|
||||
|
||||
private static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MOD_ID);
|
||||
private static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MOD_ID);
|
||||
|
||||
private static final RegistryObject<Block> RAIL_SLOPE_BLOCK = BLOCKS.register("rail_slope", RailSlopeBlock::new);
|
||||
private static final RegistryObject<Item> RAIL_SLOPE_ITEM = ITEMS.register("rail_slope", () -> new BlockItem(RAIL_SLOPE_BLOCK.get(), new Item.Properties()));
|
||||
|
||||
public ValidRailShapeTest()
|
||||
{
|
||||
if (ENABLED)
|
||||
{
|
||||
IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
|
||||
BLOCKS.register(bus);
|
||||
ITEMS.register(bus);
|
||||
}
|
||||
}
|
||||
|
||||
private static class RailSlopeBlock extends BaseRailBlock
|
||||
{
|
||||
private static final EnumProperty<RailShape> ASCENDING_RAIL_SHAPE = EnumProperty.create("shape", RailShape.class, RailShape::isAscending);
|
||||
|
||||
protected RailSlopeBlock()
|
||||
{
|
||||
super(true, Properties.of(Material.DECORATION).noCollission().strength(0.7F).sound(SoundType.METAL));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder)
|
||||
{
|
||||
builder.add(ASCENDING_RAIL_SHAPE, BaseRailBlock.WATERLOGGED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockState getStateForPlacement(BlockPlaceContext context)
|
||||
{
|
||||
RailShape shape = switch (context.getHorizontalDirection())
|
||||
{
|
||||
case NORTH -> RailShape.ASCENDING_NORTH;
|
||||
case EAST -> RailShape.ASCENDING_EAST;
|
||||
case SOUTH -> RailShape.ASCENDING_SOUTH;
|
||||
case WEST -> RailShape.ASCENDING_WEST;
|
||||
default -> throw new IllegalArgumentException("Invalid facing " + context.getHorizontalDirection());
|
||||
};
|
||||
|
||||
FluidState fluid = context.getLevel().getFluidState(context.getClickedPos());
|
||||
|
||||
return defaultBlockState()
|
||||
.setValue(ASCENDING_RAIL_SHAPE, shape)
|
||||
.setValue(BaseRailBlock.WATERLOGGED, fluid.getType() == Fluids.WATER);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canSurvive(BlockState state, LevelReader level, BlockPos pos)
|
||||
{
|
||||
RailShape shape = state.getValue(ASCENDING_RAIL_SHAPE);
|
||||
Direction dir = switch (shape)
|
||||
{
|
||||
case ASCENDING_NORTH -> Direction.NORTH;
|
||||
case ASCENDING_EAST -> Direction.EAST;
|
||||
case ASCENDING_SOUTH -> Direction.SOUTH;
|
||||
case ASCENDING_WEST -> Direction.WEST;
|
||||
default -> throw new IllegalArgumentException("Invalid shape " + shape);
|
||||
};
|
||||
|
||||
if (!canSupportRigidBlock(level, pos.relative(dir)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return super.canSurvive(state, level, pos);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValidRailShape(RailShape shape)
|
||||
{
|
||||
return shape.isAscending();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Property<RailShape> getShapeProperty()
|
||||
{
|
||||
return ASCENDING_RAIL_SHAPE;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user