Move to more robust placement system, fixes #280

This commit is contained in:
Raoul Van den Berge
2016-08-23 22:05:29 +02:00
parent 119757b328
commit 59d5f95063
11 changed files with 41 additions and 36 deletions

View File

@@ -5,7 +5,6 @@ import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.Item;
@@ -56,7 +55,7 @@ public abstract class BlockBase extends Block {
}
public Item createItem() {
return new ItemBlockBase(this, false);
return new ItemBlockBase(this, getPlacementType(), false);
}
@Override
@@ -98,26 +97,6 @@ public abstract class BlockBase extends Block {
return false;
}
@Override
public IBlockState onBlockPlaced(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase entity) {
IBlockState state = super.onBlockPlaced(world, pos, facing, hitX, hitY, hitZ, meta, entity);
if (getPlacementType() != null) {
return state.withProperty(DIRECTION, getPlacementType().getFrom(facing, pos, entity));
}
return state;
}
@Override
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase player, ItemStack stack) {
super.onBlockPlacedBy(world, pos, state, player, stack);
if (getPlacementType() != null) {
((TileBase) world.getTileEntity(pos)).setDirection(state.getValue(DIRECTION));
}
}
@Override
public void breakBlock(World world, BlockPos pos, IBlockState state) {
TileEntity tile = world.getTileEntity(pos);
@@ -147,7 +126,7 @@ public abstract class BlockBase extends Block {
world.setBlockToAir(pos);
}
protected EnumPlacementType getPlacementType() {
public EnumPlacementType getPlacementType() {
return EnumPlacementType.HORIZONTAL;
}
}

View File

@@ -233,7 +233,7 @@ public class BlockCable extends BlockCoverable {
return false;
}
protected EnumPlacementType getPlacementType() {
public EnumPlacementType getPlacementType() {
return null;
}

View File

@@ -15,6 +15,7 @@ import refinedstorage.RefinedStorage;
import refinedstorage.RefinedStorageGui;
import refinedstorage.tile.TileDiskDrive;
// @TODO: Fix bug where it doesn't display correctly
public class BlockDiskDrive extends BlockNode {
private static final PropertyInteger STORED = PropertyInteger.create("stored", 0, 7);

View File

@@ -73,6 +73,6 @@ public class BlockGrid extends BlockNode {
@Override
public Item createItem() {
return new ItemBlockBase(this, true);
return new ItemBlockBase(this, getPlacementType(), true);
}
}

View File

@@ -25,7 +25,7 @@ public enum EnumPlacementType {
this.allowed = allowed;
}
EnumFacing getFrom(EnumFacing facing, BlockPos pos, EntityLivingBase entity) {
public EnumFacing getFrom(EnumFacing facing, BlockPos pos, EntityLivingBase entity) {
switch (this) {
case ANY:
return facing.getOpposite();
@@ -38,7 +38,7 @@ public enum EnumPlacementType {
}
}
EnumFacing getNext(EnumFacing previous) {
public EnumFacing getNext(EnumFacing previous) {
switch (this) {
case ANY:
case ANY_FACE_PLAYER:

View File

@@ -1,15 +1,27 @@
package refinedstorage.item;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import refinedstorage.block.EnumPlacementType;
import refinedstorage.tile.TileBase;
public class ItemBlockBase extends ItemBlock {
public ItemBlockBase(Block block, boolean subtypes) {
private EnumPlacementType placementType;
public ItemBlockBase(Block block, EnumPlacementType placementType, boolean subtypes) {
super(block);
setRegistryName(block.getRegistryName());
this.placementType = placementType;
if (subtypes) {
setMaxDamage(0);
setHasSubtypes(true);
@@ -29,4 +41,18 @@ public class ItemBlockBase extends ItemBlock {
return getUnlocalizedName();
}
public boolean placeBlockAt(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, IBlockState newState) {
boolean result = super.placeBlockAt(stack, player, world, pos, side, hitX, hitY, hitZ, newState);
if (result && placementType != null) {
TileEntity tile = world.getTileEntity(pos);
if (tile instanceof TileBase) {
((TileBase) tile).setDirection(placementType.getFrom(side, pos, player));
}
}
return result;
}
}

View File

@@ -14,7 +14,7 @@ import java.util.List;
public class ItemBlockController extends ItemBlockBase {
public ItemBlockController() {
super(RefinedStorageBlocks.CONTROLLER, true);
super(RefinedStorageBlocks.CONTROLLER, RefinedStorageBlocks.CONTROLLER.getPlacementType(), true);
}
@Override

View File

@@ -20,7 +20,7 @@ import java.util.List;
public class ItemBlockFluidStorage extends ItemBlockBase {
public ItemBlockFluidStorage() {
super(RefinedStorageBlocks.FLUID_STORAGE, true);
super(RefinedStorageBlocks.FLUID_STORAGE, RefinedStorageBlocks.FLUID_STORAGE.getPlacementType(), true);
}
@Override

View File

@@ -20,7 +20,7 @@ import java.util.List;
public class ItemBlockStorage extends ItemBlockBase {
public ItemBlockStorage() {
super(RefinedStorageBlocks.STORAGE, true);
super(RefinedStorageBlocks.STORAGE, RefinedStorageBlocks.STORAGE.getPlacementType(), true);
}
@Override

View File

@@ -35,7 +35,7 @@ public class ClientProxy extends CommonProxy {
@SubscribeEvent
public void onModelBake(ModelBakeEvent e) {
for (ModelResourceLocation model : e.getModelRegistry().getKeys()) {
for (BlockCable cable : cables) {
for (BlockCable cable : cableTypes) {
if (model.getResourceDomain().equals(RefinedStorage.ID) && model.getResourcePath().equals(cable.getName()) && !model.getVariant().equals("inventory")) {
e.getModelRegistry().putObject(model, new ModelMultipartContainer(e.getModelRegistry().getObject(model), input -> cable.canRenderInLayer(input)));
}

View File

@@ -4,7 +4,6 @@ import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
@@ -35,7 +34,7 @@ import java.util.ArrayList;
import java.util.List;
public class CommonProxy {
protected List<BlockCable> cables = new ArrayList<>();
protected List<BlockCable> cableTypes = new ArrayList<>();
public void preInit(FMLPreInitializationEvent e) {
RefinedStorageAPI.SOLDERER_REGISTRY = new SoldererRegistry();
@@ -627,9 +626,9 @@ public class CommonProxy {
private void registerBlock(BlockCable cable) {
GameRegistry.<Block>register(cable);
GameRegistry.register(new ItemBlock(cable).setRegistryName(cable.getRegistryName()));
GameRegistry.register(new ItemBlockBase(cable, cable.getPlacementType(), false));
cables.add(cable);
cableTypes.add(cable);
}
private void registerTile(Class<? extends TileBase> tile, String id) {