Résolu ItemBlock Torch
-
Bonjour.
J’ai créer un mod de TorchCustom.
Je la pose et elle s’éteint au bout de 5 secondes.
Sauf que du coup faut un maxStackSize de 1, le problème c’est un block…
Du coup j’ai créer une instance du block:private IIcon itemBlockIIcon; public ItemBlockOFF(Block p_i45328_1_) { super(p_i45328_1_); this.setMaxStackSize(1); } @Override @SideOnly(Side.CLIENT) public int getSpriteNumber() { return 0; } @Override @SideOnly(Side.CLIENT) public IIcon getIconFromDamage(int p_77617_1_) { return this.itemBlockIIcon; } @Override public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float par8, float par9, float par10) { if(player.canPlayerEdit(x, y, z, side, stack)) { Block i1 = world.getBlock(x, y, z); if( world.isAirBlock(x, y + 1, z )) { world.setBlock(x, y + 1, z, CustomTorchMod.customTorchOFF); world.playSoundEffect(x, y, z, CustomTorchMod.customTorchOFF.stepSound.soundName, CustomTorchMod.customTorchOFF.stepSound.getVolume(), CustomTorchMod.customTorchOFF.stepSound.getPitch()); --stack.stackSize; return true; } } return false; } @Override public void registerIcons(IIconRegister IIconRegister) { this.itemBlockIIcon = IIconRegister.registerIcon("customtorchmod:unlitTorch"); } }
La class de la torch:
public class CustomTorchOFF extends Block { public CustomTorchOFF() { super(Material.circuits); this.setTickRandomly(true); this.setCreativeTab(TorchROFFMod.survivalZTab); } public AxisAlignedBB getCollisionBoundingBoxFromPool(World p_149668_1_, int p_149668_2_, int p_149668_3_, int p_149668_4_) { return null; } public boolean isOpaqueCube() { return false; } public boolean renderAsNormalBlock() { return false; } public int getRenderType() { return 2; } private boolean func_150107_m(World p_150107_1_, int p_150107_2_, int p_150107_3_, int p_150107_4_) { if (World.doesBlockHaveSolidTopSurface(p_150107_1_, p_150107_2_, p_150107_3_, p_150107_4_)) { return true; } else { Block block = p_150107_1_.getBlock(p_150107_2_, p_150107_3_, p_150107_4_); return block.canPlaceTorchOnTop(p_150107_1_, p_150107_2_, p_150107_3_, p_150107_4_); } } public boolean canPlaceBlockAt(World p_149742_1_, int p_149742_2_, int p_149742_3_, int p_149742_4_) { return p_149742_1_.isSideSolid(p_149742_2_ - 1, p_149742_3_, p_149742_4_, EAST, true) || p_149742_1_.isSideSolid(p_149742_2_ + 1, p_149742_3_, p_149742_4_, WEST, true) || p_149742_1_.isSideSolid(p_149742_2_, p_149742_3_, p_149742_4_ - 1, SOUTH, true) || p_149742_1_.isSideSolid(p_149742_2_, p_149742_3_, p_149742_4_ + 1, NORTH, true) || func_150107_m(p_149742_1_, p_149742_2_, p_149742_3_ - 1, p_149742_4_); } public int onBlockPlaced(World p_149660_1_, int p_149660_2_, int p_149660_3_, int p_149660_4_, int p_149660_5_, float p_149660_6_, float p_149660_7_, float p_149660_8_, int p_149660_9_) { int j1 = p_149660_9_; if (p_149660_5_ == 1 && this.func_150107_m(p_149660_1_, p_149660_2_, p_149660_3_ - 1, p_149660_4_)) { j1 = 5; } if (p_149660_5_ == 2 && p_149660_1_.isSideSolid(p_149660_2_, p_149660_3_, p_149660_4_ + 1, NORTH, true)) { j1 = 4; } if (p_149660_5_ == 3 && p_149660_1_.isSideSolid(p_149660_2_, p_149660_3_, p_149660_4_ - 1, SOUTH, true)) { j1 = 3; } if (p_149660_5_ == 4 && p_149660_1_.isSideSolid(p_149660_2_ + 1, p_149660_3_, p_149660_4_, WEST, true)) { j1 = 2; } if (p_149660_5_ == 5 && p_149660_1_.isSideSolid(p_149660_2_ - 1, p_149660_3_, p_149660_4_, EAST, true)) { j1 = 1; } return j1; } public void updateTick(World p_149674_1_, int p_149674_2_, int p_149674_3_, int p_149674_4_, Random p_149674_5_) { super.updateTick(p_149674_1_, p_149674_2_, p_149674_3_, p_149674_4_, p_149674_5_); if (p_149674_1_.getBlockMetadata(p_149674_2_, p_149674_3_, p_149674_4_) == 0) { this.onBlockAdded(p_149674_1_, p_149674_2_, p_149674_3_, p_149674_4_); } } public void onBlockAdded(World p_149726_1_, int p_149726_2_, int p_149726_3_, int p_149726_4_) { if (p_149726_1_.getBlockMetadata(p_149726_2_, p_149726_3_, p_149726_4_) == 0) { if (p_149726_1_.isSideSolid(p_149726_2_ - 1, p_149726_3_, p_149726_4_, EAST, true)) { p_149726_1_.setBlockMetadataWithNotify(p_149726_2_, p_149726_3_, p_149726_4_, 1, 2); } else if (p_149726_1_.isSideSolid(p_149726_2_ + 1, p_149726_3_, p_149726_4_, WEST, true)) { p_149726_1_.setBlockMetadataWithNotify(p_149726_2_, p_149726_3_, p_149726_4_, 2, 2); } else if (p_149726_1_.isSideSolid(p_149726_2_, p_149726_3_, p_149726_4_ - 1, SOUTH, true)) { p_149726_1_.setBlockMetadataWithNotify(p_149726_2_, p_149726_3_, p_149726_4_, 3, 2); } else if (p_149726_1_.isSideSolid(p_149726_2_, p_149726_3_, p_149726_4_ + 1, NORTH, true)) { p_149726_1_.setBlockMetadataWithNotify(p_149726_2_, p_149726_3_, p_149726_4_, 4, 2); } else if (this.func_150107_m(p_149726_1_, p_149726_2_, p_149726_3_ - 1, p_149726_4_)) { p_149726_1_.setBlockMetadataWithNotify(p_149726_2_, p_149726_3_, p_149726_4_, 5, 2); } } this.func_150109_e(p_149726_1_, p_149726_2_, p_149726_3_, p_149726_4_); } public void onNeighborBlockChange(World p_149695_1_, int p_149695_2_, int p_149695_3_, int p_149695_4_, Block p_149695_5_) { this.func_150108_b(p_149695_1_, p_149695_2_, p_149695_3_, p_149695_4_, p_149695_5_); } protected boolean func_150108_b(World p_150108_1_, int p_150108_2_, int p_150108_3_, int p_150108_4_, Block p_150108_5_) { if (this.func_150109_e(p_150108_1_, p_150108_2_, p_150108_3_, p_150108_4_)) { int l = p_150108_1_.getBlockMetadata(p_150108_2_, p_150108_3_, p_150108_4_); boolean flag = false; if (!p_150108_1_.isSideSolid(p_150108_2_ - 1, p_150108_3_, p_150108_4_, EAST, true) && l == 1) { flag = true; } if (!p_150108_1_.isSideSolid(p_150108_2_ + 1, p_150108_3_, p_150108_4_, WEST, true) && l == 2) { flag = true; } if (!p_150108_1_.isSideSolid(p_150108_2_, p_150108_3_, p_150108_4_ - 1, SOUTH, true) && l == 3) { flag = true; } if (!p_150108_1_.isSideSolid(p_150108_2_, p_150108_3_, p_150108_4_ + 1, NORTH, true) && l == 4) { flag = true; } if (!this.func_150107_m(p_150108_1_, p_150108_2_, p_150108_3_ - 1, p_150108_4_) && l == 5) { flag = true; } if (flag) { this.dropBlockAsItem(p_150108_1_, p_150108_2_, p_150108_3_, p_150108_4_, p_150108_1_.getBlockMetadata(p_150108_2_, p_150108_3_, p_150108_4_), 0); p_150108_1_.setBlockToAir(p_150108_2_, p_150108_3_, p_150108_4_); return true; } else { return false; } } else { return true; } } protected boolean func_150109_e(World p_150109_1_, int p_150109_2_, int p_150109_3_, int p_150109_4_) { if (!this.canPlaceBlockAt(p_150109_1_, p_150109_2_, p_150109_3_, p_150109_4_)) { if (p_150109_1_.getBlock(p_150109_2_, p_150109_3_, p_150109_4_) == this) { this.dropBlockAsItem(p_150109_1_, p_150109_2_, p_150109_3_, p_150109_4_, p_150109_1_.getBlockMetadata(p_150109_2_, p_150109_3_, p_150109_4_), 0); p_150109_1_.setBlockToAir(p_150109_2_, p_150109_3_, p_150109_4_); } return false; } else { return true; } } public MovingObjectPosition collisionRayTrace(World p_149731_1_, int p_149731_2_, int p_149731_3_, int p_149731_4_, Vec3 p_149731_5_, Vec3 p_149731_6_) { int l = p_149731_1_.getBlockMetadata(p_149731_2_, p_149731_3_, p_149731_4_) & 7; float f = 0.15F; if (l == 1) { this.setBlockBounds(0.0F, 0.2F, 0.5F - f, f * 2.0F, 0.8F, 0.5F + f); } else if (l == 2) { this.setBlockBounds(1.0F - f * 2.0F, 0.2F, 0.5F - f, 1.0F, 0.8F, 0.5F + f); } else if (l == 3) { this.setBlockBounds(0.5F - f, 0.2F, 0.0F, 0.5F + f, 0.8F, f * 2.0F); } else if (l == 4) { this.setBlockBounds(0.5F - f, 0.2F, 1.0F - f * 2.0F, 0.5F + f, 0.8F, 1.0F); } else { f = 0.1F; this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, 0.6F, 0.5F + f); } return super.collisionRayTrace(p_149731_1_, p_149731_2_, p_149731_3_, p_149731_4_, p_149731_5_, p_149731_6_); } }
La Torch se pose mal…
Merci de votre aide. -
Bonsoir,
if( world.isAirBlock(x, y + 1, z )) { world.setBlock(x, y + 1, z, CustomTorchMod.customTorchOFF);
Ici tu vérifies si le block au dessus de là où tu cliques est de l’air, et tu y places ta torche, d’où ce décalage de 1 en y quand tu la poses
-
Bonsoir, on bosse tous les deux sur le même mod, donc j’ai également le même problème.
En revanche, tu dis que le problème vient du y + 1, mais en fait on est obligé. Car on point un block sur lequel doit être la torche, donc obligé de faire ce + 1.
Si on ne le met pas, si on laisse y, c’est la torche qui prend la place du block pointé……
J’ai réussi à localiser le problème mais je n’arrive pas à le fixer…Je pense que le soucis vient des méthodes onBlockAdded et onBlockPlaced de la classe torche custom off. Ce sont ces méthodes qui déterminent le bon placement de la torche lorsque le joueur onRightClick, mais là comme on set via le world, je pense que ces méthodes ne fonctionnennet pas à 100%J’ai réessayé avec ceci venant de la classe ItemBlock, mais rien à faire…
@Override public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float par8, float par9, float par10) { if(player.canPlayerEdit(x, y, z, side, stack)) { Block i1 = world.getBlock(x, y, z); if( world.isAirBlock(x, y + 1, z)) { world.setBlock(x, y + 1, z, (CustomTorchOFF)block); ((CustomTorchOFF)block).onBlockPlaced(world, x, y + 1, z, side, par8, par9, par10, 0); world.playSoundEffect(x, y, z, "dig.wood", CustomTorchMod.customTorchOFF.stepSound.getVolume(), CustomTorchMod.customTorchOFF.stepSound.getPitch()); --stack.stackSize; return true; } } return false; }