mirror of
https://github.com/Creators-of-Create/Create.git
synced 2024-12-15 13:23:41 +01:00
Slightly improve redstone link performance
This commit is contained in:
parent
45465fb69b
commit
48d66c7e55
2 changed files with 32 additions and 24 deletions
|
@ -1,8 +1,9 @@
|
|||
package com.simibubi.create.content.logistics;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
|
@ -20,14 +21,24 @@ import net.minecraft.world.World;
|
|||
|
||||
public class RedstoneLinkNetworkHandler {
|
||||
|
||||
static Map<IWorld, Map<Pair<Frequency, Frequency>, Set<LinkBehaviour>>> connections = new HashMap<>();
|
||||
static final Map<IWorld, Map<Pair<Frequency, Frequency>, Set<LinkBehaviour>>> connections = new IdentityHashMap<>();
|
||||
|
||||
public static class Frequency {
|
||||
public static final Frequency EMPTY = new Frequency(ItemStack.EMPTY);
|
||||
private static final Map<Item, Frequency> simpleFrequencies = new IdentityHashMap<>();
|
||||
private ItemStack stack;
|
||||
private Item item;
|
||||
private int color;
|
||||
|
||||
public Frequency(ItemStack stack) {
|
||||
public static Frequency of(ItemStack stack) {
|
||||
if (stack.isEmpty())
|
||||
return EMPTY;
|
||||
if (!stack.hasTag())
|
||||
return simpleFrequencies.computeIfAbsent(stack.getItem(), $ -> new Frequency(stack));
|
||||
return new Frequency(stack);
|
||||
}
|
||||
|
||||
private Frequency(ItemStack stack) {
|
||||
this.stack = stack;
|
||||
item = stack.getItem();
|
||||
CompoundNBT displayTag = stack.getChildTag("display");
|
||||
|
@ -35,7 +46,7 @@ public class RedstoneLinkNetworkHandler {
|
|||
}
|
||||
|
||||
public ItemStack getStack() {
|
||||
return stack.copy();
|
||||
return stack;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -45,6 +56,8 @@ public class RedstoneLinkNetworkHandler {
|
|||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
return obj instanceof Frequency ? ((Frequency) obj).item == item && ((Frequency) obj).color == color
|
||||
: false;
|
||||
}
|
||||
|
@ -65,7 +78,7 @@ public class RedstoneLinkNetworkHandler {
|
|||
Map<Pair<Frequency, Frequency>, Set<LinkBehaviour>> networksInWorld = networksIn(actor.getWorld());
|
||||
Pair<Frequency, Frequency> key = actor.getNetworkKey();
|
||||
if (!networksInWorld.containsKey(key))
|
||||
networksInWorld.put(key, new HashSet<>());
|
||||
networksInWorld.put(key, new LinkedHashSet<>());
|
||||
return networksInWorld.get(key);
|
||||
}
|
||||
|
||||
|
@ -105,9 +118,9 @@ public class RedstoneLinkNetworkHandler {
|
|||
}
|
||||
if (!withinRange(actor, other))
|
||||
continue;
|
||||
|
||||
if (power < 15)
|
||||
power = Math.max(other.getTransmittedStrength(), power);
|
||||
if (power == 15)
|
||||
break;
|
||||
}
|
||||
|
||||
// fix one-to-one loading order problem
|
||||
|
@ -116,20 +129,15 @@ public class RedstoneLinkNetworkHandler {
|
|||
actor.updateReceiver(power);
|
||||
}
|
||||
|
||||
for (Iterator<LinkBehaviour> iterator = network.iterator(); iterator.hasNext();) {
|
||||
LinkBehaviour other = iterator.next();
|
||||
if (other.tileEntity.isRemoved()) {
|
||||
iterator.remove();
|
||||
continue;
|
||||
}
|
||||
if (!withinRange(actor, other))
|
||||
continue;
|
||||
if (other.isListening())
|
||||
for (LinkBehaviour other : network) {
|
||||
if (other != actor && other.isListening() && withinRange(actor, other))
|
||||
other.updateReceiver(power);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean withinRange(LinkBehaviour from, LinkBehaviour to) {
|
||||
if (from == to)
|
||||
return true;
|
||||
return from.getPos().withinDistance(to.getPos(), AllConfigs.SERVER.logistics.linkRange.get());
|
||||
}
|
||||
|
||||
|
|
|
@ -39,8 +39,8 @@ public class LinkBehaviour extends TileEntityBehaviour {
|
|||
|
||||
protected LinkBehaviour(SmartTileEntity te, Pair<ValueBoxTransform, ValueBoxTransform> slots) {
|
||||
super(te);
|
||||
frequencyFirst = new Frequency(ItemStack.EMPTY);
|
||||
frequencyLast = new Frequency(ItemStack.EMPTY);
|
||||
frequencyFirst = Frequency.EMPTY;
|
||||
frequencyLast = Frequency.EMPTY;
|
||||
firstSlot = slots.getLeft();
|
||||
secondSlot = slots.getRight();
|
||||
textShift = Vec3d.ZERO;
|
||||
|
@ -133,8 +133,8 @@ public class LinkBehaviour extends TileEntityBehaviour {
|
|||
newPosition = positionInTag != positionKey;
|
||||
|
||||
super.read(nbt, clientPacket);
|
||||
frequencyFirst = new Frequency(ItemStack.read(nbt.getCompound("FrequencyFirst")));
|
||||
frequencyLast = new Frequency(ItemStack.read(nbt.getCompound("FrequencyLast")));
|
||||
frequencyFirst = Frequency.of(ItemStack.read(nbt.getCompound("FrequencyFirst")));
|
||||
frequencyLast = Frequency.of(ItemStack.read(nbt.getCompound("FrequencyLast")));
|
||||
}
|
||||
|
||||
public void setFrequency(boolean first, ItemStack stack) {
|
||||
|
@ -148,9 +148,9 @@ public class LinkBehaviour extends TileEntityBehaviour {
|
|||
getHandler().removeFromNetwork(this);
|
||||
|
||||
if (first)
|
||||
frequencyFirst = new Frequency(stack);
|
||||
frequencyFirst = Frequency.of(stack);
|
||||
else
|
||||
frequencyLast = new Frequency(stack);
|
||||
frequencyLast = Frequency.of(stack);
|
||||
|
||||
if (!changed)
|
||||
return;
|
||||
|
|
Loading…
Reference in a new issue