Skip to content

Commit b6d43be

Browse files
committed
[Savestates] Add proper storage of tickListEntries
- Fixes pressure plates and other timed components not unpressing after a loadstate - [MCTCommon] Fix eventlistener not checking for interfaces in the super class
1 parent 34a6970 commit b6d43be

File tree

12 files changed

+229
-20
lines changed

12 files changed

+229
-20
lines changed

build.gradle

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ if(project.release=="false") {
1515
version = project.version+hash
1616
group = project.group
1717

18-
// compile for java 8
19-
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8
18+
java {
19+
// compile for java 8
20+
sourceCompatibility = targetCompatibility = 8
21+
}
2022

2123
loom {
2224
// set access widener
@@ -64,7 +66,7 @@ dependencies {
6466
//into 'run/mods/'
6567
//}
6668

67-
compileJava{
69+
compileJava {
6870
options.release = 8
6971
}
7072

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ org.gradle.jvmargs=-Xmx3G
33

44
# Fabric properties
55
minecraft_version=1.12.2
6-
loader_version=0.15.9
7-
loom_version=1.6-SNAPSHOT
6+
loader_version=0.16.9
7+
loom_version=1.8-SNAPSHOT
88

99
# Mod properties
1010
mod_name=Tool-Assisted Speedrun Mod

src/main/java/com/minecrafttas/mctcommon/events/EventListenerRegistry.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.minecrafttas.mctcommon.events;
22

3-
import org.apache.commons.lang3.ClassUtils;
4-
53
import java.lang.reflect.InvocationTargetException;
64
import java.lang.reflect.Method;
75
import java.util.ArrayList;
86
import java.util.HashMap;
97

8+
import org.apache.commons.lang3.ArrayUtils;
9+
import org.apache.commons.lang3.ClassUtils;
10+
1011
/**
1112
* Registry for making objects available to listen for events.<br>
1213
* <br>
@@ -94,7 +95,8 @@ public static void register(EventBase eventListener) {
9495
if (eventListener == null) {
9596
throw new NullPointerException("Tried to register a packethandler with value null");
9697
}
97-
for (Class<?> type : eventListener.getClass().getInterfaces()) {
98+
99+
for (Class<?> type : searchForInterfaces(eventListener.getClass())) {
98100
if (EventBase.class.isAssignableFrom(type)) {
99101

100102
// If a new event type is being registered, add a new arraylist
@@ -107,6 +109,18 @@ public static void register(EventBase eventListener) {
107109
}
108110
}
109111

112+
private static Class<?>[] searchForInterfaces(Class<?> clazz) {
113+
if (clazz == null) {
114+
return new Class<?>[] {};
115+
}
116+
Class<?>[] interfaces = clazz.getInterfaces();
117+
Class<?> superclass = clazz.getSuperclass();
118+
if (superclass != null && superclass != Object.class) {
119+
interfaces = ArrayUtils.addAll(interfaces, searchForInterfaces(superclass));
120+
}
121+
return interfaces;
122+
}
123+
110124
/**
111125
* Registers multiple objects to be an event listener. The objects must
112126
* implement an event extending {@link EventBase}
@@ -128,7 +142,7 @@ public static void unregister(EventBase eventListener) {
128142
if (eventListener == null) {
129143
throw new NullPointerException("Tried to unregister a packethandler with value null");
130144
}
131-
for (Class<?> type : eventListener.getClass().getInterfaces()) {
145+
for (Class<?> type : searchForInterfaces(eventListener.getClass())) {
132146
if (EventBase.class.isAssignableFrom(type)) {
133147
ArrayList<EventBase> registryList = EVENTLISTENER_REGISTRY.get(type);
134148
if (registryList != null) {

src/main/java/com/minecrafttas/tasmod/TASmod.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.minecrafttas.tasmod.playback.metadata.integrated.StartpositionMetadataExtension;
3030
import com.minecrafttas.tasmod.registries.TASmodPackets;
3131
import com.minecrafttas.tasmod.savestates.SavestateHandlerServer;
32+
import com.minecrafttas.tasmod.savestates.storage.NextTickListEntryStorage;
3233
import com.minecrafttas.tasmod.tickratechanger.TickrateChangerServer;
3334
import com.minecrafttas.tasmod.ticksync.TickSyncServer;
3435
import com.minecrafttas.tasmod.util.LoggerMarkers;
@@ -113,6 +114,7 @@ public void onInitialize() {
113114
PacketHandlerRegistry.register(startPositionMetadataExtension);
114115
PacketHandlerRegistry.register(tabCompletionUtils);
115116
PacketHandlerRegistry.register(commandFileCommand);
117+
EventListenerRegistry.register(new NextTickListEntryStorage());
116118
}
117119

118120
@Override

src/main/java/com/minecrafttas/tasmod/events/EventSavestate.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import com.minecrafttas.mctcommon.events.EventListenerRegistry.EventBase;
66

7+
import net.minecraft.server.MinecraftServer;
8+
79
public interface EventSavestate {
810

911
/**
@@ -19,7 +21,7 @@ interface EventServerSavestate extends EventBase {
1921
* @param target Target folder, where the savestate is copied to
2022
* @param current The current folder that will be copied from
2123
*/
22-
public void onServerSavestate(int index, Path target, Path current);
24+
public void onServerSavestate(MinecraftServer server, int index, Path target, Path current);
2325
}
2426

2527
/**
@@ -35,7 +37,7 @@ interface EventServerLoadstate extends EventBase {
3537
* @param target Target folder, where the savestate is copied to
3638
* @param current The current folder that will be copied from
3739
*/
38-
public void onServerLoadstate(int index, Path target, Path current);
40+
public void onServerLoadstate(MinecraftServer server, int index, Path target, Path current);
3941
}
4042

4143
/**
@@ -49,10 +51,10 @@ interface EventServerCompleteLoadstate extends EventBase {
4951
*/
5052
public void onServerLoadstateComplete();
5153
}
52-
54+
5355
@FunctionalInterface
5456
interface EventClientSavestate extends EventBase {
55-
57+
5658
public void onClientSavestate();
5759
}
5860
}

src/main/java/com/minecrafttas/tasmod/mixin/savestates/MixinWorldServer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* @author Scribble
2222
*/
2323
@Mixin(WorldServer.class)
24-
public class MixinWorldServer implements WorldServerDuck {
24+
public abstract class MixinWorldServer implements WorldServerDuck {
2525

2626
@Shadow
2727
private PlayerChunkMap playerChunkMap;
@@ -53,11 +53,17 @@ public void sendChunksToClient() {
5353
this.playerChunkMap.tick();
5454
}
5555

56+
/**
57+
* Retrieves the {@link #pendingTickListEntriesTreeSet}
58+
*/
5659
@Override
5760
public TreeSet<NextTickListEntry> getTickListEntriesTreeSet() {
5861
return this.pendingTickListEntriesTreeSet;
5962
}
6063

64+
/**
65+
* Retrieves the {@link #pendingTickListEntriesHashSet}
66+
*/
6167
@Override
6268
public Set<NextTickListEntry> getTickListEntriesHashSet() {
6369
return this.pendingTickListEntriesHashSet;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.minecrafttas.tasmod.mixin.storage;
2+
3+
import org.spongepowered.asm.mixin.Mixin;
4+
import org.spongepowered.asm.mixin.gen.Accessor;
5+
6+
import net.minecraft.world.NextTickListEntry;
7+
8+
@Mixin(NextTickListEntry.class)
9+
public interface AccessorNextTickListEntry {
10+
11+
@Accessor
12+
public long getTickEntryID();
13+
}

src/main/java/com/minecrafttas/tasmod/savestates/SavestateHandlerServer.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.nio.charset.StandardCharsets;
99
import java.nio.file.Files;
1010
import java.nio.file.Path;
11+
import java.nio.file.Paths;
1112
import java.nio.file.StandardCopyOption;
1213
import java.util.ArrayList;
1314
import java.util.Collections;
@@ -86,6 +87,8 @@ public static enum SavestateState {
8687
private final SavestatePlayerHandler playerHandler;
8788
private final SavestateWorldHandler worldHandler;
8889

90+
public static final Path storageDir = Paths.get("tasmod/");
91+
8992
private final Logger logger;
9093
public static boolean wasLoading;
9194

@@ -195,7 +198,7 @@ public void saveState(int savestateIndex, boolean tickrate0, boolean changeIndex
195198
Path currentfolder = savestateDirectory.resolve(".." + File.separator + worldname);
196199
Path targetfolder = getSavestateFile(indexToSave);
197200

198-
EventListenerRegistry.fireEvent(EventSavestate.EventServerSavestate.class, indexToSave, targetfolder, currentfolder);
201+
EventListenerRegistry.fireEvent(EventSavestate.EventServerSavestate.class, server, indexToSave, targetfolder, currentfolder);
199202

200203
if (Files.exists(targetfolder)) {
201204
logger.warn(LoggerMarkers.Savestate, "WARNING! Overwriting the savestate with the index {}", indexToSave);
@@ -334,7 +337,7 @@ public void loadState(int savestateIndex, boolean tickrate0, boolean changeIndex
334337
Path currentfolder = savestateDirectory.resolve(".." + File.separator + worldname);
335338
Path targetfolder = getSavestateFile(indexToLoad);
336339

337-
EventListenerRegistry.fireEvent(EventSavestate.EventServerLoadstate.class, indexToLoad, targetfolder, currentfolder);
340+
EventListenerRegistry.fireEvent(EventSavestate.EventServerLoadstate.class, server, indexToLoad, targetfolder, currentfolder);
338341

339342
/*
340343
* Prevents loading an InputSavestate when loading index 0 (Index 0 is the
@@ -625,7 +628,7 @@ private void saveSavestateDataFile(boolean legacy) {
625628
*/
626629
private void loadSavestateDataFile() {
627630
logger.trace(LoggerMarkers.Savestate, "Loading savestate data file");
628-
Path tasmodDir = savestateDirectory.resolve("../" + server.getFolderName() + "/tasmod/");
631+
Path tasmodDir = savestateDirectory.resolve("../" + server.getFolderName()).resolve(storageDir);
629632
Path savestateDat = tasmodDir.resolve("savestateData.txt");
630633

631634
if (!Files.exists(savestateDat)) {
@@ -655,7 +658,7 @@ private void loadSavestateDataFile() {
655658
public void loadCurrentIndexFromFile() {
656659
logger.trace(LoggerMarkers.Savestate, "Loading current index from file");
657660
int index = -1;
658-
Path tasmodDir = savestateDirectory.resolve("../" + server.getFolderName() + "/tasmod/");
661+
Path tasmodDir = savestateDirectory.resolve("../" + server.getFolderName()).resolve(storageDir);
659662
if (!Files.exists(tasmodDir)) {
660663
try {
661664
Files.createDirectory(tasmodDir);
@@ -806,7 +809,7 @@ public void onServerPacket(PacketID id, ByteBuffer buf, String username) throws
806809
if (player != null)
807810
player.sendMessage(new TextComponentString(TextFormatting.RED + "Failed to load a savestate: " + e.getCause().toString()));
808811

809-
LOGGER.error(e);
812+
LOGGER.throwing(e);
810813
TASmod.savestateHandlerServer.state = SavestateState.NONE;
811814
}
812815
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.minecrafttas.tasmod.savestates.storage;
2+
3+
import com.minecrafttas.tasmod.events.EventSavestate.EventServerLoadstate;
4+
import com.minecrafttas.tasmod.events.EventSavestate.EventServerSavestate;
5+
6+
public abstract class AbstractExtendStorage implements EventServerSavestate, EventServerLoadstate {
7+
}

0 commit comments

Comments
 (0)