diff options
author | Glease <4586901+Glease@users.noreply.github.com> | 2022-03-04 19:54:59 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-04 12:54:59 +0100 |
commit | 09362b416f3770559669569c0e9519a2973aed62 (patch) | |
tree | b4cad01f06e0890f88ebb02d69ea5023259db365 /src/main/java/gtPlusPlus/core | |
parent | dcdd3d7352a4735b9beea633a897eba2eba93579 (diff) | |
download | GT5-Unofficial-09362b416f3770559669569c0e9519a2973aed62.tar.gz GT5-Unofficial-09362b416f3770559669569c0e9519a2973aed62.tar.bz2 GT5-Unofficial-09362b416f3770559669569c0e9519a2973aed62.zip |
magic feather clean up (#144)
Diffstat (limited to 'src/main/java/gtPlusPlus/core')
-rw-r--r-- | src/main/java/gtPlusPlus/core/item/general/ItemMagicFeather.java | 142 |
1 files changed, 58 insertions, 84 deletions
diff --git a/src/main/java/gtPlusPlus/core/item/general/ItemMagicFeather.java b/src/main/java/gtPlusPlus/core/item/general/ItemMagicFeather.java index 4d34bf84e7..91b2ab2c41 100644 --- a/src/main/java/gtPlusPlus/core/item/general/ItemMagicFeather.java +++ b/src/main/java/gtPlusPlus/core/item/general/ItemMagicFeather.java @@ -1,10 +1,12 @@ package gtPlusPlus.core.item.general; +import java.lang.ref.WeakReference; import java.util.*; import cpw.mods.fml.common.eventhandler.EventPriority; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.TickEvent; +import cpw.mods.fml.common.gameevent.TickEvent.Phase; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import gtPlusPlus.api.objects.Logger; @@ -12,13 +14,11 @@ import gtPlusPlus.core.creative.AddToCreativeTab; import gtPlusPlus.core.item.ModItems; import gtPlusPlus.core.item.base.CoreItem; import gtPlusPlus.core.lib.CORE; -import net.minecraft.client.Minecraft; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.*; import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntityBeacon; -import net.minecraft.util.EnumChatFormatting; import net.minecraft.world.World; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.entity.living.LivingDeathEvent; @@ -26,8 +26,8 @@ import net.minecraftforge.event.entity.living.LivingDeathEvent; public class ItemMagicFeather extends CoreItem { public static final String NAME = "magicfeather"; - private static final WeakHashMap<UUID, MagicFeatherData> sPlayerData = new WeakHashMap<UUID, MagicFeatherData>(); - private static final WeakHashMap<UUID, HashSet<TileEntityBeacon>> sBeaconData = new WeakHashMap<UUID, HashSet<TileEntityBeacon>>(); + private static final WeakHashMap<EntityPlayer, MagicFeatherData> sPlayerData = new WeakHashMap<>(); + private static final WeakHashMap<EntityPlayer, HashSet<TileEntityBeacon>> sBeaconData = new WeakHashMap<>(); public ItemMagicFeather() { super("magicfeather", AddToCreativeTab.tabMisc, 1, 100, new String[]{"Lets you fly around Beacons"}, EnumRarity.uncommon, null, false, null); @@ -41,36 +41,14 @@ public class ItemMagicFeather extends CoreItem { return Integer.MAX_VALUE; } + @SuppressWarnings("unchecked") @Override @SideOnly(Side.CLIENT) public void addInformation(final ItemStack stack, final EntityPlayer aPlayer, final List list, final boolean bool) { list.add("Does not need to be the item held in your hand to work"); super.addInformation(stack, aPlayer, list, bool); - EntityPlayer player = Minecraft.getMinecraft().thePlayer; - list.add(""); - if (player != null) { - if (!isInBeaconRange(player)) { - list.add("" + EnumChatFormatting.RED + "Needs to be within beacon range"); - } - else { - list.add("" + EnumChatFormatting.GOLD + "Fly like an eagle"); - } - HashSet<TileEntityBeacon> aBeaconData = sBeaconData.get(player.getUniqueID()); - if (aBeaconData != null && !aBeaconData.isEmpty()) { - list.add("Nearby Beacons:"); - for (TileEntityBeacon aBeacon : aBeaconData) { - int level = aBeacon.getLevels(); - if (level > 0) { - int radius = (level * 10 + 10); - int x = aBeacon.xCoord; - int z = aBeacon.zCoord; - int y = aBeacon.yCoord; - list.add("Level: "+level+", Radius: "+radius); - list.add("Location: "+x+", "+y+", "+z); - } - } - } - } + list.add("Needs to be within beacon range"); + list.add("Range is beacon level * 10 + 10"); } public boolean hasCustomEntity(ItemStack stack) { @@ -80,18 +58,23 @@ public class ItemMagicFeather extends CoreItem { private static boolean isInBeaconRange(EntityPlayer player) { World world = player.getEntityWorld(); if (world.isRemote) { - HashSet<TileEntityBeacon> aBeaconData = sBeaconData.get(player.getUniqueID()); - if (aBeaconData != null) { - return !aBeaconData.isEmpty(); - } return false; } - List<TileEntity> tileEntities = world.loadedTileEntityList; - HashSet<TileEntityBeacon> aBeaconData = sBeaconData.get(player.getUniqueID()); - if (aBeaconData == null) { - aBeaconData = new HashSet<TileEntityBeacon>(); - sBeaconData.put(player.getUniqueID(), aBeaconData); + HashSet<TileEntityBeacon> aBeaconData = sBeaconData.computeIfAbsent(player, k -> new HashSet<>()); + int chunkXlo = (int) (player.posX - 50) >> 4, + chunkXhi = (int) (player.posX + 50) >> 4, + chunkZlo = (int) (player.posZ - 50) >> 4, + chunkZhi = (int) (player.posZ + 50) >> 4; + for (int chunkX = chunkXlo; chunkX < chunkXhi; chunkX++) { + for (int chunkZ = chunkZlo; chunkZ < chunkZhi; chunkZ++) { + if (!world.getChunkProvider().chunkExists(chunkX, chunkZ)) continue; + findSuitableBeacon(player, world.getChunkFromChunkCoords(chunkX, chunkZ).chunkTileEntityMap.values(), aBeaconData); + } } + return aBeaconData.size() > 0; + } + + private static void findSuitableBeacon(EntityPlayer player, Collection<TileEntity> tileEntities, HashSet<TileEntityBeacon> aBeaconData) { for (TileEntity t : tileEntities) { if (!(t instanceof TileEntityBeacon)) { continue; @@ -110,15 +93,8 @@ public class ItemMagicFeather extends CoreItem { if (player.posZ < (z - radius) || player.posZ > (z + radius)) { continue; } - if (!aBeaconData.contains(beacon)) { - aBeaconData.add(beacon); - } - } - if (aBeaconData.size() > 0) { - return true; + aBeaconData.add(beacon); } - - return false; } private static void setMayFly(EntityPlayer player, boolean mayFly) { @@ -142,60 +118,48 @@ public class ItemMagicFeather extends CoreItem { @SubscribeEvent(priority=EventPriority.HIGHEST) public void onPlayerTick(TickEvent.PlayerTickEvent event) { - if (event.side != Side.SERVER) { + if (event.side != Side.SERVER || event.phase != Phase.END) { return; } EntityPlayer player = event.player; - HashSet<TileEntityBeacon> aBeaconData = sBeaconData.get(player.getUniqueID()); + HashSet<TileEntityBeacon> aBeaconData = sBeaconData.get(player); if (aBeaconData != null && !aBeaconData.isEmpty()) { - HashSet<TileEntityBeacon> aRemovals = new HashSet<TileEntityBeacon>(); - for (TileEntityBeacon aBeacon : aBeaconData) { + for (Iterator<TileEntityBeacon> iterator = aBeaconData.iterator(); iterator.hasNext(); ) { + TileEntityBeacon aBeacon = iterator.next(); int level = aBeacon.getLevels(); if (level == 0) { - aRemovals.add(aBeacon); + iterator.remove(); continue; } int radius = (level * 10 + 10); int x = aBeacon.xCoord; int z = aBeacon.zCoord; - if (player.posX < (x - radius) || player.posX > (x + radius)) { - aRemovals.add(aBeacon); - continue; - } - if (player.posZ < (z - radius) || player.posZ > (z + radius)) { - aRemovals.add(aBeacon); - continue; + if (player.posX < (x - radius) || player.posX > (x + radius) || + player.posZ < (z - radius) || player.posZ > (z + radius)) { + iterator.remove(); } } - aBeaconData.removeAll(aRemovals); } boolean hasItem = hasItem(player, ModItems.itemMagicFeather); if (!hasItem) { - ItemMagicFeather.sPlayerData.remove(player.getUniqueID()); + ItemMagicFeather.sPlayerData.remove(player); } - MagicFeatherData data = ItemMagicFeather.sPlayerData.get(player.getUniqueID()); + MagicFeatherData data = ItemMagicFeather.sPlayerData.get(player); if (data == null) { data = new MagicFeatherData(player); - ItemMagicFeather.sPlayerData.put(player.getUniqueID(), data); + ItemMagicFeather.sPlayerData.put(player, data); } data.onTick(); } @SubscribeEvent(priority=EventPriority.LOWEST) - public void onPlayerDeath(LivingDeathEvent event) { - if (event.entityLiving != null) { - EntityLivingBase aEntity = event.entityLiving; - if (aEntity instanceof EntityPlayer && aEntity.worldObj != null) { - if (aEntity.worldObj.isRemote) { - return; - } - else { - EntityPlayer aPlayer = (EntityPlayer) aEntity; - ItemMagicFeather.sPlayerData.remove(aPlayer.getUniqueID()); - ItemMagicFeather.sBeaconData.remove(aPlayer.getUniqueID()); - } - } - } + public void onPlayerDeath(LivingDeathEvent event) { + if (event.entityLiving == null) return; + EntityLivingBase aEntity = event.entityLiving; + if (!(aEntity instanceof EntityPlayer) || aEntity.worldObj == null || aEntity.worldObj.isRemote) return; + EntityPlayer aPlayer = (EntityPlayer) aEntity; + ItemMagicFeather.sPlayerData.remove(aPlayer); + ItemMagicFeather.sBeaconData.remove(aPlayer); } private static boolean hasItem(EntityPlayer player, Item item) { @@ -210,17 +174,19 @@ public class ItemMagicFeather extends CoreItem { private static class MagicFeatherData { - private final EntityPlayer player; + private final WeakReference<EntityPlayer> player; private boolean hasItem = false; private int checkTick = 0; private boolean beaconInRangeCache; public MagicFeatherData(EntityPlayer player) { - this.player = player; + this.player = new WeakReference<>(player); this.beaconInRangeCache = player.capabilities.allowFlying; } public void onTick() { + EntityPlayer player = this.player.get(); + if (player == null) return; try { boolean hasItem = hasItem(player, ModItems.itemMagicFeather); if (hasItem != this.hasItem) { @@ -239,22 +205,26 @@ public class ItemMagicFeather extends CoreItem { t.printStackTrace(); } - boolean mayFly = player.capabilities.isCreativeMode || (hasItem && checkBeaconInRange(player)); - setMayFly(player, mayFly); + if (hasItem) { + // only modify if hasItem. Override other flight methods since you are literally holding this item in + // your own inventory. You have sent your consent. + boolean mayFly = player.capabilities.isCreativeMode || checkBeaconInRange(player); + setMayFly(player, mayFly); + } } private void onAdd() { - if (!ItemMagicFeather.isInBeaconRange(player)) { + if (!ItemMagicFeather.isInBeaconRange(getPlayer())) { return; } - setMayFly(player, true); + setMayFly(getPlayer(), true); } private void onRemove() { - if (player.capabilities.isCreativeMode) { + if (getPlayer().capabilities.isCreativeMode) { return; } - setMayFly(player, false); + setMayFly(getPlayer(), false); } private boolean checkBeaconInRange(EntityPlayer player) { @@ -264,6 +234,10 @@ public class ItemMagicFeather extends CoreItem { beaconInRangeCache = ItemMagicFeather.isInBeaconRange(player); return beaconInRangeCache; } + + private EntityPlayer getPlayer() { + return player.get(); + } } } |