Skip to content

Commit

Permalink
Improved AE dismantling
Browse files Browse the repository at this point in the history
  • Loading branch information
RecursivePineapple committed Sep 18, 2024
1 parent 383ab74 commit 5e45223
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,11 @@ private static void setState(ItemStack itemStack, NBTState state) {
NBTTagCompound newState = state.save();

if (!Objects.equals(newState, itemStack.getTagCompound())) {
// spotless:off
boolean needsSyncToServer =
NetworkUtils.isClient() &&
(
itemStack.getTagCompound() == null ||
!Objects.equals(newState.getTag("config"), itemStack.getTagCompound().getTag("config"))
);
// spotless:on
boolean configChanged = itemStack.getTagCompound() == null || !Objects.equals(
newState.getTag("config"),
itemStack.getTagCompound()
.getTag("config"));
boolean needsSyncToServer = NetworkUtils.isClient() && configChanged;

itemStack.setTagCompound(newState);

Expand Down Expand Up @@ -545,7 +542,7 @@ public void onUsingTick(ItemStack stack, EntityPlayer player, int count) {
}
}

if (ticksUsed >= 20 && (ticksUsed % 10) == 0 && !player.worldObj.isRemote) {
if (ticksUsed >= 20 && (ticksUsed % 2) == 0 && !player.worldObj.isRemote) {
PENDING_BUILDS.get(player)
.tryPlaceBlocks(stack, player);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,9 @@ public static Vector3i getLookingAtLocation(EntityPlayer player) {
}
} else {
target = new Vector3i(
(int) modifiedPosVec.xCoord,
(int) modifiedPosVec.yCoord,
(int) modifiedPosVec.zCoord - 1);
(int) (modifiedPosVec.xCoord - 1),
(int) (modifiedPosVec.yCoord),
(int) (modifiedPosVec.zCoord));
}

return target;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.server.MinecraftServer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChatComponentText;
Expand All @@ -35,9 +36,15 @@
import net.minecraftforge.fluids.IFluidHandler;

import appeng.api.config.Actionable;
import appeng.api.implementations.tiles.ISegmentedInventory;
import appeng.api.networking.security.PlayerSource;
import appeng.api.parts.IPart;
import appeng.api.parts.IPartHost;
import appeng.api.parts.PartItemStack;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IAEItemStack;
import appeng.helpers.ICustomNameObject;
import appeng.parts.AEBasePart;
import appeng.util.item.AEFluidStack;
import appeng.util.item.AEItemStack;
import cpw.mods.fml.relauncher.ReflectionHelper;
Expand Down Expand Up @@ -65,7 +72,7 @@ public class PendingBuild {

private static final int BLOCKS_PER_PLACE = 256;
private static final int MAX_PLACE_DISTANCE = 512 * 512;
private static final double EU_PER_BLOCK = 128.0, TE_PENALTY = 16.0;
private static final double EU_PER_BLOCK = 128.0, TE_PENALTY = 16.0, EU_DISTANCE_EXP = 1.25;

public void tryPlaceBlocks(ItemStack stack, EntityPlayer player) {
if (pendingBlocks == null) {
Expand Down Expand Up @@ -590,17 +597,18 @@ public boolean tryConsumePower(ItemStack stack, double x, double y, double z, do
return true;
}

euUsage *= placingPlayer.getDistanceSq(x, y, z);
euUsage *= Math.pow(placingPlayer.getDistance(x, y, z), EU_DISTANCE_EXP);

return ElectricItem.manager.use(stack, euUsage, placingPlayer);
}

private void removeBlock(World world, int x, int y, int z, Block existing, int existingMeta) {
TileEntity te = world.getTileEntity(x, y, z);

emptyInventory(te);
emptyTileInventory(te);
emptyTank(te);
removeCovers(te);
resetAEMachine(te);
resetKeptSettings(te);

if (existing instanceof IFluidBlock fluidBlock && fluidBlock.canDrain(world, x, y, z)) {
Expand All @@ -617,22 +625,26 @@ private void removeBlock(World world, int x, int y, int z, Block existing, int e
world.setBlock(x, y, z, Blocks.air);
}

private void emptyInventory(TileEntity te) {
private void emptyTileInventory(TileEntity te) {
if (te instanceof IInventory inv) {
int size = inv.getSizeInventory();
emptyInventory(inv);
}
}

for (int i = 0; i < size; i++) {
ItemStack stack = inv.getStackInSlot(i);
private void emptyInventory(IInventory inv) {
int size = inv.getSizeInventory();

if (stack != null && stack.getItem() != null) {
inv.setInventorySlotContents(i, null);
for (int i = 0; i < size; i++) {
ItemStack stack = inv.getStackInSlot(i);

givePlayerItems(stack);
}
}
if (stack != null && stack.getItem() != null) {
inv.setInventorySlotContents(i, null);

inv.markDirty();
givePlayerItems(stack);
}
}

inv.markDirty();
}

private void emptyTank(TileEntity te) {
Expand Down Expand Up @@ -660,6 +672,67 @@ private void removeCovers(TileEntity te) {
}
}

private void resetAEMachine(Object machine) {
if (machine instanceof ISegmentedInventory segmentedInventory) {
IInventory upgrades = segmentedInventory.getInventoryByName("upgrades");

if (upgrades != null) {
emptyInventory(upgrades);
}

IInventory cells = segmentedInventory.getInventoryByName("cells");

if (cells != null) {
emptyInventory(cells);
}
}

if (machine instanceof ICustomNameObject customName) {
if (customName.hasCustomName()) {
try {
customName.setCustomName(null);
} catch (IllegalArgumentException e) {
// hack because AEBasePart's default setCustomName impl throws an IAE when the name is null
if (machine instanceof AEBasePart basePart) {
NBTTagCompound tag = basePart.getItemStack()
.getTagCompound();

if (tag != null) {
tag.removeTag("display");

if (tag.hasNoTags()) {
basePart.getItemStack()
.setTagCompound(null);
}
}
}
}
}
}

if (machine instanceof IPartHost host) {
// intentionally includes UNKNOWN to remove any cables
for (ForgeDirection dir : ForgeDirection.values()) {
IPart part = host.getPart(dir);

if (part != null) {
resetAEMachine(part);

host.removePart(dir, false);

givePlayerItems(part.getItemStack(PartItemStack.Break));

ArrayList<ItemStack> drops = new ArrayList<>();
part.getDrops(drops, false);

if (!drops.isEmpty()) {
givePlayerItems(drops.toArray(new ItemStack[drops.size()]));
}
}
}
}
}

private void resetKeptSettings(TileEntity te) {
if (te instanceof IRedstoneEmitter emitter) {
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) {
Expand Down

0 comments on commit 5e45223

Please sign in to comment.