diff --git a/src/main/java/com/glodblock/github/common/item/ItemWirelessUltraTerminal.java b/src/main/java/com/glodblock/github/common/item/ItemWirelessUltraTerminal.java index 828a8b5b..c6001b01 100644 --- a/src/main/java/com/glodblock/github/common/item/ItemWirelessUltraTerminal.java +++ b/src/main/java/com/glodblock/github/common/item/ItemWirelessUltraTerminal.java @@ -274,9 +274,9 @@ public class ItemWirelessUltraTerminal extends ItemBaseWirelessTerminal @Override public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int slot, boolean p_77663_5_) { - if (Platform.isServer() && entityIn instanceof EntityPlayer player) { + if (entityIn instanceof EntityPlayer player) { if (WirelessMagnet.getMode(stack) != WirelessMagnet.Mode.Off) { - WirelessMagnet.doMagnet(stack, player.worldObj, player); + WirelessMagnet.doMagnet(stack, player); } } } diff --git a/src/main/java/com/glodblock/github/inventory/item/BaseWirelessInventory.java b/src/main/java/com/glodblock/github/inventory/item/BaseWirelessInventory.java index 55b16174..207313e2 100644 --- a/src/main/java/com/glodblock/github/inventory/item/BaseWirelessInventory.java +++ b/src/main/java/com/glodblock/github/inventory/item/BaseWirelessInventory.java @@ -71,7 +71,7 @@ public abstract class BaseWirelessInventory extends MEMonitorHandler implements private void readFromNBT() { NBTTagCompound data = Platform.openNbtData(this.target); - this.setMagnetCardMode(WirelessMagnet.Mode.values()[data.getInteger(modeKey)]); + this.setMagnetCardMode(WirelessMagnet.Mode.getModes()[data.getInteger(modeKey)]); this.setRestock(data.getBoolean(ItemBaseWirelessTerminal.restockItems)); } @@ -137,9 +137,8 @@ public abstract class BaseWirelessInventory extends MEMonitorHandler implements } public void setMagnetCardNextMode() { - setMagnetCardMode( - WirelessMagnet.Mode.values()[(getMagnetCardMode().ordinal() + 1) - % WirelessMagnet.Mode.values().length]); + final WirelessMagnet.Mode[] MODES = WirelessMagnet.Mode.getModes(); + setMagnetCardMode(MODES[(getMagnetCardMode().ordinal() + 1) % MODES.length]); } public void setRestock(boolean val) { diff --git a/src/main/java/com/glodblock/github/inventory/item/WirelessMagnet.java b/src/main/java/com/glodblock/github/inventory/item/WirelessMagnet.java index 08ac0b09..bf35550e 100644 --- a/src/main/java/com/glodblock/github/inventory/item/WirelessMagnet.java +++ b/src/main/java/com/glodblock/github/inventory/item/WirelessMagnet.java @@ -2,7 +2,6 @@ package com.glodblock.github.inventory.item; import static com.glodblock.github.common.Config.magnetRange; -import java.util.Iterator; import java.util.List; import net.minecraft.entity.item.EntityItem; @@ -12,9 +11,12 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.AxisAlignedBB; import net.minecraft.world.World; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.entity.player.PlayerPickupXpEvent; import com.glodblock.github.common.item.ItemWirelessUltraTerminal; +import appeng.entity.EntityFloatingItem; import appeng.util.Platform; public class WirelessMagnet { @@ -24,9 +26,16 @@ public class WirelessMagnet { public static String filterConfigKey = "MagnetConfig"; public enum Mode { + Off, Inv, - ME + ME; + + private static final Mode[] MODES = Mode.values(); + + public static Mode[] getModes() { + return MODES; + } } public enum ListMode { @@ -34,7 +43,7 @@ public class WirelessMagnet { BlackList } - public static List getEntitiesInRange(Class entityType, World world, int x, int y, int z, int distance) { + private static List getEntitiesInRange(Class entityType, World world, int x, int y, int z, int distance) { return world.getEntitiesWithinAABB( entityType, AxisAlignedBB.getBoundingBox( @@ -46,75 +55,83 @@ public class WirelessMagnet { z + distance)); } - @SuppressWarnings("unchecked") - public static boolean isConfigured(ItemStack wirelessTerm) { NBTTagCompound data = Platform.openNbtData(wirelessTerm); return data.hasKey(modeKey); } - public static void doMagnet(ItemStack wirelessTerm, World world, EntityPlayer player) { - if (wirelessTerm == null || player == null || player.isSneaking() || !isConfigured(wirelessTerm)) return; + public static void doMagnet(ItemStack wirelessTerm, EntityPlayer player) { + if (player.ticksExisted % 5 != 0 || player.isSneaking() || !isConfigured(wirelessTerm)) return; - Iterator iterator = getEntitiesInRange( + World world = player.worldObj; + final List items = getEntitiesInRange( EntityItem.class, world, (int) player.posX, (int) player.posY, (int) player.posZ, - magnetRange).iterator(); + magnetRange); + final boolean skipPlayerCheck = world.playerEntities.size() < 2; + boolean playSound = false; - while (iterator.hasNext()) { - EntityItem itemToGet = (EntityItem) iterator.next(); - EntityPlayer closestPlayer = world.getClosestPlayerToEntity(itemToGet, magnetRange); - - if (closestPlayer != null && closestPlayer == player) { - NBTTagCompound itemNBT = new NBTTagCompound(); - itemToGet.writeEntityToNBT(itemNBT); - - if (itemToGet.func_145800_j() == null - || !itemToGet.func_145800_j().equals(player.getCommandSenderName())) - itemToGet.delayBeforeCanPickup = 0; - - if (itemToGet.delayBeforeCanPickup <= 0) { - itemNBT.setBoolean("attractable", true); - itemToGet.readEntityFromNBT(itemNBT); - } - - if (itemNBT.getBoolean("attractable")) { - itemToGet.motionX = itemToGet.motionY = itemToGet.motionZ = 0; - itemToGet.setPosition( - player.posX - 0.2 + (world.rand.nextDouble() * 0.4), - player.posY - 0.6, - player.posZ - 0.2 + (world.rand.nextDouble() * 0.4)); - } + for (EntityItem itemToGet : items) { + if (itemToGet.getEntityItem() == null || itemToGet instanceof EntityFloatingItem) { + continue; } + + if (!skipPlayerCheck) { + EntityPlayer closestPlayer = world.getClosestPlayerToEntity(itemToGet, magnetRange); + if (closestPlayer == null || closestPlayer != player) continue; + } + + if (itemToGet.delayBeforeCanPickup > 0) { + itemToGet.delayBeforeCanPickup = 0; + } + playSound = true; + itemToGet.motionX = 0; + itemToGet.motionY = 0; + itemToGet.motionZ = 0; + itemToGet.setPosition( + player.posX - 0.2 + (world.rand.nextDouble() * 0.4), + player.posY - 0.6, + player.posZ - 0.2 + (world.rand.nextDouble() * 0.4)); } - // xp - iterator = getEntitiesInRange( - EntityXPOrb.class, - world, - (int) player.posX, - (int) player.posY, - (int) player.posZ, - magnetRange).iterator(); - while (iterator.hasNext()) { - EntityXPOrb xpToGet = (EntityXPOrb) iterator.next(); - EntityPlayer closestPlayer = world.getClosestPlayerToEntity(xpToGet, magnetRange); + if (playSound) { + world.playSoundAtEntity( + player, + "random.orb", + 0.1F, + 0.5F * ((world.rand.nextFloat() - world.rand.nextFloat()) * 0.7F + 2F)); + } - if (!xpToGet.isDead && !xpToGet.isInvisible() && closestPlayer != null && closestPlayer == player) { - int xpAmount = xpToGet.xpValue; - xpToGet.xpValue = 0; - player.xpCooldown = 0; - player.addExperience(xpAmount); - xpToGet.setDead(); - xpToGet.setInvisible(true); - world.playSoundAtEntity( - player, - "random.orb", - 0.08F, - 0.5F * ((world.rand.nextFloat() - world.rand.nextFloat()) * 0.7F + 1.8F)); + if (!world.isRemote) { + // xp + final List xpOrbs = getEntitiesInRange( + EntityXPOrb.class, + world, + (int) player.posX, + (int) player.posY, + (int) player.posZ, + magnetRange); + + for (EntityXPOrb xpToGet : xpOrbs) { + if (xpToGet.field_70532_c == 0 && xpToGet.isEntityAlive()) { + if (!skipPlayerCheck) { + EntityPlayer closestPlayer = world.getClosestPlayerToEntity(xpToGet, magnetRange); + if (closestPlayer == null || closestPlayer != player) continue; + } + + if (MinecraftForge.EVENT_BUS.post(new PlayerPickupXpEvent(player, xpToGet))) continue; + world.playSoundAtEntity( + player, + "random.orb", + 0.1F, + 0.5F * ((world.rand.nextFloat() - world.rand.nextFloat()) * 0.7F + 1.8F)); + player.onItemPickup(xpToGet, 1); + player.addExperience(xpToGet.xpValue); + xpToGet.setDead(); + } } } } @@ -123,7 +140,7 @@ public class WirelessMagnet { if (is != null && is.getItem() instanceof ItemWirelessUltraTerminal) { NBTTagCompound data = Platform.openNbtData(is); if (data.hasKey(modeKey)) { - return Mode.values()[data.getInteger(modeKey)]; + return Mode.getModes()[data.getInteger(modeKey)]; } setMode(is, Mode.Off); }