mirror of
https://github.com/GTNewHorizons/AE2FluidCraft-Rework.git
synced 2026-11-11 15:57:55 +00:00
Magnet improvements (#303)
This commit is contained in:
1 parent
fe3168ed6e
commit
c504dbb28a
3 files changed
+79
-63
No files matched your search
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 <T> List<T> getEntitiesInRange(Class<T> 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<EntityItem> 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<EntityXPOrb> 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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user