Add direction to disk drive blocks.

This commit is contained in:
raoulvdberge
2019-10-02 22:36:40 +02:00
parent 495cb3ecb3
commit d241ad7769
11 changed files with 105 additions and 75 deletions

View File

@@ -5,6 +5,7 @@ import com.raoulvdberge.refinedstorage.api.network.INetworkNodeVisitor;
import com.raoulvdberge.refinedstorage.api.network.node.INetworkNode;
import com.raoulvdberge.refinedstorage.api.util.Action;
import com.raoulvdberge.refinedstorage.apiimpl.API;
import com.raoulvdberge.refinedstorage.block.BaseBlock;
import com.raoulvdberge.refinedstorage.tile.config.RedstoneMode;
import com.raoulvdberge.refinedstorage.util.WorldUtils;
import net.minecraft.block.BlockState;
@@ -25,7 +26,6 @@ import java.util.UUID;
// TODO: getId: return a ResourceLocation.
public abstract class NetworkNode implements INetworkNode, INetworkNodeVisitor {
private static final String NBT_OWNER = "Owner";
private static final String NBT_DIRECTION = "Direction";
private static final String NBT_VERSION = "Version";
private static final int VERSION = 1;
@@ -75,10 +75,7 @@ public abstract class NetworkNode implements INetworkNode, INetworkNodeVisitor {
@Nonnull
@Override
public ItemStack getItemStack() {
BlockState state = world.getBlockState(pos);
// TODO: Fix.
return new ItemStack(Item.getItemFromBlock(state.getBlock()), 1);
return new ItemStack(Item.BLOCK_TO_ITEM.get(world.getBlockState(pos).getBlock()), 1);
}
@Override
@@ -167,7 +164,6 @@ public abstract class NetworkNode implements INetworkNode, INetworkNodeVisitor {
}
tag.putInt(NBT_VERSION, VERSION);
tag.putInt(NBT_DIRECTION, direction.ordinal());
writeConfiguration(tag);
@@ -185,10 +181,6 @@ public abstract class NetworkNode implements INetworkNode, INetworkNodeVisitor {
owner = tag.getUniqueId(NBT_OWNER);
}
if (tag.contains(NBT_DIRECTION)) {
direction = Direction.byIndex(tag.getInt(NBT_DIRECTION));
}
if (tag.contains(NBT_VERSION)) {
version = tag.getString(NBT_VERSION);
}
@@ -235,21 +227,17 @@ public abstract class NetworkNode implements INetworkNode, INetworkNodeVisitor {
}
public Direction getDirection() {
if (direction == null) {
BlockState state = world.getBlockState(pos);
if (state.getBlock() instanceof BaseBlock) {
direction = state.get(((BaseBlock) state.getBlock()).getDirection().getProperty());
}
}
return direction;
}
public void setDirection(Direction direction) {
this.direction = direction;
onDirectionChanged();
markDirty();
}
protected void onDirectionChanged() {
// NO OP
}
@Nullable
public IItemHandler getDrops() {
return null;

View File

@@ -0,0 +1,26 @@
package com.raoulvdberge.refinedstorage.block;
import com.raoulvdberge.refinedstorage.block.info.BlockDirection;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.state.StateContainer;
public abstract class BaseBlock extends Block {
public BaseBlock(Properties properties) {
super(properties);
}
public BlockDirection getDirection() {
return BlockDirection.NONE;
}
@Override
protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
super.fillStateContainer(builder);
BlockDirection dir = getDirection();
if (dir != BlockDirection.NONE) {
builder.add(dir.getProperty());
}
}
}

View File

@@ -5,7 +5,6 @@ import com.raoulvdberge.refinedstorage.block.info.IBlockInfo;
import com.raoulvdberge.refinedstorage.item.blockitem.ItemBlockBase;
import com.raoulvdberge.refinedstorage.render.IModelRegistration;
import com.raoulvdberge.refinedstorage.render.collision.CollisionGroup;
import com.raoulvdberge.refinedstorage.util.CollisionUtils;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.item.Item;
@@ -137,7 +136,7 @@ public abstract class BlockBase extends Block {
}
protected boolean canAccessGui(BlockState state, World world, BlockPos pos, float hitX, float hitY, float hitZ) {
for (CollisionGroup group : getCollisions(world.getTileEntity(pos), state)) {
/*for (CollisionGroup group : getCollisions(world.getTileEntity(pos), state)) {
if (group.canAccessGui()) {
for (AxisAlignedBB aabb : group.getItems()) {
if (CollisionUtils.isInBounds(aabb, hitX, hitY, hitZ)) {
@@ -145,7 +144,7 @@ public abstract class BlockBase extends Block {
}
}
}
}
}*/
return false;
}
@@ -158,27 +157,4 @@ public abstract class BlockBase extends Block {
super.onReplaced(state, worldIn, pos, newState, isMoving);
}
}
public List<CollisionGroup> getCollisions(TileEntity tile, BlockState state) {
return DEFAULT_COLLISION_GROUPS;
}
/* TODO
@Override
@SuppressWarnings("deprecation")
public void addCollisionBoxToList(BlockState state, World world, BlockPos pos, AxisAlignedBB entityBox, List<AxisAlignedBB> collidingBoxes, Entity entityIn, boolean isActualState) {
for (CollisionGroup group : getCollisions(world.getTileEntity(pos), this.getActualState(state, world, pos))) {
for (AxisAlignedBB aabb : group.getItems()) {
addCollisionBoxToList(pos, entityBox, collidingBoxes, aabb);
}
}
}
@Override
@SuppressWarnings("deprecation")
public RayTraceResult collisionRayTrace(BlockState state, World world, BlockPos pos, Vec3d start, Vec3d end) {
AdvancedRayTraceResult result = AdvancedRayTracer.rayTrace(pos, start, end, getCollisions(world.getTileEntity(pos), this.getActualState(state, world, pos)));
return result != null ? result.getHit() : null;
}*/
}

View File

@@ -1,6 +1,7 @@
package com.raoulvdberge.refinedstorage.block;
import com.raoulvdberge.refinedstorage.RS;
import com.raoulvdberge.refinedstorage.block.info.BlockDirection;
import com.raoulvdberge.refinedstorage.tile.DiskDriveTile;
import com.raoulvdberge.refinedstorage.util.BlockUtils;
import net.minecraft.block.BlockState;
@@ -18,6 +19,11 @@ public class DiskDriveBlock extends NodeBlock {
this.setRegistryName(RS.ID, "disk_drive");
}
@Override
public BlockDirection getDirection() {
return BlockDirection.HORIZONTAL;
}
@Override
public boolean hasTileEntity(BlockState state) {
return true;

View File

@@ -2,9 +2,8 @@ package com.raoulvdberge.refinedstorage.block;
import com.raoulvdberge.refinedstorage.RS;
import com.raoulvdberge.refinedstorage.util.BlockUtils;
import net.minecraft.block.Block;
public class MachineCasingBlock extends Block {
public class MachineCasingBlock extends BaseBlock {
public MachineCasingBlock() {
super(BlockUtils.DEFAULT_ROCK_PROPERTIES);

View File

@@ -12,7 +12,7 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;
import net.minecraft.world.server.ServerWorld;
public abstract class NodeBlock extends Block {
public abstract class NodeBlock extends BaseBlock {
public static final BooleanProperty CONNECTED = BooleanProperty.create("connected");
public NodeBlock(Block.Properties props) {

View File

@@ -2,9 +2,8 @@ package com.raoulvdberge.refinedstorage.block;
import com.raoulvdberge.refinedstorage.RS;
import com.raoulvdberge.refinedstorage.util.BlockUtils;
import net.minecraft.block.Block;
public class QuartzEnrichedIronBlock extends Block {
public class QuartzEnrichedIronBlock extends BaseBlock {
public QuartzEnrichedIronBlock() {
super(BlockUtils.DEFAULT_ROCK_PROPERTIES);

View File

@@ -9,6 +9,7 @@ import net.minecraft.util.math.BlockPos;
import java.util.Arrays;
public enum BlockDirection {
NONE(),
ANY(Direction.values()),
ANY_FACE_PLAYER(Direction.values()),
HORIZONTAL(Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST);
@@ -32,7 +33,7 @@ public enum BlockDirection {
case HORIZONTAL:
return entity.getHorizontalFacing().getOpposite();
default:
return null;
throw new RuntimeException("Unknown direction type");
}
}

View File

@@ -0,0 +1,31 @@
package com.raoulvdberge.refinedstorage.item.blockitem;
import com.raoulvdberge.refinedstorage.block.BaseBlock;
import net.minecraft.block.BlockState;
import net.minecraft.item.BlockItem;
import net.minecraft.item.BlockItemUseContext;
public class BaseBlockItem extends BlockItem {
private final BaseBlock block;
public BaseBlockItem(BaseBlock block, Properties builder) {
super(block, builder);
this.block = block;
}
@Override
protected boolean placeBlock(BlockItemUseContext context, BlockState state) {
boolean result = super.placeBlock(context, state);
if (result && block.getDirection() != null) {
context.getWorld().setBlockState(context.getPos(), state.with(block.getDirection().getProperty(), block.getDirection().getFrom(
context.getFace(),
context.getPos(),
context.getPlayer()
)));
}
return result;
}
}

View File

@@ -3,6 +3,7 @@ package com.raoulvdberge.refinedstorage.render.model.baked;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.raoulvdberge.refinedstorage.RSBlocks;
import com.raoulvdberge.refinedstorage.render.constants.ConstantsDisk;
import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.model.BakedQuad;
@@ -26,6 +27,7 @@ public class DiskDriveBakedModel extends DelegateBakedModel {
this.state = state;
this.side = side;
this.diskState = diskState;
this.random = random;
}
@Override
@@ -60,20 +62,19 @@ public class DiskDriveBakedModel extends DelegateBakedModel {
}
}
private Map<Direction, IBakedModel> models = new HashMap<>();
private Map<Direction, Map<Integer, List<IBakedModel>>> disks = new HashMap<>();
private Map<Direction, IBakedModel> baseByFacing = new HashMap<>();
private Map<Direction, Map<Integer, List<IBakedModel>>> disksByFacing = new HashMap<>();
private LoadingCache<CacheKey, List<BakedQuad>> cache = CacheBuilder.newBuilder().build(new CacheLoader<CacheKey, List<BakedQuad>>() {
@Override
public List<BakedQuad> load(CacheKey key) {
// TODO Direction facing = key.state.get(RSBlocks.DISK_DRIVE.getDirection().getProperty());
Direction facing = Direction.NORTH;
Direction facing = key.state.get(RSBlocks.DISK_DRIVE.getDirection().getProperty());
List<BakedQuad> quads = new ArrayList<>(models.get(facing).getQuads(key.state, key.side, key.random));
List<BakedQuad> quads = new ArrayList<>(baseByFacing.get(facing).getQuads(key.state, key.side, key.random));
for (int i = 0; i < 8; ++i) {
if (key.diskState[i] != ConstantsDisk.DISK_STATE_NONE) {
quads.addAll(disks.get(facing).get(key.diskState[i]).get(i).getQuads(key.state, key.side, key.random));
quads.addAll(disksByFacing.get(facing).get(key.diskState[i]).get(i).getQuads(key.state, key.side, key.random));
}
}
@@ -88,24 +89,25 @@ public class DiskDriveBakedModel extends DelegateBakedModel {
IBakedModel diskDisconnected) {
super(base);
for (Direction facing : Direction.values()) { // TODO only horizontals
models.put(facing, new TRSRBakedModel(base, facing));
for (Direction facing : Direction.values()) {
if (facing.getHorizontalIndex() == -1) {
continue;
}
disks.put(facing, new HashMap<>());
baseByFacing.put(facing, new TRSRBakedModel(base, facing));
disks.get(facing).put(ConstantsDisk.DISK_STATE_NORMAL, new ArrayList<>());
disks.get(facing).put(ConstantsDisk.DISK_STATE_NEAR_CAPACITY, new ArrayList<>());
disks.get(facing).put(ConstantsDisk.DISK_STATE_FULL, new ArrayList<>());
disks.get(facing).put(ConstantsDisk.DISK_STATE_DISCONNECTED, new ArrayList<>());
disksByFacing.put(facing, new HashMap<>());
initDiskModels(disk, ConstantsDisk.DISK_STATE_NORMAL, facing);
initDiskModels(diskNearCapacity, ConstantsDisk.DISK_STATE_NEAR_CAPACITY, facing);
initDiskModels(diskFull, ConstantsDisk.DISK_STATE_FULL, facing);
initDiskModels(diskDisconnected, ConstantsDisk.DISK_STATE_DISCONNECTED, facing);
addDiskModels(disk, ConstantsDisk.DISK_STATE_NORMAL, facing);
addDiskModels(diskNearCapacity, ConstantsDisk.DISK_STATE_NEAR_CAPACITY, facing);
addDiskModels(diskFull, ConstantsDisk.DISK_STATE_FULL, facing);
addDiskModels(diskDisconnected, ConstantsDisk.DISK_STATE_DISCONNECTED, facing);
}
}
private void initDiskModels(IBakedModel disk, int type, Direction facing) {
private void addDiskModels(IBakedModel disk, int type, Direction facing) {
disksByFacing.get(facing).put(type, new ArrayList<>());
for (int y = 0; y < 4; ++y) {
for (int x = 0; x < 2; ++x) {
TRSRBakedModel model = new TRSRBakedModel(disk, facing);
@@ -122,7 +124,7 @@ public class DiskDriveBakedModel extends DelegateBakedModel {
model.transformation = new TRSRTransformation(trans, model.transformation.getLeftRot(), model.transformation.getScale(), model.transformation.getRightRot());
disks.get(facing).get(type).add(model);
disksByFacing.get(facing).get(type).add(model);
}
}
}

View File

@@ -1,6 +1,8 @@
package com.raoulvdberge.refinedstorage.util;
import com.raoulvdberge.refinedstorage.RS;
import com.raoulvdberge.refinedstorage.block.BaseBlock;
import com.raoulvdberge.refinedstorage.item.blockitem.BaseBlockItem;
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
@@ -10,8 +12,8 @@ import net.minecraft.item.Item;
public class BlockUtils {
public static final Block.Properties DEFAULT_ROCK_PROPERTIES = Block.Properties.create(Material.ROCK).hardnessAndResistance(1.9F).sound(SoundType.STONE);
public static BlockItem createBlockItemFor(Block block) {
BlockItem blockItem = new BlockItem(block, new Item.Properties().group(RS.MAIN_GROUP));
public static BlockItem createBlockItemFor(BaseBlock block) {
BaseBlockItem blockItem = new BaseBlockItem(block, new Item.Properties().group(RS.MAIN_GROUP));
blockItem.setRegistryName(block.getRegistryName());