Skip to content

Commit

Permalink
fix: crash the client if the client-side install fuji and edit a sign…
Browse files Browse the repository at this point in the history
… in multi-player server. + refactor: change the mixin generator into a non-destruction generator
  • Loading branch information
sakurawald committed Jul 21, 2024
1 parent cb782b4 commit 0054a33
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public abstract class ServerPlayerEntityMixin {
@Unique
ServerPlayerEntity player = (ServerPlayerEntity) (Object) this;


@Inject(method = "openEditSignScreen", at = @At("HEAD"))
private void $onUse(SignBlockEntity signBlockEntity, boolean bl, CallbackInfo ci) {
if (ci.isCancelled()) return;
Expand Down
8 changes: 5 additions & 3 deletions src/main/resources/fuji.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
"package": "io.github.sakurawald.module.mixin",
"compatibilityLevel": "JAVA_17",
"plugin": "io.github.sakurawald.module.mixin.ModuleMixinConfigPlugin",
"server": [
"color.sign.SignBlockEntityMixin",
"color.sign.ServerPlayerEntityMixin",
"color.anvil.AnvilScreenHandlerMixin"
],
"mixins": [
"command_warmup.ServerPlayNetworkHandler",
"anti_build.ServerPlayerInteractionManagerMixin",
"sit.InteractModifierMixin",
"whitelist.UserWhiteListMixin",
"teleport_warmup.ServerPlayerMixin",
"command_interactive.SignBlockMixin",
"color.sign.SignBlockEntityMixin",
"works.HopperBlockEntityMixin",
"disabler.chat_speed_disabler.ServerGamePacketListenerImplMixin",
"carpet.better_info.InfoCommandMixin",
Expand All @@ -21,7 +25,6 @@
"skin.PlayerListMixin",
"placeholder.ServerPlayNetworkHandlerMixin",
"anti_build.BlockMixin",
"color.sign.ServerPlayerEntityMixin",
"afk.ServerPlayerMixin",
"command_toolbox.seen.PlayerListMixin",
"_internal.low_level.MinecraftServerMixin",
Expand All @@ -38,7 +41,6 @@
"chat.ServerGamePacketListenerImplMixin",
"system_message.ComponentMixin",
"disabler.move_speed_disabler.ServerGamePacketListenerImplMixin",
"color.anvil.AnvilScreenHandlerMixin",
"_internal.low_level.ServerPlayerEntityMixin",
"carpet.fake_player_manager.PlayerCommandMixin",
"op_protect.ServerPlayNetworkHandlerMixin",
Expand Down
49 changes: 30 additions & 19 deletions src/test/java/generator/MixinRegistryGenTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package generator;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonWriter;
import io.github.sakurawald.config.handler.ConfigHandler;
import com.google.gson.*;
import io.github.sakurawald.module.ModuleManager;
import lombok.Cleanup;
import lombok.SneakyThrows;
Expand All @@ -14,10 +9,24 @@
import org.spongepowered.asm.mixin.Mixin;

import java.io.*;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;

public class MixinRegistryGenTest {

private List<JsonElement> collectJsonArray(JsonElement jsonElement, String key) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (!jsonObject.has(key)) return List.of();

JsonArray mixinsArray = jsonObject.get(key).getAsJsonArray();
return mixinsArray.asList();
}

private List<String> collectMixins(JsonElement jsonElement, String key) {
List<JsonElement> jsonElements = collectJsonArray(jsonElement, key);
return jsonElements.stream().map(JsonElement::getAsString).toList();
}

@SneakyThrows
@Test
void generate() {
Expand All @@ -26,26 +35,28 @@ void generate() {
File file = new File("src/main/resources/fuji.mixins.json");
@Cleanup Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
JsonElement jsonElement = JsonParser.parseReader(reader);
JsonArray mixinsArray = jsonElement.getAsJsonObject().get("mixins").getAsJsonArray();
Iterator<JsonElement> iterator = mixinsArray.iterator();
while (iterator.hasNext()) {
iterator.next();
iterator.remove();
}

List<String> registeredMixins = new ArrayList<>();
registeredMixins.addAll(collectMixins(jsonElement, "mixins"));
registeredMixins.addAll(collectMixins(jsonElement, "client"));
registeredMixins.addAll(collectMixins(jsonElement, "server"));

/* reflect */
String mixinPackage = ModuleManager.class.getPackageName() + ".mixin";
Reflections reflections = new Reflections(mixinPackage);
List<String> unregisteredMixins = new ArrayList<>();
for (Class<?> clazz : reflections.getTypesAnnotatedWith(Mixin.class)) {
String mixinName = clazz.getName().substring(mixinPackage.length() + 1);
mixinsArray.add(mixinName);

if (!registeredMixins.contains(mixinName)) {
unregisteredMixins.add(mixinName);
}
}

Gson gson = ConfigHandler.getGson();
JsonWriter jsonWriter = gson.newJsonWriter(new BufferedWriter(new FileWriter(file)));
gson.toJson(jsonElement, jsonWriter);
jsonWriter.close();
}
if (!unregisteredMixins.isEmpty()) {
throw new RuntimeException("Mixins not registered: " + unregisteredMixins);
}

}
}

0 comments on commit 0054a33

Please sign in to comment.