Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit Jar network to pairs only #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 84 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,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
Expand All @@ -64,43 +83,55 @@ 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);
}
int networkSize = this.jars.size();

// Too many jars...
if (networkSize > 2) {
jars.subList(2, jars.size()).clear();
}

// Just enough jars...
if (networkSize == 2 && hasProcessedJars()) {
this.lastTime = time + 3;
}
}
}

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 All @@ -120,6 +151,24 @@ private static JarNetwork getNetwork(UUID id) {
return network;
}

public void disconnectJar(TileRemoteJar jar) {
UUID id = jar.networkId;

if (id != null) {
JarNetwork network = TileRemoteJar.networks.get(id);

if (network != null) {
if (network.jars.size() < 2) {
// Network consists of only this Jar. Discarding Network instead
networks.remove(id);
}

// Discarding Jar from Network
network.jars.remove(jar);
}
}
}

public void markForUpdate() {
this.markDirty();
this.getWorldObj().markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
Expand Down
36 changes: 13 additions & 23 deletions src/main/java/makeo/gadomancy/common/items/ItemBlockRemoteJar.java
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,33 +84,21 @@ 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 (!world.isRemote) {
if (stack.stackSize == 1 && tile != null) {
if (!world.isRemote && !player.isSneaking()) {
NBTTagCompound compound = NBTHelper.getData(stack);
if (!player.isSneaking()) {
UUID networkId = null;
if (tile.networkId == null) {
player.addChatComponentMessage(new ChatComponentTranslation("gadomancy.info.RemoteJar.new"));
networkId = UUID.randomUUID();
tile.networkId = networkId;
tile.markForUpdate();
} else {
UUID current = NBTHelper.getUUID(compound, "networkId");
if (current == null || !current.equals(tile.networkId)) {
player.addChatComponentMessage(
new ChatComponentTranslation("gadomancy.info.RemoteJar.connected"));
networkId = tile.networkId;
}
}
UUID networkId = UUID.randomUUID();

player.addChatComponentMessage(new ChatComponentTranslation("gadomancy.info.RemoteJar.new"));
NBTHelper.setUUID(compound, "networkId", networkId);

tile.disconnectJar(tile);
tile.networkId = networkId;
tile.markForUpdate();

if (networkId != null) {
NBTHelper.setUUID(compound, "networkId", networkId);
}
}
return true;
} else {
return player.isSneaking();
}
return player.isSneaking();
}
return false;
}
Expand All @@ -129,6 +118,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