Matter cables

This commit is contained in:
DBotThePony 2021-08-15 20:00:08 +07:00
parent 71e83874c0
commit 61add16d1e
Signed by: DBot
GPG Key ID: DCC23B5715498507
8 changed files with 422 additions and 5 deletions

View File

@ -25,6 +25,10 @@ public interface IMatterGridCell {
return connectOrCreateMatterGrid(pos, level, false);
}
default void onNeighbourMatterCell(BlockPos pos, Level level, Direction direction, IMatterGridCell cell) {
}
default boolean connectOrCreateMatterGrid(BlockPos pos, Level level, boolean force) {
if (getMatterGrid() != null && !force)
return true;
@ -54,6 +58,9 @@ public interface IMatterGridCell {
grid.mergeWith(getMatterGrid());
}
}
if (cell.isValidMatterCell())
onNeighbourMatterCell(pos, level, direction, cell);
}
}

View File

@ -15,10 +15,7 @@ import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import ru.dbotthepony.mc.otm.block.BlockAndroidStation;
import ru.dbotthepony.mc.otm.block.BlockBatteryBank;
import ru.dbotthepony.mc.otm.block.BlockMatterCapacitorBank;
import ru.dbotthepony.mc.otm.block.BlockMatterDecomposer;
import ru.dbotthepony.mc.otm.block.*;
import ru.dbotthepony.mc.otm.block.entity.BlockEntityAndroidStation;
import ru.dbotthepony.mc.otm.block.entity.BlockEntityBatteryBank;
import ru.dbotthepony.mc.otm.block.entity.BlockEntityMatterCapacitorBank;
@ -45,6 +42,7 @@ public class Registry {
public static final ResourceLocation BATTERY_BANK = new ResourceLocation(OverdriveThatMatters.MOD_ID, "battery_bank");
public static final ResourceLocation MATTER_DECOMPOSER = new ResourceLocation(OverdriveThatMatters.MOD_ID, "matter_decomposer");
public static final ResourceLocation MATTER_CAPACITOR_BANK = new ResourceLocation(OverdriveThatMatters.MOD_ID, "matter_capacitor_bank");
public static final ResourceLocation MATTER_CABLE = new ResourceLocation(OverdriveThatMatters.MOD_ID, "matter_cable");
public static final ResourceLocation ANDROID_CAPABILITY = new ResourceLocation(OverdriveThatMatters.MOD_ID, "android_capability");
@ -67,12 +65,14 @@ public class Registry {
public static final Block BATTERY_BANK = new BlockBatteryBank();
public static final Block MATTER_DECOMPOSER = new BlockMatterDecomposer();
public static final Block MATTER_CAPACITOR_BANK = new BlockMatterCapacitorBank();
public static final Block MATTER_CABLE = new BlockMatterCable();
static {
ANDROID_STATION.setRegistryName(Names.ANDROID_STATION);
BATTERY_BANK.setRegistryName(Names.BATTERY_BANK);
MATTER_DECOMPOSER.setRegistryName(Names.MATTER_DECOMPOSER);
MATTER_CAPACITOR_BANK.setRegistryName(Names.MATTER_CAPACITOR_BANK);
MATTER_CABLE.setRegistryName(Names.MATTER_CABLE);
}
public static void register(final RegistryEvent.Register<Block> event) {
@ -80,6 +80,7 @@ public class Registry {
event.getRegistry().register(BATTERY_BANK);
event.getRegistry().register(MATTER_DECOMPOSER);
event.getRegistry().register(MATTER_CAPACITOR_BANK);
event.getRegistry().register(MATTER_CABLE);
// OverdriveThatMatters.LOGGER.info("Registered blocks");
}
@ -90,6 +91,7 @@ public class Registry {
public static final Item BATTERY_BANK = new BlockItem(Blocks.BATTERY_BANK, new Item.Properties().stacksTo(64).tab(CreativeModeTab.TAB_MISC));
public static final Item MATTER_DECOMPOSER = new BlockItem(Blocks.MATTER_DECOMPOSER, new Item.Properties().stacksTo(64).tab(CreativeModeTab.TAB_MISC));
public static final Item MATTER_CAPACITOR_BANK = new BlockItem(Blocks.MATTER_CAPACITOR_BANK, new Item.Properties().stacksTo(64).tab(CreativeModeTab.TAB_MISC));
public static final Item MATTER_CABLE = new BlockItem(Blocks.MATTER_CABLE, new Item.Properties().stacksTo(64).tab(CreativeModeTab.TAB_MISC));
public static final ItemPill PILL_ANDROID = new ItemPill(ItemPill.PillType.BECOME_ANDROID);
public static final ItemPill PILL_HUMANE = new ItemPill(ItemPill.PillType.BECOME_HUMANE);
@ -109,6 +111,7 @@ public class Registry {
BATTERY_BANK.setRegistryName(Names.BATTERY_BANK);
MATTER_DECOMPOSER.setRegistryName(Names.MATTER_DECOMPOSER);
MATTER_CAPACITOR_BANK.setRegistryName(Names.MATTER_CAPACITOR_BANK);
MATTER_CABLE.setRegistryName(Names.MATTER_CABLE);
PILL_ANDROID.setRegistryName(Names.PILL_ANDROID);
PILL_HUMANE.setRegistryName(Names.PILL_HUMANE);
@ -128,6 +131,7 @@ public class Registry {
event.getRegistry().register(BATTERY_BANK);
event.getRegistry().register(MATTER_DECOMPOSER);
event.getRegistry().register(MATTER_CAPACITOR_BANK);
event.getRegistry().register(MATTER_CABLE);
event.getRegistry().register(PILL_ANDROID);
event.getRegistry().register(PILL_HUMANE);
@ -150,12 +154,14 @@ public class Registry {
public static BlockEntityType<BlockEntityBatteryBank> BATTERY_BANK = BlockEntityType.Builder.of(BlockEntityBatteryBank::new, Blocks.BATTERY_BANK).build(null);
public static BlockEntityType<BlockEntityMatterDecomposer> MATTER_DECOMPOSER = BlockEntityType.Builder.of(BlockEntityMatterDecomposer::new, Blocks.MATTER_DECOMPOSER).build(null);
public static BlockEntityType<BlockEntityMatterCapacitorBank> MATTER_CAPACITOR_BANK = BlockEntityType.Builder.of(BlockEntityMatterCapacitorBank::new, Blocks.MATTER_CAPACITOR_BANK).build(null);
public static BlockEntityType<BlockMatterCable.BlockEntityMatterCable> MATTER_CABLE = BlockEntityType.Builder.of(BlockMatterCable.BlockEntityMatterCable::new, Blocks.MATTER_CABLE).build(null);
static {
ANDROID_STATION.setRegistryName(Names.ANDROID_STATION);
BATTERY_BANK.setRegistryName(Names.BATTERY_BANK);
MATTER_DECOMPOSER.setRegistryName(Names.MATTER_DECOMPOSER);
MATTER_CAPACITOR_BANK.setRegistryName(Names.MATTER_CAPACITOR_BANK);
MATTER_CABLE.setRegistryName(Names.MATTER_CABLE);
}
public static void register(final RegistryEvent.Register<BlockEntityType<?>> event) {
@ -163,6 +169,7 @@ public class Registry {
event.getRegistry().register(BATTERY_BANK);
event.getRegistry().register(MATTER_DECOMPOSER);
event.getRegistry().register(MATTER_CAPACITOR_BANK);
event.getRegistry().register(MATTER_CABLE);
// OverdriveThatMatters.LOGGER.info("Registered block entities");
}

View File

@ -0,0 +1,294 @@
package ru.dbotthepony.mc.otm.block;
import com.google.common.collect.ImmutableMap;
import com.mojang.math.Matrix4f;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.material.MaterialColor;
import net.minecraft.world.phys.shapes.BooleanOp;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import ru.dbotthepony.mc.otm.IMatterGridCell;
import ru.dbotthepony.mc.otm.MatterGrid;
import ru.dbotthepony.mc.otm.Registry;
import ru.dbotthepony.mc.otm.capability.IMatterHandler;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
public class BlockMatterCable extends Block implements EntityBlock {
@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) {
return new BlockEntityMatterCable(blockPos, blockState);
}
public static class BlockEntityMatterCable extends BlockEntity implements IMatterGridCell {
public BlockEntityMatterCable(BlockPos p_155229_, BlockState p_155230_) {
super(Registry.BlockEntities.MATTER_CABLE, p_155229_, p_155230_);
// OverdriveThatMatters.LOGGER.debug("create cable block entity {} {} {}", this, p_155229_, p_155230_);
}
private MatterGrid grid;
@Override
public void setLevel(Level p_155231_) {
super.setLevel(p_155231_);
if (grid == null)
MatterGrid.scheduleDiscoverNeighbours(this, getBlockPos(), p_155231_);
}
@Override
public void onNeighbourMatterCell(BlockPos pos, Level level, Direction direction, IMatterGridCell cell) {
// OverdriveThatMatters.LOGGER.debug("Try to connect cable entity {} {} to {} {} ({})", this, pos, pos.offset(direction.getNormal()), direction, cell);
BlockState new_state = getBlockState().setValue(MAPPING_CONNECTION_PROP[direction.ordinal()], true);
if (new_state != getBlockState())
level.setBlock(getBlockPos(), new_state, UPDATE_CLIENTS);
}
@Override
public void setRemoved() {
super.setRemoved();
if (grid != null)
grid.untrack(this);
}
@Nullable
@Override
public MatterGrid getMatterGrid() {
return grid;
}
@Nullable
@Override
public IMatterHandler getMatterHandler() {
return null;
}
@Override
public boolean isValidMatterCell() {
return !isRemoved();
}
@Override
public void setMatterGrid(MatterGrid grid) {
this.grid = grid;
}
}
public static final BooleanProperty CONNECTION_SOUTH = BooleanProperty.create("connect_south");
public static final BooleanProperty CONNECTION_WEST = BooleanProperty.create("connect_west");
public static final BooleanProperty CONNECTION_EAST = BooleanProperty.create("connect_east");
public static final BooleanProperty CONNECTION_NORTH = BooleanProperty.create("connect_north");
public static final BooleanProperty CONNECTION_UP = BooleanProperty.create("connect_up");
public static final BooleanProperty CONNECTION_DOWN = BooleanProperty.create("connect_down");
public static final BooleanProperty[] MAPPING_CONNECTION_PROP = new BooleanProperty[] {
CONNECTION_DOWN,
CONNECTION_UP,
CONNECTION_NORTH,
CONNECTION_SOUTH,
CONNECTION_WEST,
CONNECTION_EAST
};
protected ImmutableMap<BlockState, VoxelShape> SHAPES;
public BlockMatterCable() {
super(BlockBehaviour.Properties.of(Material.STONE, MaterialColor.METAL).requiresCorrectToolForDrops().strength(1.0F, 6.0F));
registerDefaultState(
this.defaultBlockState()
.setValue(CONNECTION_DOWN, false)
.setValue(CONNECTION_UP, false)
.setValue(CONNECTION_NORTH, false)
.setValue(CONNECTION_SOUTH, false)
.setValue(CONNECTION_WEST, false)
.setValue(CONNECTION_EAST, false)
);
SHAPES = getShapeForEachState((state) -> {
ArrayList<VoxelShape> shapes = new ArrayList<>();
if (state.getValue(CONNECTION_SOUTH)) {
shapes.add(Shapes.box(
0.5 - 0.15,
0.5 - 0.15,
0.65,
0.5 + 0.15,
0.5 + 0.15,
1
));
}
if (state.getValue(CONNECTION_NORTH)) {
shapes.add(Shapes.box(
0.5 - 0.15,
0.5 - 0.15,
0,
0.5 + 0.15,
0.5 + 0.15,
0.35
));
}
if (state.getValue(CONNECTION_DOWN)) {
shapes.add(Shapes.box(
0.5 - 0.15,
0,
0.5 - 0.15,
0.5 + 0.15,
0.5 - 0.15,
0.5 + 0.15
));
}
if (state.getValue(CONNECTION_UP)) {
shapes.add(Shapes.box(
0.5 - 0.15,
0.5 - 0.15,
0.5 - 0.15,
0.5 + 0.15,
1,
0.5 + 0.15
));
}
if (state.getValue(CONNECTION_EAST)) {
shapes.add(Shapes.box(
0.65,
0.5 - 0.15,
0.5 - 0.15,
1,
0.5 + 0.15,
0.5 + 0.15
));
}
if (state.getValue(CONNECTION_WEST)) {
shapes.add(Shapes.box(
0,
0.5 - 0.15,
0.5 - 0.15,
0.35,
0.5 + 0.15,
0.5 + 0.15
));
}
VoxelShape final_shape = CORE_SHAPE;
for (VoxelShape add_shape : shapes) {
final_shape = Shapes.joinUnoptimized(final_shape, add_shape, BooleanOp.OR);
}
return final_shape;
});
}
private final VoxelShape CORE_SHAPE = Shapes.box(
0.5 - 0.15,
0.5 - 0.15,
0.5 - 0.15,
0.5 + 0.15,
0.5 + 0.15,
0.5 + 0.15
);
@Override
public VoxelShape getShape(BlockState p_60555_, BlockGetter p_60556_, BlockPos p_60557_, CollisionContext p_60558_) {
VoxelShape get = SHAPES.get(p_60555_);
return get != null ? get : CORE_SHAPE;
}
@Override
public boolean hasDynamicShape() {
return true;
}
// doesn't work at all
/*
@Nullable
@Override
public BlockState getStateForPlacement(BlockPlaceContext p_49820_) {
Level level = p_49820_.getLevel();
if (level.isClientSide)
return this.defaultBlockState();
BlockState state = this.defaultBlockState();
for (int i = 0; i < MAPPING_CONNECTION_PROP.length; i++) {
OverdriveThatMatters.LOGGER.info("{} {} {}", Direction.values()[i], p_49820_.getClickedPos().offset(Direction.values()[i].getNormal()), level.getBlockEntity(p_49820_.getClickedPos().offset(p_49820_.getClickedFace().getNormal()).offset(Direction.values()[i].getNormal())));
if (
level.getBlockEntity(p_49820_.getClickedPos().offset(Direction.values()[i].getNormal())) instanceof IMatterGridCell cell && cell.isValidMatterCell()// ||
//level.getBlockState(p_49820_.getClickedPos().offset(p_49820_.getClickedFace().getNormal()).offset(Direction.values()[i].getNormal())).getBlock() instanceof BlockMatterCable
) {
state.setValue(MAPPING_CONNECTION_PROP[i], true);
}
}
return state;
}
*/
// blocks
@Override
public void neighborChanged(BlockState self, Level level, BlockPos position_self, Block block_notifier, BlockPos position_notifier, boolean unknown_flag) {
Direction normal = Direction.fromNormal(position_notifier.subtract(position_self));
boolean updated = false;
BlockEntity get_entity = level.getBlockEntity(position_notifier);
if (!self.getValue(MAPPING_CONNECTION_PROP[normal.ordinal()]) && get_entity instanceof IMatterGridCell cell && cell.isValidMatterCell()) {
self = self.setValue(MAPPING_CONNECTION_PROP[normal.ordinal()], true);
updated = true;
} else if (self.getValue(MAPPING_CONNECTION_PROP[normal.ordinal()]) && (get_entity == null || get_entity instanceof IMatterGridCell cell && !cell.isValidMatterCell())) {
self = self.setValue(MAPPING_CONNECTION_PROP[normal.ordinal()], false);
updated = true;
}
if (updated) {
level.setBlock(position_self, self, UPDATE_CLIENTS);
}
super.neighborChanged(self, level, position_self, block_notifier, position_notifier, unknown_flag);
}
// tiles
/*@Override
public void onNeighborChange(BlockState state, LevelReader world, BlockPos pos, BlockPos neighbor) {
super.onNeighborChange(state, world, pos, neighbor);
}*/
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(CONNECTION_SOUTH,
CONNECTION_WEST,
CONNECTION_EAST,
CONNECTION_NORTH,
CONNECTION_UP,
CONNECTION_DOWN);
}
}

View File

@ -25,7 +25,7 @@ public abstract class BlockMattery extends Block {
}
public BlockMattery() {
this(BlockBehaviour.Properties.of(Material.STONE, MaterialColor.STONE).requiresCorrectToolForDrops().strength(1.5F, 6.0F));
this(BlockBehaviour.Properties.of(Material.STONE, MaterialColor.METAL).requiresCorrectToolForDrops().strength(1.5F, 25.0F));
}
@Override

View File

@ -0,0 +1,74 @@
{
"multipart": [
{
"apply": {
"model": "overdrive_that_matters:block/matter_cable_core"
}
},
{
"when": {
"connect_south": true
},
"apply": {
"model": "overdrive_that_matters:block/matter_cable_connection"
}
},
{
"when": {
"connect_west": true
},
"apply": {
"model": "overdrive_that_matters:block/matter_cable_connection",
"y": 90
}
},
{
"when": {
"connect_north": true
},
"apply": {
"model": "overdrive_that_matters:block/matter_cable_connection",
"y": 180
}
},
{
"when": {
"connect_east": true
},
"apply": {
"model": "overdrive_that_matters:block/matter_cable_connection",
"y": 270
}
},
{
"when": {
"connect_up": true
},
"apply": {
"model": "overdrive_that_matters:block/matter_cable_connection",
"x": 90
}
},
{
"when": {
"connect_down": true
},
"apply": {
"model": "overdrive_that_matters:block/matter_cable_connection",
"x": 270
}
}
]
}

View File

@ -0,0 +1,15 @@
{
"parent": "block/block",
"elements": [
{
"from": [ 6, 6, 10 ],
"to": [ 10, 10, 16 ],
"faces": {
"down": { "texture": "#connection" },
"up": { "texture": "#connection" },
"west": { "texture": "#connection" },
"east": { "texture": "#connection" }
}
}
]
}

View File

@ -0,0 +1,17 @@
{
"parent": "block/block",
"elements": [
{
"from": [ 6, 6, 6 ],
"to": [ 10, 10, 10 ],
"faces": {
"down": { "texture": "#core" },
"up": { "texture": "#core" },
"north": { "texture": "#core" },
"south": { "texture": "#core" },
"west": { "texture": "#core" },
"east": { "texture": "#core" }
}
}
]
}

View File

@ -0,0 +1,3 @@
{
"parent": "overdrive_that_matters:block/matter_cable_core"
}