Made MultiBlocks work!

-Added MultiBlock Base Class
-Added Check for valid Multiblock
This commit is contained in:
Ellpeck 2015-07-22 06:07:57 +02:00
parent 7701f33cf9
commit 55f4f07065
4 changed files with 289 additions and 0 deletions

View file

@ -0,0 +1,133 @@
package ellpeck.actuallyadditions.blocks;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import ellpeck.actuallyadditions.blocks.multi.IMultiBlock;
import ellpeck.actuallyadditions.blocks.multi.MultiBlockHelper;
import ellpeck.actuallyadditions.util.BlockUtil;
import ellpeck.actuallyadditions.util.INameableItem;
import ellpeck.actuallyadditions.util.ModUtil;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import java.util.List;
public class BlockOreFactory extends BlockContainerBase implements INameableItem, IMultiBlock{
public BlockOreFactory(){
super(Material.rock);
this.setHarvestLevel("pickaxe", 0);
this.setHardness(4.5F);
this.setResistance(20.0F);
this.setStepSound(soundTypeStone);
}
@Override
public TileEntity createNewTileEntity(World world, int par2){
return null;
}
@Override
public IIcon getIcon(int side, int meta){
return this.blockIcon;
}
@Override
@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister iconReg){
this.blockIcon = iconReg.registerIcon(ModUtil.MOD_ID_LOWER + ":" + this.getName());
}
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9){
if(!world.isRemote){
//TODO
return true;
}
return true;
}
@Override
public void onBlockAdded(World world, int x, int y, int z){
this.makeMultiBlock(world, x, y, z, world.getBlock(x, y, z));
}
@Override
public void onNeighborBlockChange(World world, int x, int y, int z, Block block){
this.makeMultiBlock(world, x, y, z, block);
}
private void makeMultiBlock(World world, int x, int y, int z, Block block){
if(block instanceof IMultiBlock){
MultiBlockHelper.createMultiBlock((IMultiBlock)block, world, x, y, z);
}
}
@Override
public String getName(){
return "blockOreFactory";
}
@Override
public Block[] getNeededBlocks(){
return new Block[]{this};
}
@Override
public TileEntity getCore(){
return null;
}
@Override
public int getSizeHor(){
return 3;
}
@Override
public int getSizeVer(){
return 5;
}
public static class TheItemBlock extends ItemBlock{
private Block theBlock;
public TheItemBlock(Block block){
super(block);
this.theBlock = block;
this.setHasSubtypes(false);
this.setMaxDamage(0);
}
@Override
public EnumRarity getRarity(ItemStack stack){
return EnumRarity.uncommon;
}
@Override
public String getUnlocalizedName(ItemStack stack){
return this.getUnlocalizedName();
}
@Override
@SuppressWarnings("unchecked")
@SideOnly(Side.CLIENT)
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean isHeld) {
BlockUtil.addInformation(theBlock, list, 3, "");
//TODO
}
@Override
public int getMetadata(int damage){
return damage;
}
}
}

View file

@ -82,6 +82,8 @@ public class InitBlocks{
public static Block blockXPSolidifier; public static Block blockXPSolidifier;
public static Block blockOreFactory;
public static void init(){ public static void init(){
ModUtil.LOGGER.info("Initializing Blocks..."); ModUtil.LOGGER.info("Initializing Blocks...");
@ -101,6 +103,9 @@ public class InitBlocks{
blockTestifiBucksWhiteSlab = new BlockSlabs("blockTestifiBucksWhiteSlab", blockTestifiBucksWhiteWall); blockTestifiBucksWhiteSlab = new BlockSlabs("blockTestifiBucksWhiteSlab", blockTestifiBucksWhiteWall);
BlockUtil.register(blockTestifiBucksWhiteSlab, BlockSlabs.TheItemBlock.class); BlockUtil.register(blockTestifiBucksWhiteSlab, BlockSlabs.TheItemBlock.class);
blockOreFactory = new BlockOreFactory();
BlockUtil.register(blockOreFactory, BlockOreFactory.TheItemBlock.class);
blockColoredLamp = new BlockColoredLamp(false); blockColoredLamp = new BlockColoredLamp(false);
BlockUtil.register(blockColoredLamp, BlockColoredLamp.TheItemBlock.class); BlockUtil.register(blockColoredLamp, BlockColoredLamp.TheItemBlock.class);
blockColoredLampOn = new BlockColoredLamp(true); blockColoredLampOn = new BlockColoredLamp(true);

View file

@ -0,0 +1,27 @@
package ellpeck.actuallyadditions.blocks.multi;
import net.minecraft.block.Block;
import net.minecraft.tileentity.TileEntity;
public interface IMultiBlock{
/**
* @return The types of Block the Multiblock can contain
*/
Block[] getNeededBlocks();
/**
* @return The Core TileEntity storing the MultiBlock's Data
*/
TileEntity getCore();
/**
* @return The horizontal size of the MultiBlock
*/
int getSizeHor();
/**
* @return The vertical size of the MultiBlock
*/
int getSizeVer();
}

View file

@ -0,0 +1,124 @@
package ellpeck.actuallyadditions.blocks.multi;
import ellpeck.actuallyadditions.util.WorldPos;
import net.minecraft.block.Block;
import net.minecraft.world.World;
import java.util.ArrayList;
public class MultiBlockHelper{
/**
* Checks if a MultiBlock can be created and creates it
* Made for rectangular MultiBlocks that don't need special Blocks in certain places
* @param block The MultiBlock Part the method is getting called from
* @return The Multiblock if it worked, or null if it didn't
*/
public static ArrayList<WorldPos> createMultiBlock(IMultiBlock block, World blockWorld, int blockX, int blockY, int blockZ){
if(blockWorld.isRemote) return null;
ArrayList<WorldPos> blocks = new ArrayList<WorldPos>();
int maxX = 0;
int minX = 0;
int maxZ = 0;
int minZ = 0;
int maxY = 0;
int minY = 0;
//Setting Min and Max Boundaries of the MultiBlock
//Horizontal X+
for(int i = blockX; i < blockX+block.getSizeHor(); i++){
if(!containsBlock(block.getNeededBlocks(), blockWorld.getBlock(i, blockY, blockZ))){
maxX = i-1;
break;
}
else maxX = i;
}
//Horizontal X-
for(int i = blockX; i >= blockX-block.getSizeHor(); i--){
if(!containsBlock(block.getNeededBlocks(), blockWorld.getBlock(i, blockY, blockZ))){
minX = i+1;
break;
}
else minX = i;
}
//Horizontal Z+
for(int i = blockZ; i < blockZ+block.getSizeHor(); i++){
if(!containsBlock(block.getNeededBlocks(), blockWorld.getBlock(blockX, blockY, i))){
maxZ = i-1;
break;
}
else maxZ = i;
}
//Horizontal Z-
for(int i = blockZ; i >= blockZ-block.getSizeHor(); i--){
if(!containsBlock(block.getNeededBlocks(), blockWorld.getBlock(blockX, blockY, i))){
minZ = i+1;
break;
}
else minZ = i;
}
//Horizontal Y+
for(int i = blockY; i < blockY+block.getSizeVer(); i++){
if(!containsBlock(block.getNeededBlocks(), blockWorld.getBlock(blockX, i, blockZ))){
maxY = i-1;
break;
}
else maxY = i;
}
//Horizontal Y-
for(int i = blockY; i >= blockY-block.getSizeVer(); i--){
if(!containsBlock(block.getNeededBlocks(), blockWorld.getBlock(blockX, i, blockZ))){
minY = i+1;
break;
}
else minY = i;
}
boolean failedOnce = false;
//Actually getting the MultiBlock
//For all of the coordinates gotten before, get the Blocks and store them in the list
for(int x = minX; x <= maxX; x++){
for(int y = minY; y <= maxY; y++){
for(int z = minZ; z <= maxZ; z++){
if(containsBlock(block.getNeededBlocks(), blockWorld.getBlock(x, y, z)) && maxX+1-minX >= block.getSizeHor() && maxZ+1-minZ >= block.getSizeHor() && maxY+1-minY >= block.getSizeVer()){
//Add the Block to the List to return
blocks.add(new WorldPos(blockWorld, x, y, z));
//Set the Block to MultiBlock-"State"
blockWorld.setBlockMetadataWithNotify(x, y, z, 1, 2);
}
else{
failedOnce = true;
blockWorld.setBlockMetadataWithNotify(x, y, z, 0, 2);
}
}
}
}
//Reset the Blocks if the Check failed at any point
if(failedOnce){
for(WorldPos aBlock : blocks){
if(containsBlock(block.getNeededBlocks(), aBlock.getBlock())){
aBlock.getWorld().setBlockMetadataWithNotify(aBlock.getX(), aBlock.getY(), aBlock.getZ(), 0, 2);
}
}
}
return failedOnce ? null : blocks;
}
private static boolean containsBlock(Block[] blocks, Block block){
for(Block aBlock : blocks){
if(aBlock.equals(block)) return true;
}
return false;
}
}