From 171ff70626278e226720576f1a1928823bcd74e7 Mon Sep 17 00:00:00 2001 From: DrParadox7 Date: Tue, 18 Jun 2024 16:30:36 +0200 Subject: [PATCH] Limit Jar network to pairs only Refactor Mirrored Jars to only trade in pairs, massively enhancing performance. Also fixes Jar labels being lost on placement --- .../common/blocks/tiles/TileRemoteJar.java | 101 ++++++++++++------ .../common/items/ItemBlockRemoteJar.java | 4 +- 2 files changed, 69 insertions(+), 36 deletions(-) diff --git a/src/main/java/makeo/gadomancy/common/blocks/tiles/TileRemoteJar.java b/src/main/java/makeo/gadomancy/common/blocks/tiles/TileRemoteJar.java index 4dd3272..fc13475 100644 --- a/src/main/java/makeo/gadomancy/common/blocks/tiles/TileRemoteJar.java +++ b/src/main/java/makeo/gadomancy/common/blocks/tiles/TileRemoteJar.java @@ -1,8 +1,6 @@ package makeo.gadomancy.common.blocks.tiles; import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -28,24 +26,44 @@ public class TileRemoteJar extends TileJarFillable { private boolean registered_to_network; + private boolean shouldUpdate() { + return this.count % 3 == 0 && !this.getWorldObj().isRemote + && this.networkId != null + && (!this.registered_to_network || this.amount < this.maxAmount); + } + @Override public void updateEntity() { super.updateEntity(); - if (this.count % 3 == 0 && !this.getWorldObj().isRemote - && this.networkId != null - && (!this.registered_to_network || this.amount < this.maxAmount)) { + + if (shouldUpdate()) { this.count = 0; JarNetwork network = TileRemoteJar.getNetwork(this.networkId); - this.registered_to_network = true; + if (handleNetworkConnections(network)) { + network.update(); + this.registered_to_network = true; + } + } + + this.count++; + } + + private boolean handleNetworkConnections(JarNetwork network) { + int networkCapacity = network.jars.size(); + + // Network requiring jars for operation, registering jar... + if (networkCapacity <= 2) { if (!network.jars.contains(this)) { network.jars.add((TileJarFillable) this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord)); } - network.update(); + return true; } - this.count++; + + this.networkId = null; + return false; } @Override @@ -64,43 +82,56 @@ public void writeCustomNBT(NBTTagCompound compound) { } } - private static Map networks = new HashMap(); + private static final Map networks = new HashMap(); private static class JarNetwork { private long lastTime; - private List jars = new ArrayList(); + private final List jars = new ArrayList(2); private void update() { long time = MinecraftServer.getServer().getEntityWorld().getTotalWorldTime(); if (time > this.lastTime) { - if (this.jars.size() > 1) { - Collections.sort(this.jars, new Comparator() { - - @Override - public int compare(TileJarFillable o1, TileJarFillable o2) { - return o2.amount - o1.amount; - } - }); - - TileJarFillable jar1 = this.jars.get(0); - if (!JarNetwork.isValid(jar1)) { - this.jars.remove(0); - return; - } - - TileJarFillable jar2 = this.jars.get(this.jars.size() - 1); - if (!JarNetwork.isValid(jar2)) { - this.jars.remove(this.jars.size() - 1); - return; - } - - if ((jar2.amount + 1) < jar1.amount && jar2.addToContainer(jar1.aspect, 1) == 0) { - jar1.takeFromContainer(jar1.aspect, 1); - } + + // Not enough jars... + if (this.jars.size() < 2) return; + + // Just enough jars... + if (this.jars.size() == 2) { + if (hasProcessedJars()) this.lastTime = time + 3; + return; + } + + // Too many jars. Refreshing network... + this.jars.clear(); + } + } + + private boolean hasProcessedJars() { + + TileJarFillable jar1 = this.jars.get(0); + if (!JarNetwork.isValid(jars.get(0))) { + this.jars.remove(0); + return false; + } + + TileJarFillable jar2 = this.jars.get(1); + if (!JarNetwork.isValid(jars.get(1))) { + this.jars.remove(1); + return false; + } + + // Transfer Essence if necessary + if (Math.abs(jar1.amount - jar2.amount) > 1) { + + TileJarFillable sourceJar = (jar1.amount > jar2.amount) ? jar1 : jar2; + TileJarFillable destinationJar = (sourceJar == jar1) ? jar2 : jar1; + + if (destinationJar.addToContainer(sourceJar.aspect, 1) == 0) { + sourceJar.takeFromContainer(sourceJar.aspect, 1); } - this.lastTime = time + 3; } + return true; } private static boolean isValid(TileJarFillable jar) { diff --git a/src/main/java/makeo/gadomancy/common/items/ItemBlockRemoteJar.java b/src/main/java/makeo/gadomancy/common/items/ItemBlockRemoteJar.java index 5d449e1..1dcb83e 100644 --- a/src/main/java/makeo/gadomancy/common/items/ItemBlockRemoteJar.java +++ b/src/main/java/makeo/gadomancy/common/items/ItemBlockRemoteJar.java @@ -23,6 +23,7 @@ import makeo.gadomancy.common.registration.RegisteredBlocks; import makeo.gadomancy.common.utils.NBTHelper; import makeo.gadomancy.common.utils.StringHelper; +import thaumcraft.api.aspects.Aspect; import thaumcraft.api.aspects.AspectList; import thaumcraft.common.blocks.ItemJarFilled; import thaumcraft.common.config.ConfigItems; @@ -83,7 +84,7 @@ public boolean onItemUse(ItemStack p_77648_1_, EntityPlayer p_77648_2_, World p_ public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ) { TileRemoteJar tile = BlockRemoteJar.getJarTile(world, x, y, z); - if (tile != null) { + if (stack.stackSize == 1 && tile != null) { if (!world.isRemote) { NBTTagCompound compound = NBTHelper.getData(stack); if (!player.isSneaking()) { @@ -129,6 +130,7 @@ public boolean placeBlockAt(ItemStack stack, EntityPlayer player, World world, i if (!world.isRemote) { tile.networkId = NBTHelper.getUUID(stack.getTagCompound(), "networkId"); + tile.aspectFilter = Aspect.getAspect(stack.getTagCompound().getString("AspectFilter")); tile.markForUpdate(); } }