Skip to content

Commit

Permalink
Limit Jar network to pairs only
Browse files Browse the repository at this point in the history
Refactor Mirrored Jars to only trade in pairs, massively enhancing performance.
Also fixes Jar labels being lost on placement
  • Loading branch information
DrParadox7 committed Jun 18, 2024
1 parent a55a498 commit 171ff70
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 36 deletions.
101 changes: 66 additions & 35 deletions src/main/java/makeo/gadomancy/common/blocks/tiles/TileRemoteJar.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -64,43 +82,56 @@ public void writeCustomNBT(NBTTagCompound compound) {
}
}

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;
}
});

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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();
}
}
Expand Down

0 comments on commit 171ff70

Please sign in to comment.