diff --git a/src/main/java/WayofTime/alchemicalWizardry/common/block/BlockTeleposer.java b/src/main/java/WayofTime/alchemicalWizardry/common/block/BlockTeleposer.java index 5c3e57f2..068fa292 100644 --- a/src/main/java/WayofTime/alchemicalWizardry/common/block/BlockTeleposer.java +++ b/src/main/java/WayofTime/alchemicalWizardry/common/block/BlockTeleposer.java @@ -4,7 +4,6 @@ import java.util.Random; import net.minecraft.block.Block; import net.minecraft.block.BlockContainer; -import net.minecraft.block.BlockMobSpawner; import net.minecraft.block.BlockPortal; import net.minecraft.block.material.Material; import net.minecraft.client.renderer.texture.IIconRegister; @@ -21,13 +20,13 @@ import net.minecraftforge.common.MinecraftForge; import WayofTime.alchemicalWizardry.AlchemicalWizardry; import WayofTime.alchemicalWizardry.api.event.TeleposeEvent; +import WayofTime.alchemicalWizardry.api.items.interfaces.IBindable; import WayofTime.alchemicalWizardry.api.soulNetwork.SoulNetworkHandler; import WayofTime.alchemicalWizardry.common.demonVillage.tileEntity.TEDemonPortal; import WayofTime.alchemicalWizardry.common.items.TelepositionFocus; import WayofTime.alchemicalWizardry.common.tileEntity.TETeleposer; import codechicken.multipart.MultipartHelper; import codechicken.multipart.TileMultipart; -import cpw.mods.fml.common.Optional; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; @@ -37,10 +36,7 @@ public class BlockTeleposer extends BlockContainer { private IIcon topIcon; @SideOnly(Side.CLIENT) - private IIcon sideIcon2; - - @SideOnly(Side.CLIENT) - private IIcon bottomIcon; + private IIcon sideIcon; public BlockTeleposer() { super(Material.rock); @@ -54,18 +50,14 @@ public class BlockTeleposer extends BlockContainer { @SideOnly(Side.CLIENT) public void registerBlockIcons(IIconRegister iconRegister) { this.topIcon = iconRegister.registerIcon("AlchemicalWizardry:Teleposer_Top"); - this.sideIcon2 = iconRegister.registerIcon("AlchemicalWizardry:Teleposer_Side"); - this.bottomIcon = iconRegister.registerIcon("AlchemicalWizardry:Teleposer_Side"); + this.sideIcon = iconRegister.registerIcon("AlchemicalWizardry:Teleposer_Side"); } @Override @SideOnly(Side.CLIENT) public IIcon getIcon(int side, int meta) { - return switch (side) { - case 0 -> bottomIcon; - case 1 -> topIcon; - default -> sideIcon2; - }; + if (side == 1) return topIcon; + return sideIcon; } @Override @@ -73,23 +65,18 @@ public class BlockTeleposer extends BlockContainer { float these, float are) { ItemStack playerItem = player.getCurrentEquippedItem(); - if (playerItem != null) { - if (playerItem.getItem() instanceof TelepositionFocus) { - SoulNetworkHandler.checkAndSetItemPlayer(playerItem, player); - - if (playerItem.getTagCompound() == null) { - playerItem.setTagCompound(new NBTTagCompound()); - } - - NBTTagCompound itemTag = playerItem.getTagCompound(); - itemTag.setInteger("xCoord", x); - itemTag.setInteger("yCoord", y); - itemTag.setInteger("zCoord", z); - itemTag.setInteger("dimensionId", world.provider.dimensionId); - return true; - } + if (playerItem == null || !(playerItem.getItem() instanceof TelepositionFocus)) { + player.openGui(AlchemicalWizardry.instance, 1, world, x, y, z); + return true; } - player.openGui(AlchemicalWizardry.instance, 1, world, x, y, z); + + SoulNetworkHandler.checkAndSetItemPlayer(playerItem, player); + + NBTTagCompound itemTag = IBindable.getTag(playerItem); + itemTag.setInteger("xCoord", x); + itemTag.setInteger("yCoord", y); + itemTag.setInteger("zCoord", z); + itemTag.setInteger("dimensionId", world.provider.dimensionId); return true; } @@ -103,38 +90,36 @@ public class BlockTeleposer extends BlockContainer { Random rand = new Random(); TileEntity tileEntity = world.getTileEntity(x, y, z); - if (!(tileEntity instanceof IInventory)) { + if (!(tileEntity instanceof IInventory inventory)) { return; } - IInventory inventory = (IInventory) tileEntity; + ItemStack item = inventory.getStackInSlot(0); - for (int i = 0; i < inventory.getSizeInventory(); i++) { - ItemStack item = inventory.getStackInSlot(i); - - if (item != null && item.stackSize > 0) { - float rx = rand.nextFloat() * 0.8F + 0.1F; - float ry = rand.nextFloat() * 0.8F + 0.1F; - float rz = rand.nextFloat() * 0.8F + 0.1F; - EntityItem entityItem = new EntityItem( - world, - x + rx, - y + ry, - z + rz, - new ItemStack(item.getItem(), item.stackSize, item.getItemDamage())); - - if (item.hasTagCompound()) { - entityItem.getEntityItem().setTagCompound((NBTTagCompound) item.getTagCompound().copy()); - } - - float factor = 0.05F; - entityItem.motionX = rand.nextGaussian() * factor; - entityItem.motionY = rand.nextGaussian() * factor + 0.2F; - entityItem.motionZ = rand.nextGaussian() * factor; - world.spawnEntityInWorld(entityItem); - item.stackSize = 0; - } + if (item == null || item.stackSize <= 0) { + return; } + + float rx = rand.nextFloat() * 0.8F + 0.1F; + float ry = rand.nextFloat() * 0.8F + 0.1F; + float rz = rand.nextFloat() * 0.8F + 0.1F; + EntityItem entityItem = new EntityItem( + world, + x + rx, + y + ry, + z + rz, + new ItemStack(item.getItem(), item.stackSize, item.getItemDamage())); + + if (item.hasTagCompound()) { + entityItem.getEntityItem().setTagCompound((NBTTagCompound) item.getTagCompound().copy()); + } + + float factor = 0.05F; + entityItem.motionX = rand.nextGaussian() * factor; + entityItem.motionY = rand.nextGaussian() * factor + 0.2F; + entityItem.motionZ = rand.nextGaussian() * factor; + world.spawnEntityInWorld(entityItem); + item.stackSize = 0; } @Override @@ -142,126 +127,93 @@ public class BlockTeleposer extends BlockContainer { return new TETeleposer(); } - public static boolean swapBlocks(Object caller, World worldI, World worldF, int xi, int yi, int zi, int xf, int yf, - int zf) { - return swapBlocks(caller, worldI, worldF, xi, yi, zi, xf, yf, zf, true, 3); + public static boolean swapBlocks(Object caller, World worldA, World worldB, int xA, int yA, int zA, int xB, int yB, + int zB) { + return swapBlocks(caller, worldA, worldB, xA, yA, zA, xB, yB, zB, true, 3); } - public static boolean swapBlocksWithoutSound(Object caller, World worldI, World worldF, int xi, int yi, int zi, - int xf, int yf, int zf) { - return swapBlocks(caller, worldI, worldF, xi, yi, zi, xf, yf, zf, false, 3); + public static boolean swapBlocksWithoutSound(Object caller, World worldA, World worldB, int xA, int yA, int zA, + int xB, int yB, int zB) { + return swapBlocks(caller, worldA, worldB, xA, yA, zA, xB, yB, zB, false, 3); } - public static boolean swapBlocks(Object caller, World worldI, World worldF, int xi, int yi, int zi, int xf, int yf, - int zf, boolean doSound, int flag) { - TileEntity tileEntityI = worldI.getTileEntity(xi, yi, zi); - TileEntity tileEntityF = worldF.getTileEntity(xf, yf, zf); + public static boolean swapBlocks(Object caller, World worldA, World worldB, int xA, int yA, int zA, int xB, int yB, + int zB, boolean doSound, int flag) { + TileEntity tileA = worldA.getTileEntity(xA, yA, zA); + TileEntity tileB = worldB.getTileEntity(xB, yB, zB); - NBTTagCompound nbttag1 = new NBTTagCompound(); - NBTTagCompound nbttag2 = new NBTTagCompound(); + NBTTagCompound nbtA = new NBTTagCompound(); + NBTTagCompound nbtB = new NBTTagCompound(); - if (tileEntityI != null) { - tileEntityI.writeToNBT(nbttag1); - } + if (tileA != null) tileA.writeToNBT(nbtA); + if (tileB != null) tileB.writeToNBT(nbtB); - if (tileEntityF != null) { - tileEntityF.writeToNBT(nbttag2); - } + Block blockA = worldA.getBlock(xA, yA, zA); + Block blockB = worldB.getBlock(xB, yB, zB); - Block blockI = worldI.getBlock(xi, yi, zi); - Block blockF = worldF.getBlock(xf, yf, zf); + if (blockA.equals(Blocks.air) && blockB.equals(Blocks.air)) return false; - if (blockI.equals(Blocks.air) && blockF.equals(Blocks.air)) { + if (!(caller instanceof TEDemonPortal) && (blockA instanceof BlockPortal || blockB instanceof BlockPortal)) return false; - } - if (blockI instanceof BlockMobSpawner || blockF instanceof BlockMobSpawner || caller instanceof TEDemonPortal - ? false - : blockI instanceof BlockPortal || blockF instanceof BlockPortal) { - return false; - } + int metaA = worldA.getBlockMetadata(xA, yA, zA); + int metaB = worldB.getBlockMetadata(xB, yB, zB); - int metaI = worldI.getBlockMetadata(xi, yi, zi); - int metaF = worldF.getBlockMetadata(xf, yf, zf); - - TeleposeEvent evt = new TeleposeEvent(worldI, xi, yi, zi, blockI, metaI, worldF, xf, yf, zf, blockF, metaF); + TeleposeEvent evt = new TeleposeEvent(worldA, xA, yA, zA, blockA, metaA, worldB, xB, yB, zB, blockB, metaB); if (MinecraftForge.EVENT_BUS.post(evt)) return false; if (doSound) { - worldI.playSoundEffect(xi, yi, zi, "mob.endermen.portal", 1.0F, 1.0F); - worldF.playSoundEffect(xf, yf, zf, "mob.endermen.portal", 1.0F, 1.0F); + String sound = "mob.endermen.portal"; + worldA.playSoundEffect(xA, yA, zA, sound, 1.0F, 1.0F); + worldB.playSoundEffect(xB, yB, zB, sound, 1.0F, 1.0F); } - // CLEAR TILES - Block finalBlock = blockF; + // Clear current tile entities + worldB.setTileEntity(xB, yB, zB, blockB.createTileEntity(worldB, metaB)); + worldA.setTileEntity(xA, yA, zA, blockA.createTileEntity(worldA, metaA)); - if (finalBlock != null) { - TileEntity tileToSet = finalBlock.createTileEntity(worldF, metaF); + // Swap blocks + worldB.setBlock(xB, yB, zB, blockA, metaA, flag); + worldA.setBlock(xA, yA, zA, blockB, metaB, flag); - worldF.setTileEntity(xf, yf, zf, tileToSet); - } - - if (blockI != null) { - TileEntity tileToSet = blockI.createTileEntity(worldI, metaI); - - worldI.setTileEntity(xi, yi, zi, tileToSet); - } - - // TILES CLEARED - worldF.setBlock(xf, yf, zf, blockI, metaI, flag); - - if (tileEntityI != null) { - TileEntity newTileEntityI = TileEntity.createAndLoadEntity(nbttag1); - - if (AlchemicalWizardry.isFMPLoaded && isMultipart(tileEntityI)) { - newTileEntityI = createMultipartFromNBT(worldF, nbttag1); - } - - worldF.setTileEntity(xf, yf, zf, newTileEntityI); - - newTileEntityI.xCoord = xf; - newTileEntityI.yCoord = yf; - newTileEntityI.zCoord = zf; - - if (AlchemicalWizardry.isFMPLoaded && isMultipart(tileEntityI)) { - sendDescriptorOfTile(worldF, newTileEntityI); - } - } - - worldI.setBlock(xi, yi, zi, finalBlock, metaF, flag); - - if (tileEntityF != null) { - TileEntity newTileEntityF = TileEntity.createAndLoadEntity(nbttag2); - if (AlchemicalWizardry.isFMPLoaded && isMultipart(tileEntityF)) { - newTileEntityF = createMultipartFromNBT(worldI, nbttag2); - } - - worldI.setTileEntity(xi, yi, zi, newTileEntityF); - - newTileEntityF.xCoord = xi; - newTileEntityF.yCoord = yi; - newTileEntityF.zCoord = zi; - - if (AlchemicalWizardry.isFMPLoaded && isMultipart(tileEntityF)) { - sendDescriptorOfTile(worldI, newTileEntityF); - } - } + // Restore tile entities + swapTile(worldB, xB, yB, zB, tileA, nbtA); + swapTile(worldA, xA, yA, zA, tileB, nbtB); return true; } - @Optional.Method(modid = "ForgeMultipart") - public static boolean isMultipart(TileEntity tile) { - return tile instanceof TileMultipart; + private static void swapTile(World world, int x, int y, int z, TileEntity oldTile, NBTTagCompound nbt) { + if (oldTile == null) return; + + TileEntity newTile; + + boolean multipart = AlchemicalWizardry.isFMPLoaded && oldTile instanceof TileMultipart; + if (multipart) { + newTile = MultipartHelper.createTileFromNBT(world, nbt); + } else { + newTile = TileEntity.createAndLoadEntity(nbt); + } + + world.setTileEntity(x, y, z, newTile); + + newTile.xCoord = x; + newTile.yCoord = y; + newTile.zCoord = z; + + if (multipart) MultipartHelper.sendDescPacket(world, newTile); } - @Optional.Method(modid = "ForgeMultipart") - public static TileEntity createMultipartFromNBT(World world, NBTTagCompound tag) { - return MultipartHelper.createTileFromNBT(world, tag); - } - - @Optional.Method(modid = "ForgeMultipart") - public static void sendDescriptorOfTile(World world, TileEntity tile) { - MultipartHelper.sendDescPacket(world, tile); + @Override + public void onNeighborBlockChange(World world, int x, int y, int z, Block neighbor) { + if (!(world.getTileEntity(x, y, z) instanceof TETeleposer teleposer)) return; + // was it a low->high pulse + boolean isPowered = world.isBlockIndirectlyGettingPowered(x, y, z); + if (!teleposer.hasRedstone && isPowered) { + teleposer.hasRedstone = true; + teleposer.activate(); + } else { + teleposer.hasRedstone = isPowered; + } } } diff --git a/src/main/java/WayofTime/alchemicalWizardry/common/items/EnergyItems.java b/src/main/java/WayofTime/alchemicalWizardry/common/items/EnergyItems.java index 727f3f81..a58f08f7 100644 --- a/src/main/java/WayofTime/alchemicalWizardry/common/items/EnergyItems.java +++ b/src/main/java/WayofTime/alchemicalWizardry/common/items/EnergyItems.java @@ -133,25 +133,26 @@ public class EnergyItems extends Item implements IBindable { } public static boolean canSyphonInContainer(ItemStack ist, int damageToBeDone) { - if (ist.getTagCompound() != null && !(ist.getTagCompound().getString("ownerName").equals(""))) { - String ownerName = ist.getTagCompound().getString("ownerName"); - - if (MinecraftServer.getServer() == null) { - return false; - } - - World world = MinecraftServer.getServer().worldServers[0]; - LifeEssenceNetwork data = (LifeEssenceNetwork) world.loadItemData(LifeEssenceNetwork.class, ownerName); - - if (data == null) { - data = new LifeEssenceNetwork(ownerName); - world.setItemData(ownerName, data); - } - - return data.currentEssence >= damageToBeDone; + if (!(ist.getItem() instanceof IBindable)) { + return false; } - return false; + String ownerName = IBindable.getOwnerName(ist); + MinecraftServer server = MinecraftServer.getServer(); + + if (ownerName.isEmpty() || server == null) { + return false; + } + + World world = server.worldServers[0]; + LifeEssenceNetwork data = (LifeEssenceNetwork) world.loadItemData(LifeEssenceNetwork.class, ownerName); + + if (data == null) { + data = new LifeEssenceNetwork(ownerName); + world.setItemData(ownerName, data); + } + + return data.currentEssence >= damageToBeDone; } public static void hurtPlayer(EntityPlayer user, int energySyphoned) { diff --git a/src/main/java/WayofTime/alchemicalWizardry/common/items/TelepositionFocus.java b/src/main/java/WayofTime/alchemicalWizardry/common/items/TelepositionFocus.java index c77fe4ba..1ae45072 100644 --- a/src/main/java/WayofTime/alchemicalWizardry/common/items/TelepositionFocus.java +++ b/src/main/java/WayofTime/alchemicalWizardry/common/items/TelepositionFocus.java @@ -17,7 +17,7 @@ import cpw.mods.fml.relauncher.SideOnly; public class TelepositionFocus extends EnergyItems { - private int focusLevel; + private final int focusLevel; public TelepositionFocus(int focusLevel) { super(); @@ -33,7 +33,8 @@ public class TelepositionFocus extends EnergyItems { } @Override - public void addInformation(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, List par3List, boolean par4) { + public void addInformation(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, List par3List, + boolean par4) { par3List.add(StatCollector.translateToLocal("tooltip.telepositionfocus.desc")); addBindingInformation(par1ItemStack, par3List); @@ -50,20 +51,12 @@ public class TelepositionFocus extends EnergyItems { @Override public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer) { - if (IBindable.checkAndSetItemOwner(par1ItemStack, par3EntityPlayer)) { - if (par3EntityPlayer.isSneaking()) { - return par1ItemStack; - } - } + IBindable.checkAndSetItemOwner(par1ItemStack, par3EntityPlayer); return par1ItemStack; } public int getDimensionID(ItemStack itemStack) { - if (itemStack.getTagCompound() == null) { - itemStack.setTagCompound(new NBTTagCompound()); - } - - return itemStack.getTagCompound().getInteger("dimensionId"); + return IBindable.getTag(itemStack).getInteger("dimensionId"); } public World getWorld(ItemStack itemStack) { @@ -72,27 +65,15 @@ public class TelepositionFocus extends EnergyItems { } public int xCoord(ItemStack itemStack) { - if (!(itemStack.getTagCompound() == null)) { - return itemStack.getTagCompound().getInteger("xCoord"); - } else { - return 0; - } + return IBindable.getTag(itemStack).getInteger("xCoord"); } public int yCoord(ItemStack itemStack) { - if (!(itemStack.getTagCompound() == null)) { - return itemStack.getTagCompound().getInteger("yCoord"); - } else { - return 0; - } + return IBindable.getTag(itemStack).getInteger("yCoord"); } public int zCoord(ItemStack itemStack) { - if (!(itemStack.getTagCompound() == null)) { - return itemStack.getTagCompound().getInteger("zCoord"); - } else { - return 0; - } + return IBindable.getTag(itemStack).getInteger("zCoord"); } public int getFocusLevel() { diff --git a/src/main/java/WayofTime/alchemicalWizardry/common/spell/complex/effect/SpellHelper.java b/src/main/java/WayofTime/alchemicalWizardry/common/spell/complex/effect/SpellHelper.java index 63debc80..18aff51a 100644 --- a/src/main/java/WayofTime/alchemicalWizardry/common/spell/complex/effect/SpellHelper.java +++ b/src/main/java/WayofTime/alchemicalWizardry/common/spell/complex/effect/SpellHelper.java @@ -1,7 +1,6 @@ package WayofTime.alchemicalWizardry.common.spell.complex.effect; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import java.util.Random; import java.util.regex.Pattern; @@ -26,17 +25,15 @@ import net.minecraft.item.crafting.FurnaceRecipes; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.network.play.server.S06PacketUpdateHealth; -import net.minecraft.network.play.server.S07PacketRespawn; -import net.minecraft.network.play.server.S1DPacketEntityEffect; import net.minecraft.network.play.server.S1FPacketSetExperience; import net.minecraft.potion.Potion; -import net.minecraft.potion.PotionEffect; import net.minecraft.server.MinecraftServer; -import net.minecraft.server.management.ServerConfigurationManager; import net.minecraft.util.AxisAlignedBB; +import net.minecraft.util.ChatComponentTranslation; import net.minecraft.util.DamageSource; import net.minecraft.util.MovingObjectPosition; import net.minecraft.util.Vec3; +import net.minecraft.world.Teleporter; import net.minecraft.world.World; import net.minecraft.world.WorldServer; import net.minecraftforge.common.util.FakePlayer; @@ -52,7 +49,6 @@ import WayofTime.alchemicalWizardry.api.soulNetwork.SoulNetworkHandler; import WayofTime.alchemicalWizardry.api.spell.APISpellHelper; import WayofTime.alchemicalWizardry.common.NewPacketHandler; import WayofTime.alchemicalWizardry.common.items.sigil.SigilDivination; -import cpw.mods.fml.common.FMLCommonHandler; public class SpellHelper { @@ -756,89 +752,64 @@ public class SpellHelper { return null; } - // Adapated from Enhanced Portals 3 code - public static Entity teleportEntityToDim(World oldWorld, int newWorldID, double d, double e, double f, + public static void teleportEntityToDim(World oldWorld, int newWorldID, double x, double y, double z, Entity entity) { - if (entity != null) { - if (entity.timeUntilPortal <= 0) { - WorldServer oldWorldServer = MinecraftServer.getServer().worldServerForDimension(entity.dimension); - WorldServer newWorldServer = MinecraftServer.getServer().worldServerForDimension(newWorldID); - if (entity instanceof EntityPlayer) { - EntityPlayerMP player = (EntityPlayerMP) entity; - if (!player.worldObj.isRemote) { - player.worldObj.theProfiler.startSection("portal"); - player.worldObj.theProfiler.startSection("changeDimension"); - ServerConfigurationManager config = player.mcServer.getConfigurationManager(); - oldWorld.playSoundEffect( - player.posX, - player.posY, - player.posZ, - "mob.endermen.portal", - 1.0F, - 1.0F); - player.closeScreen(); - player.dimension = newWorldServer.provider.dimensionId; - player.playerNetServerHandler.sendPacket( - new S07PacketRespawn( - player.dimension, - player.worldObj.difficultySetting, - newWorldServer.getWorldInfo().getTerrainType(), - player.theItemInWorldManager.getGameType())); - oldWorldServer.removeEntity(player); - player.isDead = false; - player.setLocationAndAngles(d, e, f, player.rotationYaw, player.rotationPitch); - newWorldServer.spawnEntityInWorld(player); - player.setWorld(newWorldServer); - config.func_72375_a(player, oldWorldServer); - player.playerNetServerHandler - .setPlayerLocation(d, e, f, entity.rotationYaw, entity.rotationPitch); - player.theItemInWorldManager.setWorld(newWorldServer); - config.updateTimeAndWeatherForPlayer(player, newWorldServer); - config.syncPlayerInventory(player); - player.worldObj.theProfiler.endSection(); - oldWorldServer.resetUpdateEntityTick(); - newWorldServer.resetUpdateEntityTick(); - player.worldObj.theProfiler.endSection(); - for (Iterator potion = player.getActivePotionEffects().iterator(); potion - .hasNext();) { - player.playerNetServerHandler - .sendPacket(new S1DPacketEntityEffect(player.getEntityId(), potion.next())); - } - player.playerNetServerHandler.sendPacket( - new S1FPacketSetExperience( - player.experience, - player.experienceTotal, - player.experienceLevel)); - FMLCommonHandler.instance().firePlayerChangedDimensionEvent( - player, - oldWorldServer.provider.dimensionId, - player.dimension); - player.timeUntilPortal = 150; - } - player.worldObj.theProfiler.endSection(); - newWorldServer.playSoundEffect(d, e, f, "mob.endermen.portal", 1.0F, 1.0F); - return player; - } else { - NBTTagCompound tag = new NBTTagCompound(); - entity.writeToNBTOptional(tag); - entity.setDead(); - oldWorld.playSoundEffect(entity.posX, entity.posY, entity.posZ, "mob.endermen.portal", 1.0F, 1.0F); - Entity teleportedEntity = EntityList.createEntityFromNBT(tag, newWorldServer); - if (teleportedEntity != null) { - teleportedEntity.setLocationAndAngles(d, e, f, entity.rotationYaw, entity.rotationPitch); - teleportedEntity.forceSpawn = true; - newWorldServer.spawnEntityInWorld(teleportedEntity); - teleportedEntity.setWorld(newWorldServer); - teleportedEntity.timeUntilPortal = 150; - } - oldWorldServer.resetUpdateEntityTick(); - newWorldServer.resetUpdateEntityTick(); - newWorldServer.playSoundEffect(d, e, f, "mob.endermen.portal", 1.0F, 1.0F); - return teleportedEntity; - } - } + if (entity == null || entity.timeUntilPortal > 0) return; + + MinecraftServer server = MinecraftServer.getServer(); + WorldServer oldWorldServer = server.worldServerForDimension(entity.dimension); + WorldServer newWorldServer = server.worldServerForDimension(newWorldID); + + oldWorld.playSoundEffect(entity.posX, entity.posY, entity.posZ, "mob.endermen.portal", 1.0F, 1.0F); + + if (entity instanceof EntityPlayerMP player) { + teleportPlayer(player, newWorldServer, x, y, z); + } else { + teleportNonPlayer(entity, oldWorldServer, newWorldServer, x, y, z); } - return null; + + newWorldServer.playSoundEffect(x, y, z, "mob.endermen.portal", 1.0F, 1.0F); + } + + private static void teleportPlayer(EntityPlayerMP player, WorldServer newWorldServer, double x, double y, + double z) { + if (player.worldObj.isRemote) return; + + // Teleporting to the Spirit World from Witchery causes the player to get stuck until exiting the world. + // Blocks and mobs are fine, so only player TPs need to be canceled. + // TODO: Figure out a better fix for this instead of just preventing it. + if (newWorldServer.provider.getDimensionName().equals("Spirit World")) { + player.addChatComponentMessage(new ChatComponentTranslation("message.warn.spiritWorld")); + return; + } + + player.mcServer.getConfigurationManager().transferPlayerToDimension( + player, + newWorldServer.provider.dimensionId, + new BMTeleporter(newWorldServer, x, y, z)); + + player.timeUntilPortal = 20; + player.playerNetServerHandler.sendPacket( + new S1FPacketSetExperience(player.experience, player.experienceTotal, player.experienceLevel)); + } + + private static void teleportNonPlayer(Entity entity, WorldServer oldWorldServer, WorldServer newWorldServer, + double x, double y, double z) { + NBTTagCompound tag = new NBTTagCompound(); + entity.writeToNBTOptional(tag); + entity.setDead(); + + Entity newEntity = EntityList.createEntityFromNBT(tag, newWorldServer); + if (newEntity != null) { + newEntity.setLocationAndAngles(x, y, z, entity.rotationYaw, entity.rotationPitch); + newEntity.forceSpawn = true; + newWorldServer.spawnEntityInWorld(newEntity); + newEntity.setWorld(newWorldServer); + newEntity.timeUntilPortal = 20; + } + + oldWorldServer.resetUpdateEntityTick(); + newWorldServer.resetUpdateEntityTick(); } public static boolean areItemStacksEqual(ItemStack stack, ItemStack compressedStack) { @@ -926,4 +897,34 @@ public class SpellHelper { return damage; } + + private static class BMTeleporter extends Teleporter { + + private final double x; + private final double y; + private final double z; + + public BMTeleporter(WorldServer worldServer, double x, double y, double z) { + super(worldServer); + this.x = x; + this.y = y; + this.z = z; + } + + @Override + public void placeInPortal(Entity entity, double x, double y, double z, float rotationYaw) { + entity.setLocationAndAngles(this.x, this.y, this.z, entity.rotationYaw, entity.rotationPitch); + entity.motionX = entity.motionY = entity.motionZ = 0.0D; + } + + @Override + public boolean placeInExistingPortal(Entity entity, double x, double y, double z, float rotationYaw) { + return false; + } + + @Override + public boolean makePortal(Entity entity) { + return false; + } + } } diff --git a/src/main/java/WayofTime/alchemicalWizardry/common/tileEntity/TETeleposer.java b/src/main/java/WayofTime/alchemicalWizardry/common/tileEntity/TETeleposer.java index 4c214ee1..5df54614 100644 --- a/src/main/java/WayofTime/alchemicalWizardry/common/tileEntity/TETeleposer.java +++ b/src/main/java/WayofTime/alchemicalWizardry/common/tileEntity/TETeleposer.java @@ -1,18 +1,17 @@ package WayofTime.alchemicalWizardry.common.tileEntity; -import java.util.Iterator; import java.util.List; import net.minecraft.entity.EntityLivingBase; -import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.Packet; -import net.minecraft.tileentity.TileEntity; import net.minecraft.util.AxisAlignedBB; +import net.minecraft.util.StatCollector; import net.minecraft.world.World; +import WayofTime.alchemicalWizardry.api.items.interfaces.IBindable; import WayofTime.alchemicalWizardry.api.soulNetwork.SoulNetworkHandler; import WayofTime.alchemicalWizardry.common.NewPacketHandler; import WayofTime.alchemicalWizardry.common.block.BlockTeleposer; @@ -30,16 +29,14 @@ public class TETeleposer extends TEInventory implements IBloodMagicWailaProvider private int resultID; private int resultDamage; - private int previousInput; - private boolean isActive; + public boolean hasRedstone; public TETeleposer() { super(sizeInv); resultID = 0; resultDamage = 0; - isActive = false; - previousInput = 0; + hasRedstone = false; } @Override @@ -48,8 +45,7 @@ public class TETeleposer extends TEInventory implements IBloodMagicWailaProvider resultID = par1NBTTagCompound.getInteger("resultID"); resultDamage = par1NBTTagCompound.getInteger("resultDamage"); - isActive = par1NBTTagCompound.getBoolean("isActive"); - previousInput = par1NBTTagCompound.getInteger("previousInput"); + hasRedstone = par1NBTTagCompound.getBoolean("hasRedstone"); } @Override @@ -58,8 +54,7 @@ public class TETeleposer extends TEInventory implements IBloodMagicWailaProvider par1NBTTagCompound.setInteger("resultID", resultID); par1NBTTagCompound.setInteger("resultDamage", resultDamage); - par1NBTTagCompound.setBoolean("isActive", isActive); - par1NBTTagCompound.setInteger("previousInput", previousInput); + par1NBTTagCompound.setBoolean("hasRedstone", hasRedstone); } @Override @@ -72,167 +67,131 @@ public class TETeleposer extends TEInventory implements IBloodMagicWailaProvider return 1; } - // Logic for the actual block is under here - @Override - public void updateEntity() { - super.updateEntity(); + public void activate() { + if (worldObj.isRemote) return; - if (worldObj.isRemote) { + ItemStack focus = this.getStackInSlot(0); + + if (focus == null || !(focus.getItem() instanceof TelepositionFocus focusItem)) { return; } - int currentInput = worldObj.getBlockPowerInput(xCoord, yCoord, zCoord); + World targetWorld = focusItem.getWorld(focus); + if (targetWorld == null) return; - if (previousInput == 0 && currentInput != 0) { - ItemStack focus = this.getStackInSlot(0); + int x = focusItem.xCoord(focus); + int y = focusItem.yCoord(focus); + int z = focusItem.zCoord(focus); - if (focus != null && focus.getItem() instanceof TelepositionFocus) { - TelepositionFocus focusItem = (TelepositionFocus) (focus.getItem()); - int xf = focusItem.xCoord(focus); - int yf = focusItem.yCoord(focus); - int zf = focusItem.zCoord(focus); - World worldF = focusItem.getWorld(focus); - int damage = (int) (0.5f * Math.sqrt( - (xCoord - xf) * (xCoord - xf) + (yCoord - yf + 1) * (yCoord - yf + 1) - + (zCoord - zf) * (zCoord - zf))); - int focusLevel = focusItem.getFocusLevel(); - int transportCount = 0; - int entityCount = 0; + if (!(targetWorld.getTileEntity(x, y, z) instanceof TETeleposer target) || target == this) { + return; + } - if (worldF != null && worldF.getTileEntity(xf, yf, zf) instanceof TETeleposer - && !worldF.getTileEntity(xf, yf, zf).equals(this)) { - // Prime the teleportation - int d0 = focusLevel - 1; - AxisAlignedBB axisalignedbb1 = AxisAlignedBB.getBoundingBox( - (double) this.xCoord - 0.5, - (double) this.yCoord + d0 + 0.5, - (double) this.zCoord - 0.5, - (double) (this.xCoord + 0.5), - (double) (this.yCoord + 1.5 + d0), - (double) (this.zCoord + 0.5)).expand(d0, d0, d0); - axisalignedbb1.maxY = Math.min((double) this.worldObj.getHeight(), this.yCoord + 2 + d0 + d0); - List list1 = this.worldObj.getEntitiesWithinAABB(EntityLivingBase.class, axisalignedbb1); - Iterator iterator1 = list1.iterator(); - EntityLivingBase entityplayer1; + int damage = (int) (0.5f + * Math.sqrt(Math.pow(xCoord - x, 2) + Math.pow(yCoord - y + 1, 2) + Math.pow(zCoord - z, 2))); - while (iterator1.hasNext()) { - entityplayer1 = (EntityLivingBase) iterator1.next(); - entityCount++; - } + int size = focusItem.getFocusLevel(); - AxisAlignedBB axisalignedbb2 = AxisAlignedBB - .getBoundingBox(xf - 0.5, yf + d0 + 0.5, zf - 0.5, xf + 0.5, yf + 1.5 + d0, zf + 0.5) - .expand(d0, d0, d0); - List list2 = worldF.getEntitiesWithinAABB(EntityLivingBase.class, axisalignedbb2); - Iterator iterator2 = list2.iterator(); - EntityLivingBase entityplayer2; + List localEntities = getEntitiesInArea(worldObj, xCoord, yCoord, zCoord, size); + List targetEntities = getEntitiesInArea(targetWorld, x, y, z, size); - while (iterator2.hasNext()) { - entityplayer2 = (EntityLivingBase) iterator2.next(); - entityCount++; - } + int entityCount = localEntities.size() + targetEntities.size(); - if (EnergyItems.canSyphonInContainer( - focus, - damage * (focusLevel * 2 - 1) * (focusLevel * 2 - 1) * (focusLevel * 2 - 1) - + damage * entityCount)) { - for (int k = 0; k <= (focusLevel * 2 - 2); k++) - for (int i = -(focusLevel - 1); i <= (focusLevel - 1); i++) { - for (int j = -(focusLevel - 1); j <= (focusLevel - 1); j++) { - { - if (BlockTeleposer.swapBlocks( - this, - worldObj, - worldF, - xCoord + i, - yCoord + 1 + k, - zCoord + j, - xf + i, - yf + 1 + k, - zf + j)) { - transportCount++; - } - } - } - } + if (!EnergyItems.canSyphonInContainer(focus, damage * entityCount)) return; - if (!worldF.equals(worldObj)) { - entityCount = 0; - } + int movedBlocks = moveBlocks(size, targetWorld, x, y, z); - // Teleport with minimum cost of 2000 LP. - int cost = Math.max(damage * transportCount + damage * entityCount, 2000); - SoulNetworkHandler.syphonFromNetwork(focus, cost); + int cost = Math.max(damage * movedBlocks + damage * entityCount, 2000); - if (worldF.equals(worldObj)) { - iterator1 = list1.iterator(); - iterator2 = list2.iterator(); + SoulNetworkHandler.syphonFromNetwork(focus, cost); - while (iterator1.hasNext()) { - entityplayer1 = (EntityLivingBase) iterator1.next(); - entityplayer1.worldObj = worldF; - entityplayer1.setPositionAndUpdate( - entityplayer1.posX - this.xCoord + xf, - entityplayer1.posY - this.yCoord + yf, - entityplayer1.posZ - this.zCoord + zf); - } + if (targetWorld.equals(worldObj)) { + teleportEntitiesSameWorld(localEntities, this.xCoord, this.yCoord, this.zCoord, x, y, z); + teleportEntitiesSameWorld(targetEntities, x, y, z, this.xCoord, this.yCoord, this.zCoord); + } else { + teleportEntitiesToOtherWorld( + localEntities, + worldObj, + this.xCoord, + this.yCoord, + this.zCoord, + targetWorld, + x, + y, + z); + teleportEntitiesToOtherWorld( + targetEntities, + targetWorld, + x, + y, + z, + worldObj, + this.xCoord, + this.yCoord, + this.zCoord); - while (iterator2.hasNext()) { - entityplayer2 = (EntityLivingBase) iterator2.next(); - entityplayer2.worldObj = worldF; - entityplayer2.setPositionAndUpdate( - entityplayer2.posX + this.xCoord - xf, - entityplayer2.posY + this.yCoord - yf, - entityplayer2.posZ + this.zCoord - zf); - } - } else { - iterator1 = list1.iterator(); - iterator2 = list2.iterator(); + } + } - while (iterator1.hasNext()) { - entityplayer1 = (EntityLivingBase) iterator1.next(); - SpellHelper.teleportEntityToDim( - worldObj, - worldF.provider.dimensionId, - entityplayer1.posX - this.xCoord + xf, - entityplayer1.posY - this.yCoord + yf, - entityplayer1.posZ - this.zCoord + zf, - entityplayer1); - // entityplayer1.worldObj = worldF; - // entityplayer1.setPositionAndUpdate(entityplayer1.posX - // - this.xCoord + xf, entityplayer1.posY - this.yCoord + yf, entityplayer1.posZ - - // this.zCoord + zf); - } + private List getEntitiesInArea(World world, int x, int y, int z, int size) { + double half = (size * 2 - 1) / 2.0; - while (iterator2.hasNext()) { - entityplayer2 = (EntityLivingBase) iterator2.next(); - SpellHelper.teleportEntityToDim( - worldF, - worldObj.provider.dimensionId, - entityplayer2.posX + this.xCoord - xf, - entityplayer2.posY + this.yCoord - yf, - entityplayer2.posZ + this.zCoord - zf, - entityplayer2); - // entityplayer2.worldObj = worldF; - // entityplayer2.setPositionAndUpdate(entityplayer2.posX - // + this.xCoord - xf, entityplayer2.posY + this.yCoord - yf, entityplayer2.posZ + - // this.zCoord - zf); - } - } + AxisAlignedBB box = AxisAlignedBB.getBoundingBox( + x + 0.5 - half, + y + 1, + z + 0.5 - half, + x + 0.5 + half, + y + 1 + (size * 2 - 1), + z + 0.5 + half); + + return world.getEntitiesWithinAABB(EntityLivingBase.class, box); + } + + private int moveBlocks(int focusLevel, World targetWorld, int x, int y, int z) { + int transportCount = 0; + int range = focusLevel - 1; + + for (int k = 0; k <= (focusLevel * 2 - 2); k++) { + for (int i = -range; i <= range; i++) { + for (int j = -range; j <= range; j++) { + if (BlockTeleposer.swapBlocks( + this, + worldObj, + targetWorld, + xCoord + i, + yCoord + 1 + k, + zCoord + j, + x + i, + y + 1 + k, + z + j)) { + transportCount++; } } } } - - previousInput = currentInput; + return transportCount; } - public void setActive() { - isActive = false; + private void teleportEntitiesSameWorld(List list, int sourceX, int sourceY, int sourceZ, + int destX, int destY, int destZ) { + String sound = "mob.endermen.portal"; + for (EntityLivingBase e : list) { + e.setPositionAndUpdate(e.posX - sourceX + destX, e.posY - sourceY + destY, e.posZ - sourceZ + destZ); + e.worldObj.playSoundEffect(e.posX, e.posY, e.posZ, sound, 1.0F, 1.0F); + } } - public boolean isActive() { - return isActive; + private void teleportEntitiesToOtherWorld(List list, World sourceWorld, int sourceX, int sourceY, + int sourceZ, World destWorld, int destX, int destY, int destZ) { + for (EntityLivingBase e : list) { + SpellHelper.teleportEntityToDim( + sourceWorld, + destWorld.provider.dimensionId, + e.posX - sourceX + destX, + e.posY - sourceY + destY, + e.posZ - sourceZ + destZ, + e); + } } @Override @@ -247,11 +206,8 @@ public class TETeleposer extends TEInventory implements IBloodMagicWailaProvider if (intData.length == 3) { for (int i = 0; i < 1; i++) { - if (intData[i * 3 + 2] != 0) { - ItemStack is = new ItemStack( - Item.getItemById(intData[i * 3]), - intData[i * 3 + 2], - intData[i * 3 + 1]); + if (intData[2] != 0) { + ItemStack is = new ItemStack(Item.getItemById(intData[i * 3]), intData[2], intData[1]); inv[i] = is; } else { inv[i] = null; @@ -261,18 +217,17 @@ public class TETeleposer extends TEInventory implements IBloodMagicWailaProvider } public int[] buildIntDataList() { - int[] sortList = new int[3]; // 1 * 3 - int pos = 0; + int[] sortList = new int[3]; for (ItemStack is : inv) { if (is != null) { - sortList[pos++] = Item.getIdFromItem(is.getItem()); - sortList[pos++] = is.getItemDamage(); - sortList[pos++] = is.stackSize; + sortList[0] = Item.getIdFromItem(is.getItem()); + sortList[1] = is.getItemDamage(); + sortList[2] = is.stackSize; } else { - sortList[pos++] = 0; - sortList[pos++] = 0; - sortList[pos++] = 0; + for (int i = 0; i < 3; i++) { + sortList[i] = 0; + } } } @@ -289,12 +244,20 @@ public class TETeleposer extends TEInventory implements IBloodMagicWailaProvider IWailaConfigHandler config) { if (!config.getConfig(BloodMagicWailaPlugin.WAILA_CONFIG_TELEPOSER)) return; final ItemStack contained = getStackInSlot(0); - if (contained != null) { + if (contained.getItem() instanceof TelepositionFocus focus) { currenttip.add(contained.getDisplayName()); + + NBTTagCompound itemTag = IBindable.getTag(contained); + currenttip.add( + StatCollector.translateToLocal("tooltip.alchemy.coords") + " " + + itemTag.getInteger("xCoord") + + ", " + + itemTag.getInteger("yCoord") + + ", " + + itemTag.getInteger("zCoord")); + currenttip.add( + StatCollector.translateToLocal("tooltip.alchemy.dimension") + " " + + focus.getDimensionID(contained)); } } - - @Override - public void getWailaNBTData(final EntityPlayerMP player, final TileEntity tile, final NBTTagCompound tag, - final World world, int x, int y, int z) {} } diff --git a/src/main/resources/assets/alchemicalwizardry/lang/en_US.lang b/src/main/resources/assets/alchemicalwizardry/lang/en_US.lang index efab1ad1..63d1898a 100644 --- a/src/main/resources/assets/alchemicalwizardry/lang/en_US.lang +++ b/src/main/resources/assets/alchemicalwizardry/lang/en_US.lang @@ -579,6 +579,7 @@ message.tanksegmenter.nowhas=Tank now has message.tanksegmenter.setto=Tank Segmenter now set to: message.tanksegmenter.tankssetto=tank(s) set to: message.routerfocus.limit=Focus' Item Limit set to: +message.warn.spiritWorld=Teleporting to Witchery's Spirit World causes problems and is temporarily disabled. #Achievements achievement.alchemicalwizardry:firstPrick=Your First Prick!