From 39af314d5779b579f91ae8dadde9fa042e7e2dec Mon Sep 17 00:00:00 2001 From: Jaiden Baker Date: Sat, 23 Mar 2024 06:02:36 +1300 Subject: [PATCH] Fixes for wireless chargers (#852) * Fix wireless chargers * Add getOnlinePlayers PlayerUtils function * Wireless Charger MP fix * Optimize mapping check --- .../core/util/minecraft/PlayerUtils.java | 17 +- .../common/helpers/ChargingHelper.java | 147 +++++++++--------- .../basic/GregtechMetaWirelessCharger.java | 22 +-- 3 files changed, 85 insertions(+), 101 deletions(-) diff --git a/src/main/java/gtPlusPlus/core/util/minecraft/PlayerUtils.java b/src/main/java/gtPlusPlus/core/util/minecraft/PlayerUtils.java index 10ebc11e1b..c84f14d593 100644 --- a/src/main/java/gtPlusPlus/core/util/minecraft/PlayerUtils.java +++ b/src/main/java/gtPlusPlus/core/util/minecraft/PlayerUtils.java @@ -1,6 +1,5 @@ package gtPlusPlus.core.util.minecraft; -import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.UUID; @@ -36,6 +35,12 @@ public class PlayerUtils { } } + public static List getOnlinePlayers() { + final List onlinePlayers = MinecraftServer.getServer() + .getConfigurationManager().playerEntityList; + return onlinePlayers; + } + public static void messagePlayer(final EntityPlayer P, final String S) { gregtech.api.util.GT_Utility.sendChatToPlayer(P, S); } @@ -46,12 +51,7 @@ public static void messagePlayer(final EntityPlayer P, final IChatComponent S) { public static EntityPlayer getPlayer(final String name) { try { - final List i = new ArrayList<>(); - for (EntityPlayerMP playerMP : (Iterable) MinecraftServer.getServer() - .getConfigurationManager().playerEntityList) { - i.add(playerMP); - } - for (final EntityPlayer temp : i) { + for (final EntityPlayer temp : getOnlinePlayers()) { if (temp.getDisplayName().toLowerCase().equals(name.toLowerCase())) { return temp; } @@ -64,8 +64,7 @@ public static EntityPlayer getPlayerOnServerFromUUID(final UUID parUUID) { if (parUUID == null) { return null; } - final List allPlayers = MinecraftServer.getServer().getConfigurationManager().playerEntityList; - for (final EntityPlayerMP player : allPlayers) { + for (final EntityPlayerMP player : getOnlinePlayers()) { if (player.getUniqueID().equals(parUUID)) { return player; } diff --git a/src/main/java/gtPlusPlus/xmod/gregtech/common/helpers/ChargingHelper.java b/src/main/java/gtPlusPlus/xmod/gregtech/common/helpers/ChargingHelper.java index a2114ab375..0551f66aa3 100644 --- a/src/main/java/gtPlusPlus/xmod/gregtech/common/helpers/ChargingHelper.java +++ b/src/main/java/gtPlusPlus/xmod/gregtech/common/helpers/ChargingHelper.java @@ -14,12 +14,12 @@ import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; -import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent; import baubles.api.BaublesApi; import cofh.api.energy.IEnergyContainerItem; import cpw.mods.fml.common.eventhandler.EventPriority; import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.gameevent.TickEvent.ServerTickEvent; import gregtech.api.enums.GT_Values; import gregtech.api.util.GT_ModHandler; import gregtech.common.items.GT_MetaGenerated_Item_01; @@ -31,6 +31,7 @@ import gtPlusPlus.core.util.Utils; import gtPlusPlus.core.util.math.MathUtils; import gtPlusPlus.core.util.minecraft.NBTUtils; +import gtPlusPlus.core.util.minecraft.PlayerUtils; import gtPlusPlus.xmod.gregtech.common.tileentities.machines.basic.GregtechMetaWirelessCharger; import ic2.api.item.ElectricItem; import ic2.api.item.IElectricItem; @@ -42,81 +43,74 @@ public class ChargingHelper { private int mTickTimer = 0; private static final int mTickMultiplier = 20; - // Called whenever the player is updated or ticked. @SubscribeEvent(priority = EventPriority.HIGHEST) + public void onServerTick(ServerTickEvent event) { + if (++mTickTimer % mTickMultiplier == 0) { + if (Utils.isServer()) { + for (EntityPlayer mPlayerMan : PlayerUtils.getOnlinePlayers()) { + doPlayerChargeTick(mPlayerMan); + } + } + } + } + @SuppressWarnings("unused") - public void onPlayerTick(LivingUpdateEvent event) { + private void doPlayerChargeTick(EntityPlayer mPlayerMan) { try { - if (event.entity != null && event.entityLiving != null) { - if (event.entityLiving instanceof EntityPlayer mPlayerMan) { - if (Utils.isServer()) { - mTickTimer++; - if (mTickTimer % mTickMultiplier == 0) { - - long mVoltage; - long mEuStored; - - if (!mChargerMap.isEmpty() && mValidPlayers.containsKey(mPlayerMan.getDisplayName())) { - InventoryPlayer mPlayerInventory = mPlayerMan.inventory; - ItemStack[] mArmourContents = mPlayerInventory.armorInventory.clone(); - ItemStack[] mInventoryContents = mPlayerInventory.mainInventory.clone(); - ItemStack[] baubleSlots = null; - if (Baubles.isModLoaded()) { - IInventory baubleInv = BaublesApi.getBaubles(mPlayerMan); - if (baubleInv != null) { - baubleSlots = new ItemStack[baubleInv.getSizeInventory()]; - for (int i = 0; i < baubleInv.getSizeInventory(); i++) { - baubleSlots[i] = baubleInv.getStackInSlot(i); - } - } + long mVoltage; + long mEuStored; + + if (!mChargerMap.isEmpty() && mValidPlayers.containsKey(mPlayerMan.getDisplayName())) { + InventoryPlayer mPlayerInventory = mPlayerMan.inventory; + ItemStack[] mArmourContents = mPlayerInventory.armorInventory.clone(); + ItemStack[] mInventoryContents = mPlayerInventory.mainInventory.clone(); + ItemStack[] baubleSlots = null; + if (Baubles.isModLoaded()) { + IInventory baubleInv = BaublesApi.getBaubles(mPlayerMan); + if (baubleInv != null) { + baubleSlots = new ItemStack[baubleInv.getSizeInventory()]; + for (int i = 0; i < baubleInv.getSizeInventory(); i++) { + baubleSlots[i] = baubleInv.getStackInSlot(i); + } + } + } + + for (GregtechMetaWirelessCharger mEntityTemp : mChargerMap.values()) { + if (mEntityTemp != null) { + if (mEntityTemp.getBaseMetaTileEntity() == null + || !mEntityTemp.getBaseMetaTileEntity().isAllowedToWork()) + continue; + if (mPlayerMan.getEntityWorld().provider.dimensionId == mEntityTemp.getDimensionID()) { + mVoltage = mEntityTemp.maxEUInput(); + mEuStored = mEntityTemp.getEUVar(); + if (mVoltage > 0 && mEuStored >= mVoltage) { + Map LR = mEntityTemp.getLongRangeMap(); + Map LO = mEntityTemp.getLocalMap(); + + long mStartingEu = mEntityTemp.getEUVar(); + if (canCharge(mEntityTemp, mPlayerMan, LR, LO)) { + chargeItems(mEntityTemp, mArmourContents); + chargeItems(mEntityTemp, mInventoryContents); + chargeItems(mEntityTemp, baubleSlots); } - for (GregtechMetaWirelessCharger mEntityTemp : mChargerMap.values()) { - if (mEntityTemp != null) { - if (mEntityTemp.getBaseMetaTileEntity() == null - || !mEntityTemp.getBaseMetaTileEntity().isAllowedToWork()) - continue; - if (mPlayerMan.getEntityWorld().provider.dimensionId - == mEntityTemp.getDimensionID()) { - mVoltage = mEntityTemp.maxEUInput(); - mEuStored = mEntityTemp.getEUVar(); - if (mVoltage > 0 && mEuStored >= mVoltage) { - - Map LR = mEntityTemp.getLongRangeMap(); - Map LO = mEntityTemp.getLocalMap(); - - long mStartingEu = mEntityTemp.getEUVar(); - if (canCharge(mEntityTemp, mPlayerMan, LR, LO)) { - chargeItems(mEntityTemp, mArmourContents); - chargeItems(mEntityTemp, mInventoryContents); - chargeItems(mEntityTemp, baubleSlots); - } - - if (mStartingEu - mEntityTemp.getEUVar() <= 0) { - long mMaxDistance; - if (mEntityTemp.getMode() == 0) { - mMaxDistance = (4 * GT_Values.V[mEntityTemp.getTier()]); - } else if (mEntityTemp.getMode() == 1) { - mMaxDistance = (mEntityTemp.getTier() * 10L); - } else { - mMaxDistance = (4 * GT_Values.V[mEntityTemp.getTier()] / 2); - } - double mDistance = calculateDistance(mEntityTemp, mPlayerMan); - long mVoltageCost = MathUtils - .findPercentageOfInt(mMaxDistance, (float) mDistance); - - if (mVoltageCost > 0) { - if (mVoltageCost > mEntityTemp.maxEUInput()) { - mEntityTemp.setEUVar( - (mEntityTemp.getEUVar() - - mEntityTemp.maxEUInput())); - } else { - mEntityTemp - .setEUVar((mEntityTemp.getEUVar() - mVoltageCost)); - } - } - } - } + if (mStartingEu - mEntityTemp.getEUVar() <= 0) { + long mMaxDistance; + if (mEntityTemp.getMode() == 0) { + mMaxDistance = (4 * GT_Values.V[mEntityTemp.getTier()]); + } else if (mEntityTemp.getMode() == 1) { + mMaxDistance = (mEntityTemp.getTier() * 10L); + } else { + mMaxDistance = (4 * GT_Values.V[mEntityTemp.getTier()] / 2); + } + double mDistance = calculateDistance(mEntityTemp, mPlayerMan); + long mVoltageCost = MathUtils.findPercentageOfInt(mMaxDistance, (float) mDistance); + + if (mVoltageCost > 0) { + if (mVoltageCost > mEntityTemp.maxEUInput()) { + mEntityTemp.setEUVar((mEntityTemp.getEUVar() - mEntityTemp.maxEUInput())); + } else { + mEntityTemp.setEUVar((mEntityTemp.getEUVar() - mVoltageCost)); } } } @@ -137,15 +131,16 @@ public void onPlayerTick(LivingUpdateEvent event) { } } + public static GregtechMetaWirelessCharger getEntry(BlockPos mPos) { + return mChargerMap.get(mPos); + } + public static boolean addEntry(BlockPos mPos, GregtechMetaWirelessCharger mEntity) { if (mEntity == null) { return false; } - if (!mChargerMap.containsKey(mPos)) { - return mChargerMap.put(mPos, mEntity) == null; - } else { - return true; - } + mChargerMap.put(mPos, mEntity); + return true; } public static boolean removeEntry(BlockPos mPos, GregtechMetaWirelessCharger mEntity) { diff --git a/src/main/java/gtPlusPlus/xmod/gregtech/common/tileentities/machines/basic/GregtechMetaWirelessCharger.java b/src/main/java/gtPlusPlus/xmod/gregtech/common/tileentities/machines/basic/GregtechMetaWirelessCharger.java index d1d937fc82..bd0cf2e098 100644 --- a/src/main/java/gtPlusPlus/xmod/gregtech/common/tileentities/machines/basic/GregtechMetaWirelessCharger.java +++ b/src/main/java/gtPlusPlus/xmod/gregtech/common/tileentities/machines/basic/GregtechMetaWirelessCharger.java @@ -28,7 +28,6 @@ public class GregtechMetaWirelessCharger extends GregtechMetaTileEntity { - private boolean mHasBeenMapped = false; private int mCurrentDimension = 0; public int mMode = 0; public boolean mLocked = true; @@ -471,12 +470,13 @@ public void onPostTick(final IGregTechTileEntity aBaseMetaTileEntity, final long this.mCurrentDimension = aBaseMetaTileEntity.getWorld().provider.dimensionId; } - if (!mHasBeenMapped && ChargingHelper.addEntry(getTileEntityPosition(), this)) { - mHasBeenMapped = true; - } + if (aTick % 20 == 0) { + boolean mHasBeenMapped = this.equals(ChargingHelper.getEntry(getTileEntityPosition())); + if (!mHasBeenMapped) { + mHasBeenMapped = ChargingHelper.addEntry(getTileEntityPosition(), this); + } - if (aTick % 20 == 0 && mHasBeenMapped) { - if (!aBaseMetaTileEntity.getWorld().playerEntities.isEmpty()) { + if (mHasBeenMapped && !aBaseMetaTileEntity.getWorld().playerEntities.isEmpty()) { for (Object mTempPlayer : aBaseMetaTileEntity.getWorld().playerEntities) { if (mTempPlayer instanceof EntityPlayer || mTempPlayer instanceof EntityPlayerMP) { EntityPlayer mTemp = (EntityPlayer) mTempPlayer; @@ -655,14 +655,4 @@ public void doExplosion(long aExplosionPower) { ChargingHelper.removeEntry(getTileEntityPosition(), this); super.doExplosion(aExplosionPower); } - - @Override - public void onPreTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) { - if (aBaseMetaTileEntity.isServerSide()) { - if (!mHasBeenMapped && ChargingHelper.addEntry(getTileEntityPosition(), this)) { - mHasBeenMapped = true; - } - } - super.onPreTick(aBaseMetaTileEntity, aTick); - } }