Résolu Bloc container
-
Bonjour,
J’ai créé un macérateur, et je voulais qu’il ai un état “allumé” et un état “éteind”. J’ai donc recherché dans les classes de Minecraft et du four et utilisé ce système (voir BlockCrusher.updateCrusherBlockState). Le problème, c’est que comme on remplace le bloc, le container est modifié et je me retrouve avec 1 item par slot (rempli auparavant) au lieu d’un nombre supérieur (en gros ça éjecte tout sauf 1 item dans chaque slot).
Auriez-vous une solution ?BlockCrusher.java :
package fr.MrBlockTNT.NewOres.Basic.crusher; import java.util.List; import java.util.Random; import net.minecraft.block.Block; import net.minecraft.client.renderer.texture.IIconRegister; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.IInventory; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.IIcon; import net.minecraft.util.MathHelper; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import cpw.mods.fml.common.network.internal.FMLNetworkHandler; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import fr.MrBlockTNT.NewOres.Basic.Basic; import fr.MrBlockTNT.NewOres.Basic.BasicBlock; public class BlockCrusher extends BasicBlock { public static String[] subBlock = new String[] {"copper", "bronze", "steel"}; public IIcon[][] iconArray = new IIcon[subBlock.length][3]; private static boolean field_149934_M; private boolean isActive = false; public BlockCrusher(boolean active) { this.isActive = active; } public void registerBlockIcons(IIconRegister iconRegister) { for(int i = 0; i < subBlock.length; i++) { this.iconArray*[0] = iconRegister.registerIcon("neworesbasic:" + subBlock* + "_crusher_top"); this.iconArray*[1] = iconRegister.registerIcon(this.isActive ? "neworesbasic:" + subBlock* + "_crusher_front_active" : "neworesbasic:" + subBlock* + "_crusher_front"); this.iconArray*[2] = iconRegister.registerIcon("neworesbasic:" + subBlock* + "_crusher_side"); } } public void getSubBlocks(Item item, CreativeTabs tabs, List list) { for(int i = 0; i < subBlock.length; i++) { list.add(new ItemStack(item, 1, i)); } } public int damageDropped(int metadata) { return metadata; } @SideOnly(Side.CLIENT) public IIcon getIcon(IBlockAccess world, int x, int y, int z, int side) { int metadata = world.getBlockMetadata(x, y, z); TileEntity tile = world.getTileEntity(x, y, z); if(side == 1) // UP { return this.iconArray[metadata][0]; } else if(tile instanceof TileEntityCrusher) { byte direction = ((TileEntityCrusher)tile).getDirection(); return side == 3 && direction == 0 ? this.iconArray[metadata][1] : (side == 4 && direction == 1 ? this.iconArray[metadata][1] : (side == 2 && direction == 2 ? this.iconArray[metadata][1] : (side == 5 && direction == 3 ? this.iconArray[metadata][1] : this.iconArray[metadata][2]))); } else { return this.iconArray[metadata][2]; } } @SideOnly(Side.CLIENT) public IIcon getIcon(int side, int metadata) { if(side == 1) // UP { return this.iconArray[metadata][0]; } else if(side == 3) // FRONT : 3 { return this.iconArray[metadata][1]; } else { return this.iconArray[metadata][2]; } } public BlockCrusher() { this.setCreativeTab(Basic.BasicTab); } @Override public TileEntity createTileEntity(World world, int metadata) { return new TileEntityCrusher(); } @Override public boolean hasTileEntity(int metadata) { return true; } public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9) { FMLNetworkHandler.openGui(player, Basic.instance, 0, world, x, y, z); return true; } public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase living, ItemStack stack) { TileEntity te = world.getTileEntity(x, y, z); if(te != null && te instanceof TileEntityCrusher && stack.hasDisplayName()) { ((TileEntityCrusher)te).setCustomGuiName(stack.getDisplayName()); } if(te instanceof TileEntityCrusher) { int direction = MathHelper.floor_double((double)(living.rotationYaw * 4.0F / 360.0F) + 2.5D) & 3; ((TileEntityCrusher)te).setDirection((byte)direction); } } public void breakBlock(World world, int x, int y, int z, Block block, int metadata) { dropContainerItem(world, x, y, z); super.breakBlock(world, x, y, z, block, metadata); } protected void dropContainerItem(World world, int x, int y, int z) { TileEntityCrusher tecrusher = (TileEntityCrusher)world.getTileEntity(x, y, z); if (tecrusher != null) { for (int slotId = 0; slotId < tecrusher.getSizeInventory(); slotId++) { ItemStack stack = tecrusher.getStackInSlot(slotId); if (stack != null) { float f = world.rand.nextFloat() * 0.8F + 0.1F; float f1 = world.rand.nextFloat() * 0.8F + 0.1F; EntityItem entityitem; for (float f2 = world.rand.nextFloat() * 0.8F + 0.1F; stack.stackSize > 0; world.spawnEntityInWorld(entityitem)) { int k1 = world.rand.nextInt(21) + 10; if (k1 > stack.stackSize) { k1 = stack.stackSize; } stack.stackSize -= k1; entityitem = new EntityItem(world, (double)((float)x + f), (double)((float)y + f1), (double)((float)z + f2), new ItemStack(stack.getItem(), k1, stack.getItemDamage())); float f3 = 0.05F; entityitem.motionX = (double)((float)world.rand.nextGaussian() * f3); entityitem.motionY = (double)((float)world.rand.nextGaussian() * f3 + 0.2F); entityitem.motionZ = (double)((float)world.rand.nextGaussian() * f3); if (stack.hasTagCompound()) { entityitem.getEntityItem().setTagCompound((NBTTagCompound)stack.getTagCompound().copy()); } } } } } } public static void updateCrusherBlockState(boolean p_149931_0_, World world, int x, int y, int z) { int l = world.getBlockMetadata(x, y, z); TileEntity tileentity = world.getTileEntity(x, y, z); field_149934_M = true; int metadata = world.getBlockMetadata(x, y, z); if (p_149931_0_) { world.setBlock(x, y, z, Basic.crusher_active, metadata, 0); // In-crushing block } else { world.setBlock(x, y, z, Basic.crusher, metadata, 0); } field_149934_M = false; world.setBlockMetadataWithNotify(x, y, z, l, 2); if (tileentity != null) { tileentity.validate(); world.setTileEntity(x, y, z, tileentity); } } public boolean hasComparatorInputOverride() { return true; } public int getComparatorInputOverride(World p_149736_1_, int p_149736_2_, int p_149736_3_, int p_149736_4_, int p_149736_5_) { return Container.calcRedstoneFromInventory((IInventory)p_149736_1_.getTileEntity(p_149736_2_, p_149736_3_, p_149736_4_)); } @SideOnly(Side.CLIENT) public void randomDisplayTick(World world, int x, int y, int z, Random rand) { if(isActive) { for(int i = 0; i < rand.nextInt(8); i++) { float f = (float)x + 0.4F + rand.nextFloat() * 6.0F / 16.0F; float f1 = (float)y + 1.1F; float f2 = (float)z + 0.4F; float f3 = 0.52F; float f4 = rand.nextFloat() * 0.6F - 0.3F; world.spawnParticle("smoke", (double)f, (double)f1, (double)(f2 + f4), 0.0D, 0.0D, 0.0D); } } } }
TileEntityCrusher.java :
package fr.MrBlockTNT.NewOres.Basic.crusher; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; import net.minecraft.init.Items; import net.minecraft.inventory.IInventory; import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemHoe; import net.minecraft.item.ItemStack; import net.minecraft.item.ItemSword; import net.minecraft.item.ItemTool; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.network.NetworkManager; import net.minecraft.network.Packet; import net.minecraft.network.play.server.S35PacketUpdateTileEntity; import net.minecraft.tileentity.TileEntity; import cpw.mods.fml.common.registry.GameRegistry; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; public class TileEntityCrusher extends TileEntity implements IInventory { private static final int[] slotsTop = new int[] {0}; private static final int[] slotsBottom = new int[] {2, 1}; private static final int[] slotsSides = new int[] {1}; private ItemStack[] inventory = new ItemStack[3]; private String customName; public int crusherCrushTime; public int currentItemPowerTime; public int crusherResultCrushTime; public int maxCrushTime = 200; public static float powerMultiplier = 1.3F; private byte direction; @Override public void readFromNBT(NBTTagCompound nbttag) { super.readFromNBT(nbttag); this.direction = nbttag.getByte("Direction"); NBTTagList nbttaglist = nbttag.getTagList("Items", 10); this.inventory = new ItemStack[this.getSizeInventory()]; for (int i = 0; i < nbttaglist.tagCount(); ++i) { NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i); byte b0 = nbttagcompound1.getByte("Slot"); if (b0 >= 0 && b0 < this.inventory.length) { this.inventory[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1); } } this.crusherCrushTime = nbttag.getShort("CrushTime"); this.crusherResultCrushTime = nbttag.getShort("CrushResultTime"); this.currentItemPowerTime = getItemPowerTime(this.inventory[1]); if (nbttag.hasKey("CustomName", 8)) { this.customName = nbttag.getString("CustomName"); } } @Override public void writeToNBT(NBTTagCompound nbttag) { super.writeToNBT(nbttag); nbttag.setByte("Direction", this.direction); nbttag.setShort("CrushTime", (short)this.crusherCrushTime); nbttag.setShort("CrushResultTime", (short)this.crusherResultCrushTime); NBTTagList nbttaglist = new NBTTagList(); for (int i = 0; i < this.inventory.length; ++i) { if (this.inventory* != null) { NBTTagCompound nbttagcompound1 = new NBTTagCompound(); nbttagcompound1.setByte("Slot", (byte)i); this.inventory*.writeToNBT(nbttagcompound1); nbttaglist.appendTag(nbttagcompound1); } } nbttag.setTag("Items", nbttaglist); if (this.hasCustomInventoryName()) { nbttag.setString("CustomName", this.customName); } } public byte getDirection() { return direction; } public void setDirection(byte direction) { this.direction = direction; this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord); } public Packet getDescriptionPacket() { NBTTagCompound nbttagcompound = new NBTTagCompound(); this.writeToNBT(nbttagcompound); return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 0, nbttagcompound); } public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) { this.readFromNBT(pkt.func_148857_g()); this.worldObj.markBlockRangeForRenderUpdate(this.xCoord, this.yCoord, this.zCoord, this.xCoord, this.yCoord, this.zCoord); } @Override public int getSizeInventory() { return inventory.length; } public ItemStack getStackInSlot(int par1) { return this.inventory[par1]; } @Override public ItemStack decrStackSize(int slotId, int quantity) { if (this.inventory[slotId] != null) { ItemStack itemstack; if (this.inventory[slotId].stackSize <= quantity) { itemstack = this.inventory[slotId]; this.inventory[slotId] = null; return itemstack; } else { itemstack = this.inventory[slotId].splitStack(quantity); if (this.inventory[slotId].stackSize == 0) { this.inventory[slotId] = null; } return itemstack; } } else { return null; } } @Override public ItemStack getStackInSlotOnClosing(int slotId) { if (this.inventory[slotId] != null) { ItemStack itemstack = this.inventory[slotId]; this.inventory[slotId] = null; return itemstack; } else { return null; } } @Override public void setInventorySlotContents(int slotId, ItemStack stack) { this.inventory[slotId] = stack; if (stack != null && stack.stackSize > this.getInventoryStackLimit()) { stack.stackSize = this.getInventoryStackLimit(); } this.markDirty(); } @Override public String getInventoryName() { return this.hasCustomInventoryName() ? this.customName : "container.crusher"; } @Override public boolean hasCustomInventoryName() { return this.customName != null && this.customName.length() > 0; } public void setCustomGuiName(String name) { this.customName = name; } @Override public int getInventoryStackLimit() { return 64; } @Override public boolean isUseableByPlayer(EntityPlayer player) { return worldObj.getTileEntity(xCoord, yCoord, zCoord) == this && player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64; } @Override public boolean isItemValidForSlot(int par1, ItemStack par2ItemStack) { return par1 == 2 ? false : (par1 == 1 ? isItemFuel(par2ItemStack) : true); } @Override public void openInventory() { } @Override public void closeInventory() { } @SideOnly(Side.CLIENT) public int getCrushProgressScaled(int speed) { return this.crusherResultCrushTime * speed/ this.maxCrushTime; } @SideOnly(Side.CLIENT) public int getCrushTimeRemainingScaled(int p_145955_1_) { if (this.currentItemPowerTime == 0) { this.currentItemPowerTime = this.maxCrushTime; } return this.crusherCrushTime * p_145955_1_ / this.currentItemPowerTime; } public boolean isCrushing() { return this.crusherCrushTime > 0; } public void updateEntity() { if(this.getBlockMetadata() == 0) { this.maxCrushTime = 200; this.powerMultiplier = 1.0F; } else if(this.getBlockMetadata() == 1) { this.maxCrushTime = 190; this.powerMultiplier = 1.1F; } else if(this.getBlockMetadata() == 2) { this.maxCrushTime = 150; this.powerMultiplier = 1.3F; } boolean flag = this.crusherCrushTime > 0; boolean flag1 = false; if (this.crusherCrushTime > 0) { –this.crusherCrushTime; } if (!this.worldObj.isRemote) { if (this.crusherCrushTime == 0 && this.canCrush()) { this.currentItemPowerTime = this.crusherCrushTime = getItemPowerTime(this.inventory[1]); if (this.crusherCrushTime > 0) { flag1 = true; if (this.inventory[1] != null) { –this.inventory[1].stackSize; if (this.inventory[1].stackSize == 0) { this.inventory[1] = inventory[1].getItem().getContainerItem(inventory[1]); } } } } if (this.isCrushing() && this.canCrush()) { ++this.crusherResultCrushTime; if (this.crusherResultCrushTime == maxCrushTime) { this.crusherResultCrushTime = 0; this.crushItem(); flag1 = true; } /** * <todo>*/ } else { this.crusherResultCrushTime = 0; } if (flag != this.crusherCrushTime > 0) { flag1 = true; BlockCrusher.updateCrusherBlockState(this.crusherCrushTime > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord); } } if (flag1) { this.markDirty(); } } private boolean canCrush() { if (this.inventory[0] == null) { return false; } else { ItemStack itemstack = CrusherRecipes.crushing().getCrushingResult(this.inventory[0]); if (itemstack == null) return false; if (this.inventory[2] == null) return true; if (!this.inventory[2].isItemEqual(itemstack)) return false; int result = inventory[2].stackSize + itemstack.stackSize; return result <= getInventoryStackLimit() && result <= this.inventory[2].getMaxStackSize(); //Forge BugFix: Make it respect stack sizes properly. } } public void crushItem() { if (this.canCrush()) { ItemStack itemstack = CrusherRecipes.crushing().getCrushingResult(this.inventory[0]); if (this.inventory[2] == null) { this.inventory[2] = itemstack.copy(); } else if (this.inventory[2].getItem() == itemstack.getItem()) { this.inventory[2].stackSize += itemstack.stackSize; // Forge BugFix: Results may have multiple items } –this.inventory[0].stackSize; if (this.inventory[0].stackSize <= 0) { this.inventory[0] = null; } } } public static int getItemPowerTime(ItemStack p_145952_0_) { if (p_145952_0_ == null) { return 0; } else { Item item = p_145952_0_.getItem(); if (item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air) { Block block = Block.getBlockFromItem(item); if (block == Blocks.wooden_slab) { return Integer.valueOf((int) (150 * powerMultiplier)); } if (block.getMaterial() == Material.wood) { return Integer.valueOf((int) (300 * powerMultiplier)); } if (block == Blocks.coal_block) { return Integer.valueOf((int) (16000 * powerMultiplier)); } } if (item instanceof ItemTool && ((ItemTool)item).getToolMaterialName().equals("WOOD")) return Integer.valueOf((int) (200 * powerMultiplier)); if (item instanceof ItemSword && ((ItemSword)item).getToolMaterialName().equals("WOOD")) return Integer.valueOf((int) (200 * powerMultiplier)); if (item instanceof ItemHoe && ((ItemHoe)item).getToolMaterialName().equals("WOOD")) return Integer.valueOf((int) (200 * powerMultiplier)); if (item == Items.stick) return Integer.valueOf((int) (100 * powerMultiplier)); if (item == Items.coal) return Integer.valueOf((int) (1600 * powerMultiplier)); if (item == Items.lava_bucket) return Integer.valueOf((int) (20000 * powerMultiplier)); if (item == Item.getItemFromBlock(Blocks.sapling)) return Integer.valueOf((int) (100 * powerMultiplier)); if (item == Items.blaze_rod) return Integer.valueOf((int) (2400 * powerMultiplier)); return GameRegistry.getFuelValue(p_145952_0_); } } public static boolean isItemFuel(ItemStack p_145954_0_) { return getItemPowerTime(p_145954_0_) > 0; } public boolean canInsertItem(int par1, ItemStack par2ItemStack, int par3) { return this.isItemValidForSlot(par1, par2ItemStack); } public boolean canExtractItem(int par1, ItemStack par2ItemStack, int par3) { return par3 != 0 || par1 != 1 || par2ItemStack.getItem() == Items.bucket; } }
ContainerCrusher.java :
package fr.MrBlockTNT.NewOres.Basic.crusher; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.ICrafting; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; public class ContainerCrusher extends Container { private TileEntityCrusher tec; private int lastCrushResultTime; private int lastCrushTime; private int lastItemPowerTime; public ContainerCrusher(InventoryPlayer par1InventoryPlayer, TileEntityCrusher par2TileEntityCrusher) { this.tec = par2TileEntityCrusher; this.addSlotToContainer(new Slot(par2TileEntityCrusher, 0, 56, 17)); this.addSlotToContainer(new Slot(par2TileEntityCrusher, 1, 56, 53)); this.addSlotToContainer(new SlotCrusher(par1InventoryPlayer.player, par2TileEntityCrusher, 2, 116, 35)); int i; for (i = 0; i < 3; ++i) { for (int j = 0; j < 9; ++j) { this.addSlotToContainer(new Slot(par1InventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18)); } } for (i = 0; i < 9; ++i) { this.addSlotToContainer(new Slot(par1InventoryPlayer, i, 8 + i * 18, 142)); } } public void addCraftingToCrafters(ICrafting par1ICrafting) { super.addCraftingToCrafters(par1ICrafting); par1ICrafting.sendProgressBarUpdate(this, 0, this.tec.crusherResultCrushTime); par1ICrafting.sendProgressBarUpdate(this, 1, this.tec.crusherCrushTime); par1ICrafting.sendProgressBarUpdate(this, 2, this.tec.currentItemPowerTime); } /** * Looks for changes made in the container, sends them to every listener. */ public void detectAndSendChanges() { super.detectAndSendChanges(); for (int i = 0; i < this.crafters.size(); ++i) { ICrafting icrafting = (ICrafting)this.crafters.get(i); if (this.lastCrushResultTime != this.tec.crusherResultCrushTime) { icrafting.sendProgressBarUpdate(this, 0, this.tec.crusherResultCrushTime); } if (this.lastCrushTime != this.tec.crusherCrushTime) { icrafting.sendProgressBarUpdate(this, 1, this.tec.crusherCrushTime); } if (this.lastItemPowerTime != this.tec.currentItemPowerTime) { icrafting.sendProgressBarUpdate(this, 2, this.tec.currentItemPowerTime); } } this.lastCrushResultTime = this.tec.crusherResultCrushTime; this.lastCrushTime = this.tec.crusherCrushTime; this.lastItemPowerTime = this.tec.currentItemPowerTime; } @SideOnly(Side.CLIENT) public void updateProgressBar(int par1, int par2) { if (par1 == 0) { this.tec.crusherResultCrushTime = par2; } if (par1 == 1) { this.tec.crusherCrushTime = par2; } if (par1 == 2) { this.tec.currentItemPowerTime = par2; } } public boolean canInteractWith(EntityPlayer par1EntityPlayer) { return this.tec.isUseableByPlayer(par1EntityPlayer); } /** * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that. */ public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2) { ItemStack itemstack = null; Slot slot = (Slot)this.inventorySlots.get(par2); if (slot != null && slot.getHasStack()) { ItemStack itemstack1 = slot.getStack(); itemstack = itemstack1.copy(); if (par2 == 2) { if (!this.mergeItemStack(itemstack1, 3, 39, true)) { return null; } slot.onSlotChange(itemstack1, itemstack); } else if (par2 != 1 && par2 != 0) { if (CrusherRecipes.crushing().getCrushingResult(itemstack1) != null) { if (!this.mergeItemStack(itemstack1, 0, 1, false)) { return null; } } else if (TileEntityCrusher.isItemFuel(itemstack1)) { if (!this.mergeItemStack(itemstack1, 1, 2, false)) { return null; } } else if (par2 >= 3 && par2 < 30) { if (!this.mergeItemStack(itemstack1, 30, 39, false)) { return null; } } else if (par2 >= 30 && par2 < 39 && !this.mergeItemStack(itemstack1, 3, 30, false)) { return null; } } else if (!this.mergeItemStack(itemstack1, 3, 39, false)) { return null; } if (itemstack1.stackSize == 0) { slot.putStack((ItemStack)null); } else { slot.onSlotChanged(); } if (itemstack1.stackSize == itemstack.stackSize) { return null; } slot.onPickupFromSlot(par1EntityPlayer, itemstack1); } return itemstack; } }
Si des parties de code obfusqué dérange quelqu’un, qu’il me demande la “traduction”.
Merci d’avance.</todo>
-
Personnellement je trouve le système du four complètement mal foutu (en plus ça utilise deux id)
Dans mon mod privatizer j’ai refais le principe du four en ne passant que par le tile entity :
https://github.com/FFMT/Privatizer -
Merci je regarde ça.
-
J’ai regardé dans ton code mais j’ai beau essayer, ça ne marche pas :
BlockCrusher.java :
package fr.MrBlockTNT.NewOres.Basic.crusher; import java.util.List; import java.util.Random; import net.minecraft.block.Block; import net.minecraft.client.renderer.texture.IIconRegister; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.IInventory; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.IIcon; import net.minecraft.util.MathHelper; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import cpw.mods.fml.common.network.internal.FMLNetworkHandler; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import fr.MrBlockTNT.NewOres.Basic.Basic; import fr.MrBlockTNT.NewOres.Basic.BasicBlock; public class BlockCrusher extends BasicBlock { public static String[] subBlock = new String[] {"copper", "bronze", "steel"}; private IIcon[][] iconArray = new IIcon[subBlock.length][3]; private IIcon[] on_state = new IIcon[subBlock.length]; private static boolean field_149934_M; public void registerBlockIcons(IIconRegister iconRegister) { for(int i = 0; i < subBlock.length; i++) { this.iconArray*[0] = iconRegister.registerIcon("neworesbasic:" + subBlock* + "_crusher_top"); this.iconArray*[1] = iconRegister.registerIcon("neworesbasic:" + subBlock* + "_crusher_front"); this.iconArray*[2] = iconRegister.registerIcon("neworesbasic:" + subBlock* + "_crusher_side"); this.on_state* = iconRegister.registerIcon("neworesbasic:" + subBlock* + "_crusher_front_active"); } } public void getSubBlocks(Item item, CreativeTabs tabs, List list) { for(int i = 0; i < subBlock.length; i++) { list.add(new ItemStack(item, 1, i)); } } public int damageDropped(int metadata) { return metadata; } @SideOnly(Side.CLIENT) public IIcon getIcon(IBlockAccess world, int x, int y, int z, int side) { int metadata = world.getBlockMetadata(x, y, z); TileEntity tile = world.getTileEntity(x, y, z); if(side == 1) // UP { return this.iconArray[metadata][0]; } else if(tile instanceof TileEntityCrusher) { TileEntityCrusher crusher = (TileEntityCrusher)tile; byte direction = ((TileEntityCrusher)tile).getDirection(); switch(direction) { case 0: return side == 3 ? (crusher.isActive() ? this.on_state[metadata] : this.iconArray[metadata][1]) : this.iconArray[metadata][2]; case 1: return side == 4 ? (crusher.isActive() ? this.on_state[metadata] : this.iconArray[metadata][1]) : this.iconArray[metadata][2]; case 2: return side == 2 ? (crusher.isActive() ? this.on_state[metadata] : this.iconArray[metadata][1]) : this.iconArray[metadata][2]; case 3: return side == 5 ? (crusher.isActive() ? this.on_state[metadata] : this.iconArray[metadata][1]) : this.iconArray[metadata][2]; } } else { return this.iconArray[metadata][2]; } return getIcon(side, world.getBlockMetadata(x, y, z)); } @SideOnly(Side.CLIENT) public IIcon getIcon(int side, int metadata) { if(side == 1) // UP { return this.iconArray[metadata][0]; } else if(side == 3) // FRONT : 3 { return this.iconArray[metadata][1]; } else { return this.iconArray[metadata][2]; } } public BlockCrusher() { this.setCreativeTab(Basic.BasicTab); } @Override public TileEntity createTileEntity(World world, int metadata) { return new TileEntityCrusher(); } @Override public boolean hasTileEntity(int metadata) { return true; } public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9) { FMLNetworkHandler.openGui(player, Basic.instance, 0, world, x, y, z); return true; } public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase living, ItemStack stack) { TileEntity te = world.getTileEntity(x, y, z); if(te != null && te instanceof TileEntityCrusher && stack.hasDisplayName()) { ((TileEntityCrusher)te).setCustomGuiName(stack.getDisplayName()); } if(te instanceof TileEntityCrusher) { int direction = MathHelper.floor_double((double)(living.rotationYaw * 4.0F / 360.0F) + 2.5D) & 3; ((TileEntityCrusher)te).setDirection((byte)direction); } } public void breakBlock(World world, int x, int y, int z, Block block, int metadata) { dropContainerItem(world, x, y, z); super.breakBlock(world, x, y, z, block, metadata); } protected void dropContainerItem(World world, int x, int y, int z) { TileEntityCrusher tecrusher = (TileEntityCrusher)world.getTileEntity(x, y, z); if (tecrusher != null) { for (int slotId = 0; slotId < tecrusher.getSizeInventory(); slotId++) { ItemStack stack = tecrusher.getStackInSlot(slotId); if (stack != null) { float f = world.rand.nextFloat() * 0.8F + 0.1F; float f1 = world.rand.nextFloat() * 0.8F + 0.1F; EntityItem entityitem; for (float f2 = world.rand.nextFloat() * 0.8F + 0.1F; stack.stackSize > 0; world.spawnEntityInWorld(entityitem)) { int k1 = world.rand.nextInt(21) + 10; if (k1 > stack.stackSize) { k1 = stack.stackSize; } stack.stackSize -= k1; entityitem = new EntityItem(world, (double)((float)x + f), (double)((float)y + f1), (double)((float)z + f2), new ItemStack(stack.getItem(), k1, stack.getItemDamage())); float f3 = 0.05F; entityitem.motionX = (double)((float)world.rand.nextGaussian() * f3); entityitem.motionY = (double)((float)world.rand.nextGaussian() * f3 + 0.2F); entityitem.motionZ = (double)((float)world.rand.nextGaussian() * f3); if (stack.hasTagCompound()) { entityitem.getEntityItem().setTagCompound((NBTTagCompound)stack.getTagCompound().copy()); } } } } } } public static void updateCrusherBlockState(boolean p_149931_0_, World world, int x, int y, int z) { int l = world.getBlockMetadata(x, y, z); TileEntity tileentity = world.getTileEntity(x, y, z); field_149934_M = true; int metadata = world.getBlockMetadata(x, y, z); /*if (p_149931_0_) { world.setBlock(x, y, z, Basic.crusher_active, metadata, 0); // In-crushing block } else { world.setBlock(x, y, z, Basic.crusher, metadata, 0); }*/ field_149934_M = false; world.setBlockMetadataWithNotify(x, y, z, l, 2); if (tileentity != null) { tileentity.validate(); world.setTileEntity(x, y, z, tileentity); } } public boolean hasComparatorInputOverride() { return true; } public int getComparatorInputOverride(World p_149736_1_, int p_149736_2_, int p_149736_3_, int p_149736_4_, int p_149736_5_) { return Container.calcRedstoneFromInventory((IInventory)p_149736_1_.getTileEntity(p_149736_2_, p_149736_3_, p_149736_4_)); } @SideOnly(Side.CLIENT) public void randomDisplayTick(World world, int x, int y, int z, Random rand) { TileEntity tile = world.getTileEntity(x, y, z); if(tile instanceof TileEntityCrusher && ((TileEntityCrusher)tile).isActive()) { for(int i = 0; i < rand.nextInt(8); i++) { float f = (float)x + 0.4F + rand.nextFloat() * 6.0F / 16.0F; float f1 = (float)y + 1.1F; float f2 = (float)z + 0.4F; float f3 = 0.52F; float f4 = rand.nextFloat() * 0.6F - 0.3F; world.spawnParticle("smoke", (double)f, (double)f1, (double)(f2 + f4), 0.0D, 0.0D, 0.0D); } } } @SideOnly(Side.CLIENT) public int getLightValue(IBlockAccess world, int x, int y, int z) { TileEntity te = world.getTileEntity(x, y, z); if(te instanceof TileEntityCrusher && ((TileEntityCrusher)te).isActive()) { return 13; } return super.getLightValue(world, x, y, z); } }
TileEntityCrusher.java :
package fr.MrBlockTNT.NewOres.Basic.crusher; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; import net.minecraft.init.Items; import net.minecraft.inventory.IInventory; import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemHoe; import net.minecraft.item.ItemStack; import net.minecraft.item.ItemSword; import net.minecraft.item.ItemTool; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.network.NetworkManager; import net.minecraft.network.Packet; import net.minecraft.network.play.server.S35PacketUpdateTileEntity; import net.minecraft.tileentity.TileEntity; import cpw.mods.fml.common.registry.GameRegistry; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; public class TileEntityCrusher extends TileEntity implements IInventory { private static final int[] slotsTop = new int[] {0}; private static final int[] slotsBottom = new int[] {2, 1}; private static final int[] slotsSides = new int[] {1}; private ItemStack[] inventory = new ItemStack[3]; private String customName; public int crusherCrushTime; public int currentItemPowerTime; public int crusherResultCrushTime; public int maxCrushTime = 200; public static float powerMultiplier = 1F; private byte direction; private boolean active; @Override public void readFromNBT(NBTTagCompound nbttag) { super.readFromNBT(nbttag); this.direction = nbttag.getByte("Direction"); NBTTagList nbttaglist = nbttag.getTagList("Items", 10); this.inventory = new ItemStack[this.getSizeInventory()]; for (int i = 0; i < nbttaglist.tagCount(); ++i) { NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i); byte b0 = nbttagcompound1.getByte("Slot"); if (b0 >= 0 && b0 < this.inventory.length) { this.inventory[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1); } } this.crusherCrushTime = nbttag.getShort("CrushTime"); this.crusherResultCrushTime = nbttag.getShort("CrushResultTime"); this.currentItemPowerTime = getItemPowerTime(this.inventory[1]); if (nbttag.hasKey("CustomName", 8)) { this.customName = nbttag.getString("CustomName"); } this.active = nbttag.getBoolean("Active"); } @Override public void writeToNBT(NBTTagCompound nbttag) { super.writeToNBT(nbttag); nbttag.setByte("Direction", this.direction); nbttag.setShort("CrushTime", (short)this.crusherCrushTime); nbttag.setShort("CrushResultTime", (short)this.crusherResultCrushTime); NBTTagList nbttaglist = new NBTTagList(); for (int i = 0; i < this.inventory.length; ++i) { if (this.inventory* != null) { NBTTagCompound nbttagcompound1 = new NBTTagCompound(); nbttagcompound1.setByte("Slot", (byte)i); this.inventory*.writeToNBT(nbttagcompound1); nbttaglist.appendTag(nbttagcompound1); } } nbttag.setTag("Items", nbttaglist); if (this.hasCustomInventoryName()) { nbttag.setString("CustomName", this.customName); } nbttag.setBoolean("Active", this.active); } public boolean isActive() { return this.active; } public byte getDirection() { return direction; } public void setDirection(byte direction) { this.direction = direction; this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord); } public Packet getDescriptionPacket() { NBTTagCompound nbttagcompound = new NBTTagCompound(); this.writeToNBT(nbttagcompound); return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 0, nbttagcompound); } public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) { this.readFromNBT(pkt.func_148857_g()); this.worldObj.markBlockRangeForRenderUpdate(this.xCoord, this.yCoord, this.zCoord, this.xCoord, this.yCoord, this.zCoord); } @Override public int getSizeInventory() { return inventory.length; } public ItemStack getStackInSlot(int par1) { return this.inventory[par1]; } @Override public ItemStack decrStackSize(int slotId, int quantity) { if (this.inventory[slotId] != null) { ItemStack itemstack; if (this.inventory[slotId].stackSize <= quantity) { itemstack = this.inventory[slotId]; this.inventory[slotId] = null; return itemstack; } else { itemstack = this.inventory[slotId].splitStack(quantity); if (this.inventory[slotId].stackSize == 0) { this.inventory[slotId] = null; } return itemstack; } } else { return null; } } @Override public ItemStack getStackInSlotOnClosing(int slotId) { if (this.inventory[slotId] != null) { ItemStack itemstack = this.inventory[slotId]; this.inventory[slotId] = null; return itemstack; } else { return null; } } @Override public void setInventorySlotContents(int slotId, ItemStack stack) { this.inventory[slotId] = stack; if (stack != null && stack.stackSize > this.getInventoryStackLimit()) { stack.stackSize = this.getInventoryStackLimit(); } this.markDirty(); } @Override public String getInventoryName() { return this.hasCustomInventoryName() ? this.customName : "container.crusher"; } @Override public boolean hasCustomInventoryName() { return this.customName != null && this.customName.length() > 0; } public void setCustomGuiName(String name) { this.customName = name; } @Override public int getInventoryStackLimit() { return 64; } @Override public boolean isUseableByPlayer(EntityPlayer player) { return worldObj.getTileEntity(xCoord, yCoord, zCoord) == this && player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64; } @Override public boolean isItemValidForSlot(int par1, ItemStack par2ItemStack) { return par1 == 2 ? false : (par1 == 1 ? isItemFuel(par2ItemStack) : true); } @Override public void openInventory() { } @Override public void closeInventory() { } @SideOnly(Side.CLIENT) public int getCrushProgressScaled(int speed) { return this.crusherResultCrushTime * speed/ this.maxCrushTime; } @SideOnly(Side.CLIENT) public int getCrushTimeRemainingScaled(int p_145955_1_) { if (this.currentItemPowerTime == 0) { this.currentItemPowerTime = this.maxCrushTime; } return this.crusherCrushTime * p_145955_1_ / this.currentItemPowerTime; } public boolean isCrushing() { return this.crusherCrushTime > 0; } public void updateEntity() { if(this.getBlockMetadata() == 0) { this.maxCrushTime = 200; this.powerMultiplier = 1.0F; } else if(this.getBlockMetadata() == 1) { this.maxCrushTime = 190; this.powerMultiplier = 1.1F; } else if(this.getBlockMetadata() == 2) { this.maxCrushTime = 150; this.powerMultiplier = 1.3F; } boolean flag = this.crusherCrushTime > 0; boolean flag1 = false; if (this.crusherCrushTime > 0) { –this.crusherCrushTime; } if (!this.worldObj.isRemote) { if (this.crusherCrushTime == 0 && this.canCrush()) { this.currentItemPowerTime = this.crusherCrushTime = getItemPowerTime(this.inventory[1]); if (this.crusherCrushTime > 0) { flag1 = true; if (this.inventory[1] != null) { –this.inventory[1].stackSize; if (this.inventory[1].stackSize == 0) { this.inventory[1] = inventory[1].getItem().getContainerItem(inventory[1]); } } } } if (this.isCrushing() && this.canCrush()) { ++this.crusherResultCrushTime; if (this.crusherResultCrushTime == maxCrushTime) { this.crusherResultCrushTime = 0; this.crushItem(); flag1 = true; } /** * <todo>*/ } else { this.crusherResultCrushTime = 0; } if (flag != this.crusherCrushTime > 0) { flag1 = true; BlockCrusher.updateCrusherBlockState(this.crusherCrushTime > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord); } } if (flag1) { this.markDirty(); } } private boolean canCrush() { if (this.inventory[0] == null) { return false; } else { ItemStack itemstack = CrusherRecipes.crushing().getCrushingResult(this.inventory[0]); if (itemstack == null) return false; if (this.inventory[2] == null) return true; if (!this.inventory[2].isItemEqual(itemstack)) return false; int result = inventory[2].stackSize + itemstack.stackSize; return result <= getInventoryStackLimit() && result <= this.inventory[2].getMaxStackSize(); //Forge BugFix: Make it respect stack sizes properly. } } public void crushItem() { if (this.canCrush()) { ItemStack itemstack = CrusherRecipes.crushing().getCrushingResult(this.inventory[0]); if (this.inventory[2] == null) { this.inventory[2] = itemstack.copy(); } else if (this.inventory[2].getItem() == itemstack.getItem()) { this.inventory[2].stackSize += itemstack.stackSize; // Forge BugFix: Results may have multiple items } –this.inventory[0].stackSize; if (this.inventory[0].stackSize <= 0) { this.inventory[0] = null; } } } public static int getItemPowerTime(ItemStack p_145952_0_) { if (p_145952_0_ == null) { return 0; } else { Item item = p_145952_0_.getItem(); if (item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air) { Block block = Block.getBlockFromItem(item); if (block == Blocks.wooden_slab) { return Integer.valueOf((int) (150 * powerMultiplier)); } if (block.getMaterial() == Material.wood) { return Integer.valueOf((int) (300 * powerMultiplier)); } if (block == Blocks.coal_block) { return Integer.valueOf((int) (16000 * powerMultiplier)); } } if (item instanceof ItemTool && ((ItemTool)item).getToolMaterialName().equals("WOOD")) return Integer.valueOf((int) (200 * powerMultiplier)); if (item instanceof ItemSword && ((ItemSword)item).getToolMaterialName().equals("WOOD")) return Integer.valueOf((int) (200 * powerMultiplier)); if (item instanceof ItemHoe && ((ItemHoe)item).getToolMaterialName().equals("WOOD")) return Integer.valueOf((int) (200 * powerMultiplier)); if (item == Items.stick) return Integer.valueOf((int) (100 * powerMultiplier)); if (item == Items.coal) return Integer.valueOf((int) (1600 * powerMultiplier)); if (item == Items.lava_bucket) return Integer.valueOf((int) (20000 * powerMultiplier)); if (item == Item.getItemFromBlock(Blocks.sapling)) return Integer.valueOf((int) (100 * powerMultiplier)); if (item == Items.blaze_rod) return Integer.valueOf((int) (2400 * powerMultiplier)); return GameRegistry.getFuelValue(p_145952_0_); } } public static boolean isItemFuel(ItemStack p_145954_0_) { return getItemPowerTime(p_145954_0_) > 0; } public boolean canInsertItem(int par1, ItemStack par2ItemStack, int par3) { return this.isItemValidForSlot(par1, par2ItemStack); } public boolean canExtractItem(int par1, ItemStack par2ItemStack, int par3) { return par3 != 0 || par1 != 1 || par2ItemStack.getItem() == Items.bucket; } } ```</todo>
-
Au final c’était juste une erreur dans ton code :
TileEntiyPrivateFurnace.updateEntity :boolean active = furnaceBurnTime > 0;
doit être remplacé par :
this.active = furnaceBurnTime > 0;
Pour que isActive() passe à true et que getIcon() agisse en conséquence.
Sujet résolu.
-
Pourtant chez moi tout est ok …
-
Bah logiquement comme dans TileEntityPrivateFurnace.java, le booléen active (de la classe) n’est “changé/actualisé” nulle part. (à moins que je n’ai pas vu mais en général Ctrl + F montre tout)
-
https://github.com/FFMT/Privatizer/blob/master/privatizer_src/fr/mcnanotech/privatizer/common/TileEntityPrivateFurnace.java#L281
Si. Mais que côté serveur puis j’envoie aux clients -
Ah d’accord