Skip to content

Commit

Permalink
ref: better code performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Uhutown committed Oct 18, 2023
1 parent 64834a3 commit 4767759
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 39 deletions.
37 changes: 16 additions & 21 deletions src/main/java/com/troblecodings/signals/handler/NameHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,10 @@ public static void onWorldSave(final WorldEvent.Save event) {
synchronized (ALL_NAMES) {
map = ImmutableMap.copyOf(ALL_NAMES);
}
new Thread(() -> {
synchronized (ALL_LEVEL_FILES) {
map.entrySet().stream().filter(entry -> entry.getKey().world.equals(world))
.forEach(entry -> createToFile(entry.getKey(), entry.getValue()));
}
}, "OSNameHandler:Save").start();
IO_SERVICE.execute(() -> {
map.entrySet().stream().filter(entry -> entry.getKey().world.equals(world))
.forEach(entry -> createToFile(entry.getKey(), entry.getValue()));
});
}

@SubscribeEvent
Expand Down Expand Up @@ -201,16 +199,6 @@ public static void onChunkWatch(final ChunkWatchEvent.Watch event) {
return;
final EntityPlayer player = event.getPlayer();
final List<StateInfo> states = new ArrayList<>();
synchronized (ALL_LEVEL_FILES) {
if (!ALL_LEVEL_FILES.containsKey(world)) {
ALL_LEVEL_FILES.put(world,
new NameHandlerFile(Paths.get("osfiles/namefiles/"
+ ((WorldServer) world).getMinecraftServer().getName()
.replace(":", "").replace("/", "").replace("\\", "")
+ "/" + ((WorldServer) world).provider.getDimensionType().getName()
.replace(":", ""))));
}
}
chunk.getTileEntityMap().forEach((pos, tile) -> {
if (tile instanceof SignalTileEntity || tile instanceof RedstoneIOTileEntity) {
final StateInfo info = new StateInfo(world, pos);
Expand Down Expand Up @@ -262,14 +250,21 @@ private static void loadNames(final List<StateInfo> infos,
}
LOAD_COUNTER.put(info, 1);
String name;
NameHandlerFile file;
synchronized (ALL_LEVEL_FILES) {
final NameHandlerFile file = ALL_LEVEL_FILES.get(info.world);
if (file == null)
return;
synchronized (file) {
name = file.getString(info.pos);
file = ALL_LEVEL_FILES.get(info.world);
if (file == null) {
file = new NameHandlerFile(Paths.get("osfiles/namefiles/"
+ ((WorldServer) info.world).getMinecraftServer().getName()
.replace(":", "").replace("/", "").replace("\\", "")
+ "/" + ((WorldServer) info.world).provider.getDimensionType()
.getName().replace(":", "")));
ALL_LEVEL_FILES.put(info.world, file);
}
}
synchronized (file) {
name = file.getString(info.pos);
}
synchronized (ALL_NAMES) {
ALL_NAMES.put(info, name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,17 @@ private static void createToFile(final SignalStateInfo info,
final Map<SEProperty, String> states) {
if (states == null)
return;
final SignalStateFile file;
SignalStateFile file;
synchronized (ALL_LEVEL_FILES) {
file = ALL_LEVEL_FILES.get(info.world);
if (file == null) {
return;
file = new SignalStateFile(Paths.get("osfiles/signalfiles/"
+ ((WorldServer) info.world).getMinecraftServer().getName().replace(":", "")
.replace("/", "").replace("\\", "")
+ "/" + ((WorldServer) info.world).provider.getDimensionType().getName()
.replace(":", "")));
}
ALL_LEVEL_FILES.put(info.world, file);
}
SignalStatePos pos = file.find(info.pos);
if (pos == null) {
Expand Down Expand Up @@ -200,6 +205,10 @@ public static Map<SEProperty, String> getStates(final SignalStateInfo info) {
if (info.world.isRemote) {
return new HashMap<>();
}
synchronized (SIGNAL_COUNTER) {
if (SIGNAL_COUNTER.containsKey(info))
return new HashMap<>();
}
return readAndSerialize(info);
}
}
Expand All @@ -225,9 +234,15 @@ private static Map<SEProperty, String> readAndSerialize(final SignalStateInfo st
SignalStateFile file;
synchronized (ALL_LEVEL_FILES) {
file = ALL_LEVEL_FILES.get(stateInfo.world);
if (file == null) {
file = new SignalStateFile(Paths.get("osfiles/signalfiles/"
+ ((WorldServer) stateInfo.world).getMinecraftServer().getName()
.replace(":", "").replace("/", "").replace("\\", "")
+ "/" + ((WorldServer) stateInfo.world).provider.getDimensionType()
.getName().replace(":", "")));
}
ALL_LEVEL_FILES.put(stateInfo.world, file);
}
if (file == null)
return new HashMap<>();
SignalStatePos pos = file.find(stateInfo.pos);
if (pos == null) {
if (stateInfo.world.isRemote) {
Expand Down Expand Up @@ -264,10 +279,8 @@ public static void onWorldSave(final WorldEvent.Save save) {
maps = ImmutableMap.copyOf(CURRENTLY_LOADED_STATES);
}
IO_SERVICE.execute(() -> {
synchronized (ALL_LEVEL_FILES) {
maps.entrySet().stream().filter(entry -> entry.getKey().world.equals(world))
.forEach(entry -> createToFile(entry.getKey(), entry.getValue()));
}
maps.entrySet().stream().filter(entry -> entry.getKey().world.equals(world))
.forEach(entry -> createToFile(entry.getKey(), entry.getValue()));
});
}

Expand Down Expand Up @@ -350,16 +363,6 @@ public static void onChunkWatch(final ChunkWatchEvent.Watch event) {

final EntityPlayer player = event.getPlayer();
final List<StateLoadHolder> states = new ArrayList<>();
synchronized (ALL_LEVEL_FILES) {
if (!ALL_LEVEL_FILES.containsKey(world)) {
ALL_LEVEL_FILES.put(world,
new SignalStateFile(Paths.get("osfiles/signalfiles/"
+ ((WorldServer) world).getMinecraftServer().getName()
.replace(":", "").replace("/", "").replace("\\", "")
+ "/" + ((WorldServer) world).provider.getDimensionType().getName()
.replace(":", ""))));
}
}
chunk.getTileEntityMap().forEach((pos, tile) -> {
if (tile instanceof SignalTileEntity) {
final SignalTileEntity signalTile = (SignalTileEntity) tile;
Expand Down

0 comments on commit 4767759

Please sign in to comment.