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
This commit is contained in:
DrParadox7 committed 2024-06-19 11:56:20 +02:00
1 parent a55a4980b1
commit fb5bf61efd
2 files changed
+68 -34

No files matched your search

@@ -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,45 @@ 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;
this.registered_to_network = false;
return false;
}
@Override
@@ -64,45 +83,58 @@ public class TileRemoteJar extends TileJarFillable {
}
}
private static Map<UUID, JarNetwork> networks = new HashMap<UUID, JarNetwork>();
private static final Map<UUID, JarNetwork> networks = new HashMap<UUID, JarNetwork>();
private static class JarNetwork {
private long lastTime;
private List<TileJarFillable> jars = new ArrayList<TileJarFillable>();
private final List<TileJarFillable> jars = new ArrayList<TileJarFillable>(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<TileJarFillable>() {
@Override
public int compare(TileJarFillable o1, TileJarFillable o2) {
return o2.amount - o1.amount;
}
});
// Not enough jars...
if (this.jars.size() < 2) return;
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);
}
// Just enough jars...
if (this.jars.size() == 2) {
if (hasProcessedJars()) this.lastTime = time + 3;
return;
}
this.lastTime = time + 3;
// 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);
}
}
return true;
}
private static boolean isValid(TileJarFillable jar) {
return jar != null && jar.getWorldObj() != null
&& !jar.isInvalid()
@@ -23,6 +23,7 @@ import makeo.gadomancy.common.blocks.tiles.TileRemoteJar;
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 class ItemBlockRemoteJar extends ItemBlock {
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 class ItemBlockRemoteJar extends ItemBlock {
if (!world.isRemote) {
tile.networkId = NBTHelper.getUUID(stack.getTagCompound(), "networkId");
tile.aspectFilter = Aspect.getAspect(stack.getTagCompound().getString("AspectFilter"));
tile.markForUpdate();
}
}