Skip to content

Commit

Permalink
Handle invalid game portals in non-lossy way
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Dec 6, 2024
1 parent fc6f449 commit fd42b12
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.mojang.serialization.JsonOps;
import com.mojang.serialization.MapCodec;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.resource.ResourceManager;
Expand All @@ -12,6 +13,8 @@
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.plasmid.impl.Plasmid;
import xyz.nucleoid.plasmid.api.util.TinyRegistry;
import xyz.nucleoid.plasmid.impl.PlasmidConfig;
import xyz.nucleoid.plasmid.impl.portal.game.InvalidGamePortalBackend;

import java.io.IOException;
import java.util.Collection;
Expand Down Expand Up @@ -86,7 +89,12 @@ private Map<Identifier, GamePortalConfig> loadConfigs(DynamicRegistryManager reg
var json = JsonParser.parseReader(reader);
var identifier = identifierFromPath(path);
GamePortalConfig.CODEC.parse(ops, json)
.resultOrPartial(error -> Plasmid.LOGGER.error("Failed to parse game portal at {}: {}", path, error))
.resultOrPartial(error -> {
Plasmid.LOGGER.error("Failed to parse game portal at {}: {}", path, error);
if (PlasmidConfig.get().ignoreInvalidGames()) {
configs.put(identifier, InvalidGamePortalBackend.CONFIG);
}
})
.ifPresent(config -> configs.put(identifier, config));
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package xyz.nucleoid.plasmid.impl.portal.game;

import com.mojang.serialization.MapCodec;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import xyz.nucleoid.plasmid.api.game.config.CustomValuesConfig;
import xyz.nucleoid.plasmid.impl.portal.GamePortalBackend;
import xyz.nucleoid.plasmid.impl.portal.GamePortalConfig;

public record InvalidGamePortalBackend(Identifier identifier) implements GamePortalBackend {
public static final GamePortalConfig CONFIG = new GamePortalConfig() {
@Override
public GamePortalBackend createBackend(MinecraftServer server, Identifier id) {
return new InvalidGamePortalBackend(id);
}

@Override
public CustomValuesConfig custom() {
return CustomValuesConfig.empty();
}

@Override
public MapCodec<? extends GamePortalConfig> codec() {
return MapCodec.unit(this);
}
};

@Override
public Text getName() {
return Text.literal("Invalid portal'" + this.identifier + "'");
}

@Override
public void applyTo(ServerPlayerEntity player, boolean alt) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ private void removeHologram() {
@Inject(method = "tick", at = @At("HEAD"))
private void onTick(CallbackInfo ci) {
if (this.loadedPortalId != null) {
this.tryConnectTo(this.loadedPortalId);
this.loadedPortalId = null;
if (this.tryConnectTo(this.loadedPortalId)) {
this.loadedPortalId = null;
}
}
}

Expand Down

0 comments on commit fd42b12

Please sign in to comment.