Skip to content

Commit

Permalink
feat: added new load system NOT TESTED
Browse files Browse the repository at this point in the history
  • Loading branch information
Uhutown committed Oct 18, 2023
1 parent 069d9e8 commit 64834a3
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 46 deletions.
22 changes: 17 additions & 5 deletions src/main/java/com/troblecodings/signals/core/LinkedPositions.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ public class LinkedPositions {
private static final String SIGNAL_NAME = "signalName";
private static final String ALL_POS = "allPos";

private final BlockPos thisPos;
private final Map<BlockPos, Signal> signals = new HashMap<>();
private final Map<BlockPos, LinkType> linkedBlocks = new HashMap<>();
private final Map<BlockPos, List<SubsidiaryState>> possibleSubsidiaries = new HashMap<>();

public LinkedPositions(final BlockPos thisPos) {
this.thisPos = thisPos;
}

private final SignalStateListener listener = (stateInfo, properties, changed) -> {
if (changed.equals(ChangedState.UPDATED) || changed.equals(ChangedState.ADDED_TO_CACHE)) {
loadPossibleSubsidiaires(stateInfo, properties);
Expand All @@ -45,6 +50,8 @@ public void addSignal(final BlockPos signalPos, final Signal signal, final World
signals.put(signalPos, signal);
final SignalStateInfo info = new SignalStateInfo(world, signalPos, signal);
SignalConfig.reset(info);
SignalStateHandler.loadSignal(
new StateLoadHolder(info, new LoadHolder<>(new StateInfo(world, thisPos))));
loadPossibleSubsidiaires(info, SignalStateHandler.getStates(info));
}

Expand Down Expand Up @@ -77,11 +84,12 @@ public Map<BlockPos, LinkType> getAllLinkedPos() {
}

public void unlink(final BlockPos tilePos, final World world) {
final List<SignalStateInfo> signalsToUnload = new ArrayList<>();
final List<StateLoadHolder> signalsToUnload = new ArrayList<>();
signals.forEach((pos, signal) -> {
final SignalStateInfo info = new SignalStateInfo(world, pos, signal);
SignalConfig.reset(info);
signalsToUnload.add(info);
signalsToUnload.add(
new StateLoadHolder(info, new LoadHolder<>(new StateInfo(world, tilePos))));
SignalStateHandler.removeListener(info, listener);
});
linkedBlocks.entrySet().stream().filter(entry -> !entry.getValue().equals(LinkType.SIGNAL))
Expand Down Expand Up @@ -127,19 +135,23 @@ public void read(final NBTWrapper wrapper) {
public void loadSignals(final World world) {
if (world.isRemote)
return;
final List<SignalStateInfo> signalInfos = new ArrayList<>();
final List<StateLoadHolder> signalInfos = new ArrayList<>();
signals.forEach((pos, signal) -> {
final SignalStateInfo info = new SignalStateInfo(world, pos, signal);
SignalStateHandler.addListener(info, listener);
signalInfos.add(
new StateLoadHolder(info, new LoadHolder<>(new StateInfo(world, thisPos))));
});
SignalStateHandler.loadSignals(signalInfos);
}

public void unloadSignals(final World world) {
if (world.isRemote)
return;
final List<SignalStateInfo> signalInfos = new ArrayList<>();
signals.forEach((pos, signal) -> signalInfos.add(new SignalStateInfo(world, pos, signal)));
final List<StateLoadHolder> signalInfos = new ArrayList<>();
signals.forEach((pos, signal) -> signalInfos
.add(new StateLoadHolder(new SignalStateInfo(world, pos, signal),
new LoadHolder<>(new StateInfo(world, thisPos)))));
SignalStateHandler.unloadSignals(signalInfos);
}

Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/troblecodings/signals/core/LoadHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.troblecodings.signals.core;

import java.util.Objects;

public class LoadHolder<T> {

public final T holder;

public LoadHolder(final T holder) {
this.holder = holder;
}

@Override
public int hashCode() {
return Objects.hash(holder);
}

@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final LoadHolder<?> other = (LoadHolder<?>) obj;
return Objects.equals(holder, other.holder);
}

}
34 changes: 34 additions & 0 deletions src/main/java/com/troblecodings/signals/core/StateLoadHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.troblecodings.signals.core;

import java.util.Objects;

import com.troblecodings.signals.handler.SignalStateInfo;

public class StateLoadHolder {

public final SignalStateInfo info;
public final LoadHolder<?> holder;

public StateLoadHolder(final SignalStateInfo info, final LoadHolder<?> holder) {
this.info = info;
this.holder = holder;
}

@Override
public int hashCode() {
return Objects.hash(holder, info);
}

@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final StateLoadHolder other = (StateLoadHolder) obj;
return Objects.equals(holder, other.holder) && Objects.equals(info, other.info);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ public static void readTileNBT(final StateInfo identifier, final NBTWrapper wrap
return;
LinkedPositions holder;
synchronized (ALL_LINKED_POS) {
holder = ALL_LINKED_POS.computeIfAbsent(identifier, _u -> new LinkedPositions());
holder = ALL_LINKED_POS.computeIfAbsent(identifier,
_u -> new LinkedPositions(identifier.pos));
}
holder.read(wrapper);
PathwayHolder grid;
Expand Down Expand Up @@ -161,7 +162,8 @@ public static boolean linkPosToSignalBox(final StateInfo identifier, final Block
return false;
LinkedPositions holder;
synchronized (ALL_LINKED_POS) {
holder = ALL_LINKED_POS.computeIfAbsent(identifier, _u -> new LinkedPositions());
holder = ALL_LINKED_POS.computeIfAbsent(identifier,
_u -> new LinkedPositions(identifier.pos));
}
final boolean linked = holder.addLinkedPos(linkPos, type);
if (!linked)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import com.troblecodings.signals.OpenSignalsMain;
import com.troblecodings.signals.SEProperty;
import com.troblecodings.signals.blocks.Signal;
import com.troblecodings.signals.core.LoadHolder;
import com.troblecodings.signals.core.SignalStateListener;
import com.troblecodings.signals.core.StateLoadHolder;
import com.troblecodings.signals.core.WriteBuffer;
import com.troblecodings.signals.enums.ChangedState;
import com.troblecodings.signals.tileentitys.SignalTileEntity;
Expand All @@ -30,6 +32,7 @@
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.PacketBuffer;
import net.minecraft.network.play.client.CPacketCustomPayload;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraft.world.chunk.Chunk;
Expand All @@ -50,7 +53,7 @@ private SignalStateHandler() {
private static ExecutorService IO_SERVICE = Executors.newFixedThreadPool(3);
private static final Map<SignalStateInfo, Map<SEProperty, String>> CURRENTLY_LOADED_STATES = new HashMap<>();
private static final Map<World, SignalStateFile> ALL_LEVEL_FILES = new HashMap<>();
private static final Map<SignalStateInfo, Integer> SIGNAL_COUNTER = new HashMap<>();
private static final Map<SignalStateInfo, List<LoadHolder<?>>> SIGNAL_COUNTER = new HashMap<>();
private static final Map<SignalStateInfo, List<SignalStateListener>> ALL_LISTENERS = new HashMap<>();
private static final String CHANNELNAME = "statehandlernet";
private static FMLEventChannel channel;
Expand Down Expand Up @@ -84,8 +87,10 @@ public static void createStates(final SignalStateInfo info,
CURRENTLY_LOADED_STATES.put(info, ImmutableMap.copyOf(states));
}
new Thread(() -> {
final List<LoadHolder<?>> list = new ArrayList<>();
list.add(new LoadHolder<>(new ChunkPos(info.pos)));
synchronized (SIGNAL_COUNTER) {
SIGNAL_COUNTER.put(info, 1);
SIGNAL_COUNTER.put(info, list);
}
sendToAll(info, states);
createToFile(info, states);
Expand Down Expand Up @@ -344,7 +349,7 @@ public static void onChunkWatch(final ChunkWatchEvent.Watch event) {
return;

final EntityPlayer player = event.getPlayer();
final List<SignalStateInfo> states = new ArrayList<>();
final List<StateLoadHolder> states = new ArrayList<>();
synchronized (ALL_LEVEL_FILES) {
if (!ALL_LEVEL_FILES.containsKey(world)) {
ALL_LEVEL_FILES.put(world,
Expand All @@ -360,7 +365,7 @@ public static void onChunkWatch(final ChunkWatchEvent.Watch event) {
final SignalTileEntity signalTile = (SignalTileEntity) tile;
final SignalStateInfo info = new SignalStateInfo(world, pos,
signalTile.getSignal());
states.add(info);
states.add(new StateLoadHolder(info, new LoadHolder<>(new ChunkPos(pos))));
synchronized (CURRENTLY_LOADED_STATES) {
if (CURRENTLY_LOADED_STATES.containsKey(info)) {
sendToPlayer(info, CURRENTLY_LOADED_STATES.get(info), player);
Expand All @@ -377,91 +382,97 @@ public static void onChunkUnWatch(final ChunkWatchEvent.UnWatch event) {
final World world = chunk.getWorld();
if (world.isRemote)
return;
final List<SignalStateInfo> states = new ArrayList<>();
final List<StateLoadHolder> states = new ArrayList<>();
chunk.getTileEntityMap().forEach((pos, tile) -> {
if (tile instanceof SignalTileEntity) {
final SignalTileEntity signalTile = (SignalTileEntity) tile;
states.add(new SignalStateInfo(world, pos, signalTile.getSignal()));
states.add(
new StateLoadHolder(new SignalStateInfo(world, pos, signalTile.getSignal()),
new LoadHolder<>(new ChunkPos(pos))));
}
});
unloadSignals(states);
}

public static void loadSignal(final SignalStateInfo info) {
public static void loadSignal(final StateLoadHolder info) {
loadSignal(info, null);
}

public static void loadSignals(final List<SignalStateInfo> signals) {
public static void loadSignals(final List<StateLoadHolder> signals) {
loadSignals(signals, null);
}

public static void loadSignal(final SignalStateInfo info, final @Nullable EntityPlayer player) {
public static void loadSignal(final StateLoadHolder info, final @Nullable EntityPlayer player) {
loadSignals(ImmutableList.of(info), player);
}

public static void loadSignals(final List<SignalStateInfo> signals,
public static void loadSignals(final List<StateLoadHolder> signals,
final @Nullable EntityPlayer player) {
if (signals == null || signals.isEmpty() || IO_SERVICE.isShutdown())
return;
IO_SERVICE.execute(() -> {
signals.forEach(info -> {
synchronized (ALL_LEVEL_FILES) {
if (!ALL_LEVEL_FILES.containsKey(info.world)) {
ALL_LEVEL_FILES.put(info.world,
if (!ALL_LEVEL_FILES.containsKey(info.info.world)) {
ALL_LEVEL_FILES.put(info.info.world,
new SignalStateFile(Paths.get("osfiles/signalfiles/"
+ ((WorldServer) info.world).getMinecraftServer().getName()
.replace(":", "").replace("/", "").replace("\\", "")
+ "/" + ((WorldServer) info.world).provider
+ ((WorldServer) info.info.world).getMinecraftServer()
.getName().replace(":", "").replace("/", "")
.replace("\\", "")
+ "/" + ((WorldServer) info.info.world).provider
.getDimensionType().getName().replace(":", ""))));
}
}
synchronized (SIGNAL_COUNTER) {
Integer count = SIGNAL_COUNTER.get(info);
if (count != null && count > 0) {
SIGNAL_COUNTER.put(info, ++count);
List<LoadHolder<?>> holders = SIGNAL_COUNTER.get(info.info);
if (holders != null && holders.size() > 0) {
if (!holders.contains(info.holder))
holders.add(info.holder);
return;
}
SIGNAL_COUNTER.put(info, 1);
holders = new ArrayList<>();
holders.add(info.holder);
SIGNAL_COUNTER.put(info.info, holders);
}
final Map<SEProperty, String> properties = readAndSerialize(info);
final Map<SEProperty, String> properties = readAndSerialize(info.info);
synchronized (CURRENTLY_LOADED_STATES) {
CURRENTLY_LOADED_STATES.put(info, properties);
CURRENTLY_LOADED_STATES.put(info.info, properties);
}
if (player == null) {
sendToAll(info, properties);
sendToAll(info.info, properties);
} else {
sendToPlayer(info, properties, player);
sendToPlayer(info.info, properties, player);
}
updateListeners(info, properties, ChangedState.ADDED_TO_CACHE);
updateListeners(info.info, properties, ChangedState.ADDED_TO_CACHE);
});
});
}

public static void unloadSignal(final SignalStateInfo info) {
public static void unloadSignal(final StateLoadHolder info) {
unloadSignals(ImmutableList.of(info));
}

public static void unloadSignals(final List<SignalStateInfo> signals) {
public static void unloadSignals(final List<StateLoadHolder> signals) {
if (signals == null || signals.isEmpty() || IO_SERVICE.isShutdown())
return;
IO_SERVICE.execute(() -> {
signals.forEach(info -> {
synchronized (SIGNAL_COUNTER) {
Integer count = SIGNAL_COUNTER.get(info);
if (count != null && count > 1) {
SIGNAL_COUNTER.put(info, --count);
final List<LoadHolder<?>> holders = SIGNAL_COUNTER.getOrDefault(info.info,
new ArrayList<>());
holders.remove(info.holder);
if (!holders.isEmpty())
return;
}
SIGNAL_COUNTER.remove(info);
}
SIGNAL_COUNTER.remove(info.info);
Map<SEProperty, String> properties;
synchronized (CURRENTLY_LOADED_STATES) {
properties = CURRENTLY_LOADED_STATES.remove(info);
properties = CURRENTLY_LOADED_STATES.remove(info.info);
}
if (properties == null)
return;
createToFile(info, properties);
updateListeners(info, properties, ChangedState.REMOVED_FROM_CACHE);
createToFile(info.info, properties);
updateListeners(info.info, properties, ChangedState.REMOVED_FROM_CACHE);
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.troblecodings.signals.OpenSignalsMain;
import com.troblecodings.signals.blocks.BasicBlock;
import com.troblecodings.signals.blocks.Signal;
import com.troblecodings.signals.core.LoadHolder;
import com.troblecodings.signals.core.StateInfo;
import com.troblecodings.signals.core.StateLoadHolder;
import com.troblecodings.signals.enums.LinkType;
import com.troblecodings.signals.handler.SignalBoxHandler;
import com.troblecodings.signals.handler.SignalStateHandler;
Expand Down Expand Up @@ -74,8 +76,10 @@ public boolean link(final BlockPos pos, final NBTTagCompound tag) {
} else if (block == OSBlocks.REDSTONE_OUT) {
type = LinkType.OUTPUT;
}
if (type.equals(LinkType.SIGNAL)) {
SignalStateHandler.loadSignal(new SignalStateInfo(world, pos, (Signal) block));
if (type.equals(LinkType.SIGNAL) && !world.isRemote) {
SignalStateHandler
.loadSignal(new StateLoadHolder(new SignalStateInfo(world, pos, (Signal) block),
new LoadHolder<>(new StateInfo(world, pos))));
}
return SignalBoxHandler.linkPosToSignalBox(new StateInfo(world, this.pos), pos,
(BasicBlock) block, type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
import com.troblecodings.signals.SEProperty;
import com.troblecodings.signals.blocks.RedstoneInput;
import com.troblecodings.signals.blocks.Signal;
import com.troblecodings.signals.core.LoadHolder;
import com.troblecodings.signals.core.SignalStateListener;
import com.troblecodings.signals.core.StateInfo;
import com.troblecodings.signals.core.StateLoadHolder;
import com.troblecodings.signals.enums.ChangedState;
import com.troblecodings.signals.enums.EnumMode;
import com.troblecodings.signals.enums.EnumState;
Expand Down Expand Up @@ -228,16 +231,18 @@ public void onLoad() {
final SignalStateInfo info = new SignalStateInfo(world, linkedSignalPosition,
linkedSignal);
if (linkedSignalPosition != null && linkedSignal != null) {
SignalStateHandler.loadSignal(info);
SignalStateHandler.loadSignal(
new StateLoadHolder(info, new LoadHolder<>(new StateInfo(world, pos))));
SignalStateHandler.addListener(info, listener);
}
}
}

public void unloadSignal() {
if (linkedSignalPosition != null & linkedSignal != null)
SignalStateHandler
.unloadSignal(new SignalStateInfo(world, linkedSignalPosition, linkedSignal));
SignalStateHandler.unloadSignal(new StateLoadHolder(
new SignalStateInfo(world, linkedSignalPosition, linkedSignal),
new LoadHolder<>(new StateInfo(world, pos))));
}

public BlockPos getLinkedPosition() {
Expand Down

0 comments on commit 64834a3

Please sign in to comment.