From 0599fddc516ca098c5ad47017f0bdca725ff9046 Mon Sep 17 00:00:00 2001 From: Alexander Anishin Date: Fri, 2 Jun 2023 14:31:48 +0300 Subject: [PATCH] Optimized Dislocator Receptacle --- .../multiblock/DislocatorReceptacle.java | 79 ++++++------ .../TileDislocatorReceptacle.java | 114 ++++++++++-------- 2 files changed, 109 insertions(+), 84 deletions(-) diff --git a/src/main/java/com/brandon3055/draconicevolution/common/blocks/multiblock/DislocatorReceptacle.java b/src/main/java/com/brandon3055/draconicevolution/common/blocks/multiblock/DislocatorReceptacle.java index 15069d18..97a439ac 100644 --- a/src/main/java/com/brandon3055/draconicevolution/common/blocks/multiblock/DislocatorReceptacle.java +++ b/src/main/java/com/brandon3055/draconicevolution/common/blocks/multiblock/DislocatorReceptacle.java @@ -22,11 +22,15 @@ import com.brandon3055.draconicevolution.common.items.tools.TeleporterMKI; import com.brandon3055.draconicevolution.common.lib.References; import com.brandon3055.draconicevolution.common.tileentities.multiblocktiles.TileDislocatorReceptacle; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; + /** * Created by Brandon on 19/5/2015. */ public class DislocatorReceptacle extends BlockCustomDrop implements ITileEntityProvider { + @SideOnly(Side.CLIENT) IIcon textureInactive; public DislocatorReceptacle() { @@ -41,6 +45,7 @@ public class DislocatorReceptacle extends BlockCustomDrop implements ITileEntity } @Override + @SideOnly(Side.CLIENT) public void registerBlockIcons(IIconRegister iconRegister) { textureInactive = iconRegister .registerIcon(References.RESOURCESPREFIX + "animated/dislocatorReceptacle_inactive"); @@ -48,23 +53,21 @@ public class DislocatorReceptacle extends BlockCustomDrop implements ITileEntity } @Override - public IIcon getIcon(IBlockAccess access, int x, int y, int z, int side) { - TileDislocatorReceptacle tile = access.getTileEntity(x, y, z) instanceof TileDislocatorReceptacle - ? (TileDislocatorReceptacle) access.getTileEntity(x, y, z) - : null; - if (tile != null) return tile.isActive ? blockIcon : textureInactive; + @SideOnly(Side.CLIENT) + public IIcon getIcon(IBlockAccess world, int x, int y, int z, int side) { + TileEntity tile = world.getTileEntity(x, y, z); + if (tile instanceof TileDislocatorReceptacle) { + return ((TileDislocatorReceptacle) tile).isActive ? blockIcon : textureInactive; + } return blockIcon; } @Override public void updateTick(World world, int x, int y, int z, Random random) { - TileDislocatorReceptacle tile = (TileDislocatorReceptacle) world.getTileEntity(x, y, z); - if (tile != null) tile.updateState(); - } - - @Override - public IIcon getIcon(int p_149691_1_, int meta) { - return blockIcon; + TileEntity tile = world.getTileEntity(x, y, z); + if (tile instanceof TileDislocatorReceptacle) { + ((TileDislocatorReceptacle) tile).updateState(); + } } @Override @@ -72,44 +75,45 @@ public class DislocatorReceptacle extends BlockCustomDrop implements ITileEntity return true; } + @Override + public TileEntity createNewTileEntity(World world, int metadata) { + return new TileDislocatorReceptacle(); + } + @Override public boolean canEntityDestroy(IBlockAccess world, int x, int y, int z, Entity entity) { return false; } @Override - public TileEntity createNewTileEntity(World world, int i) { - return new TileDislocatorReceptacle(); - } - - @Override - public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float p_149727_7_, - float p_149727_8_, float p_149727_9_) { - if (world.isRemote) return true; - TileDislocatorReceptacle tile = (TileDislocatorReceptacle) world.getTileEntity(x, y, z); - if (tile == null) return false; - - if (tile.getStackInSlot(0) != null) { + public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float subX, + float subY, float subZ) { + if (world.isRemote) { + return true; + } + TileEntity tile = world.getTileEntity(x, y, z); + if (!(tile instanceof TileDislocatorReceptacle)) { + return false; + } + TileDislocatorReceptacle receptacle = (TileDislocatorReceptacle) tile; + ItemStack stackInSlot = receptacle.getStackInSlot(0); + if (stackInSlot != null) { if (player.getHeldItem() == null) { - player.inventory.setInventorySlotContents(player.inventory.currentItem, tile.getStackInSlot(0)); - tile.setInventorySlotContents(0, null); + player.inventory.setInventorySlotContents(player.inventory.currentItem, stackInSlot); } else { - world.spawnEntityInWorld( - new EntityItem(world, player.posX, player.posY, player.posZ, tile.getStackInSlot(0))); - tile.setInventorySlotContents(0, null); + world.spawnEntityInWorld(new EntityItem(world, player.posX, player.posY, player.posZ, stackInSlot)); } + receptacle.setInventorySlotContents(0, null); world.markBlockForUpdate(x, y, z); world.notifyBlockChange(x, y, z, this); - } else { ItemStack stack = player.getHeldItem(); if (stack != null && stack.getItem() instanceof TeleporterMKI && ((TeleporterMKI) stack.getItem()).getLocation(stack) != null) { - tile.setInventorySlotContents(0, player.getHeldItem()); + receptacle.setInventorySlotContents(0, player.getHeldItem()); player.destroyCurrentEquippedItem(); } } - return true; } @@ -120,8 +124,13 @@ public class DislocatorReceptacle extends BlockCustomDrop implements ITileEntity @Override public int getComparatorInputOverride(World world, int x, int y, int z, int p_149736_5_) { - TileDislocatorReceptacle tile = (TileDislocatorReceptacle) world.getTileEntity(x, y, z); - return tile == null ? 0 : tile.getStackInSlot(0) != null ? 15 : 0; + TileEntity tile = world.getTileEntity(x, y, z); + if (tile instanceof TileDislocatorReceptacle) { + if (((TileDislocatorReceptacle) tile).getStackInSlot(0) != null) { + return 15; + } + } + return 0; } @Override @@ -135,5 +144,5 @@ public class DislocatorReceptacle extends BlockCustomDrop implements ITileEntity } @Override - protected void getCustomTileEntityDrops(TileEntity te, List droppes) {} + protected void getCustomTileEntityDrops(TileEntity tile, List drops) {} } diff --git a/src/main/java/com/brandon3055/draconicevolution/common/tileentities/multiblocktiles/TileDislocatorReceptacle.java b/src/main/java/com/brandon3055/draconicevolution/common/tileentities/multiblocktiles/TileDislocatorReceptacle.java index ce0bd58f..2bcaafcf 100644 --- a/src/main/java/com/brandon3055/draconicevolution/common/tileentities/multiblocktiles/TileDislocatorReceptacle.java +++ b/src/main/java/com/brandon3055/draconicevolution/common/tileentities/multiblocktiles/TileDislocatorReceptacle.java @@ -27,27 +27,37 @@ public class TileDislocatorReceptacle extends TileEntity implements IInventory { @Override public void updateEntity() { - if (worldObj.isRemote) return; - if (coolDown > 0) coolDown--; - if (ticksTillStart > -1) ticksTillStart--; - if (ticksTillStart == 0) + if (worldObj.isRemote) { + return; + } + if (coolDown > 0) { + coolDown--; + } + if (ticksTillStart > -1) { + ticksTillStart--; + } + if (ticksTillStart == 0) { worldObj.scheduleBlockUpdate(xCoord, yCoord, zCoord, worldObj.getBlock(xCoord, yCoord, zCoord), 1); + } } public void validateActivePortal() { - if (updating) return; - if (structure == null) { - isActive = false; + if (updating) { return; } - isActive = structure.checkFrameIsValid(worldObj, xCoord, yCoord, zCoord) + + final boolean isActiveBeforeValidation = isActive; + isActive = structure != null && structure.checkFrameIsValid(worldObj, xCoord, yCoord, zCoord) && structure.scanPortal(worldObj, xCoord, yCoord, zCoord, false, true); - worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); + if (isActive != isActiveBeforeValidation) { + worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); + } } public void updateState() { - if (structure == null || !isActive) + if (structure == null || !isActive) { structure = PortalHelper.getValidStructure(worldObj, xCoord, yCoord, zCoord); + } if (structure == null) { isActive = false; worldObj.notifyBlockChange(xCoord, yCoord, zCoord, blockType); @@ -55,25 +65,30 @@ public class TileDislocatorReceptacle extends TileEntity implements IInventory { return; } + final boolean isActiveBeforeUpdate = isActive; if (isActive) { - if (getStackInSlot(0) == null || !structure.checkFrameIsValid(worldObj, xCoord, yCoord, zCoord)) + if (dislocator == null || !structure.checkFrameIsValid(worldObj, xCoord, yCoord, zCoord)) { isActive = false; + } } else { - boolean frameValid = structure.checkFrameIsValid(worldObj, xCoord, yCoord, zCoord); - boolean portalEmpty = structure.scanPortal(worldObj, xCoord, yCoord, zCoord, false, false); + boolean isFrameValid = structure.checkFrameIsValid(worldObj, xCoord, yCoord, zCoord); + boolean isPortalEmpty = structure.scanPortal(worldObj, xCoord, yCoord, zCoord, false, false); - if (getLocation() != null && frameValid && portalEmpty) { + if (getLocation() != null && isFrameValid && isPortalEmpty) { isActive = true; structure.scanPortal(worldObj, xCoord, yCoord, zCoord, true, false); } } - worldObj.notifyBlockChange(xCoord, yCoord, zCoord, blockType); - worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); + if (isActive != isActiveBeforeUpdate) { + worldObj.notifyBlockChange(xCoord, yCoord, zCoord, blockType); + worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); + } } public Teleporter.TeleportLocation getLocation() { - if (getStackInSlot(0) != null && getStackInSlot(0).getItem() instanceof TeleporterMKI) - return ((TeleporterMKI) getStackInSlot(0).getItem()).getLocation(getStackInSlot(0)); + if (dislocator != null && dislocator.getItem() instanceof TeleporterMKI) { + return ((TeleporterMKI) dislocator.getItem()).getLocation(dislocator); + } return null; } @@ -100,40 +115,40 @@ public class TileDislocatorReceptacle extends TileEntity implements IInventory { } @Override - public ItemStack decrStackSize(int i, int count) { - ItemStack itemstack = getStackInSlot(i); - - if (itemstack != null) { - if (itemstack.stackSize <= count) { - setInventorySlotContents(i, null); - } else { - itemstack = itemstack.splitStack(count); - if (itemstack.stackSize == 0) { - setInventorySlotContents(i, null); - } + public ItemStack decrStackSize(int index, int count) { + ItemStack stack = null; + if (index == 0 && dislocator != null) { + stack = dislocator; + if (count >= 1) { + setInventorySlotContents(index, null); } } - if (isActive) updateState(); - else ticksTillStart = 1; - return itemstack; + return stack; } @Override - public ItemStack getStackInSlotOnClosing(int i) { - ItemStack item = getStackInSlot(i); - if (item != null) setInventorySlotContents(i, null); - return item; - } - - @Override - public void setInventorySlotContents(int i, ItemStack itemstack) { - if (i != 0) return; - dislocator = itemstack; - if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) { - itemstack.stackSize = getInventoryStackLimit(); + public ItemStack getStackInSlotOnClosing(int index) { + ItemStack stack = getStackInSlot(index); + if (stack != null) { + setInventorySlotContents(index, null); + } + return stack; + } + + @Override + public void setInventorySlotContents(int index, ItemStack stack) { + if (index != 0) { + return; + } + dislocator = stack; + if (stack != null && stack.stackSize > getInventoryStackLimit()) { + stack.stackSize = getInventoryStackLimit(); + } + if (isActive) { + updateState(); + } else { + ticksTillStart = 1; } - if (isActive) updateState(); - else ticksTillStart = 1; } @Override @@ -169,9 +184,10 @@ public class TileDislocatorReceptacle extends TileEntity implements IInventory { public void closeInventory() {} @Override - public boolean isItemValidForSlot(int i, ItemStack itemstack) { - return itemstack != null && itemstack.getItem() instanceof TeleporterMKI - && ((TeleporterMKI) itemstack.getItem()).getLocation(itemstack) != null; + public boolean isItemValidForSlot(int index, ItemStack stack) { + return index == 0 && stack != null + && stack.getItem() instanceof TeleporterMKI + && ((TeleporterMKI) stack.getItem()).getLocation(stack) != null; } @Override