Skip to content

Commit

Permalink
Update Polymer Port
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Mar 26, 2023
1 parent 64dd122 commit 066360c
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 18 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ dependencies {
modImplementation include("eu.pb4:sgui:${project.sgui_version}")
modImplementation include("eu.pb4:polymer-resource-pack:${project.polymer_version}")
modImplementation include("eu.pb4:player-data-api:${project.pda_version}")
modImplementation include("fr.catcore:server-translations-api:${project.translation_version}")
modImplementation include("xyz.nucleoid:server-translations-api:${project.translation_version}")


modLocalRuntime "eu.pb4:polymer-autohost:${project.polymer_version}"
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod_menu_version=5.0.2
port_version=1


polymer_version=0.4.2+1.19.3
polymer_version=0.4.2+1.19.4
sgui_version=1.2.1+1.19.3
translation_version=2.0.0-beta.2+1.19.4+pre2
translation_version=2.0.0-beta.2+1.19.4-pre2
pda_version=0.2.2+1.19.3
4 changes: 3 additions & 1 deletion src/main/java/dev/emi/trinkets/TrinketsMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ public void onInitialize() {
TrinketsPoly.init();
CommandRegistrationCallback.EVENT.register((dispatcher, registry, env) ->
dispatcher.register(literal("trinkets")
.requires(source -> source.hasPermissionLevel(2))
.executes(ctx -> TrinketsFlatUI.open(ctx.getSource().getPlayerOrThrow()))
.then(CommandManager.literal("compact").executes(TrinketsPoly::toggleCompactCommand))
.then(
literal("set")
.requires(source -> source.hasPermissionLevel(2))
.then(
argument("group", string())
.then(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.emi.trinkets.mixin;

import dev.emi.trinkets.poly.TrinketsFlatUI;
import dev.emi.trinkets.poly.TrinketsPoly;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.network.packet.c2s.play.ClickSlotC2SPacket;
import net.minecraft.screen.slot.SlotActionType;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -23,8 +25,17 @@ public class ServerPlayNetworkHandlerMixin {
@Inject(method = "onClickSlot", at = @At(value = "INVOKE", target = "Lnet/minecraft/screen/ScreenHandler;disableSyncing()V", shift = At.Shift.BEFORE), cancellable = true)
private void polyport_trinkets_handleClick(ClickSlotC2SPacket packet, CallbackInfo ci) {
if (packet.getSyncId() == this.player.playerScreenHandler.syncId && packet.getSlot() >= 5 && packet.getSlot() <= 8 && packet.getActionType() == SlotActionType.PICKUP && packet.getButton() == 1) {
TrinketsFlatUI.open(this.player);
ci.cancel();
if (switch (packet.getSlot()) {
case 5 -> TrinketsPoly.CONFIG.helmetSlot;
case 6 -> TrinketsPoly.CONFIG.chestplateSlot;
case 7 -> TrinketsPoly.CONFIG.leggingsSlot;
case 8 -> TrinketsPoly.CONFIG.bootsSlot;
default -> false;
}) {
TrinketsFlatUI.open(this.player);
ci.cancel();

}
}
}
}
62 changes: 62 additions & 0 deletions src/main/java/dev/emi/trinkets/poly/PolyConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package dev.emi.trinkets.poly;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import dev.emi.trinkets.TrinketsMain;
import net.fabricmc.loader.api.FabricLoader;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

public class PolyConfig {
private final static Path CONFIG_PATH = FabricLoader.getInstance().getConfigDir().resolve("trinkets_polymer.json");

private static final Gson GSON = new GsonBuilder()
.disableHtmlEscaping().setLenient().setPrettyPrinting()
.create();

@SerializedName("use_compact_ui_as_default")
public boolean compactUi = true;

@SerializedName("enable_helmet_slot_opening")
public boolean helmetSlot = true;
@SerializedName("enable_chestplate_slot_opening")
public boolean chestplateSlot = true;
@SerializedName("enable_leggings_slot_opening")
public boolean leggingsSlot = true;
@SerializedName("enable_boots_slot_opening")
public boolean bootsSlot = true;


public static PolyConfig loadOrCreateConfig() {
try {
PolyConfig config;

if (Files.exists(CONFIG_PATH)) {
config = GSON.fromJson(Files.readString(CONFIG_PATH), PolyConfig.class);
} else {
config = new PolyConfig();
}

saveConfig(config);
return config;
} catch (IOException exception) {
TrinketsMain.LOGGER.error("Something went wrong while reading config!");
exception.printStackTrace();
return new PolyConfig();
}
}

public static void saveConfig(PolyConfig config) {
try {
Files.writeString(CONFIG_PATH, GSON.toJson(config), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
} catch (Exception e) {
TrinketsMain.LOGGER.error("Something went wrong while saving config!");
e.printStackTrace();
}
}
}
15 changes: 3 additions & 12 deletions src/main/java/dev/emi/trinkets/poly/TrinketsPoly.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,21 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.emi.trinkets.TrinketsMain;
import eu.pb4.playerdata.api.PlayerDataApi;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.nbt.NbtByte;
import net.minecraft.nbt.NbtType;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

public class TrinketsPoly {
public static final PolyConfig CONFIG = PolyConfig.loadOrCreateConfig();
public static final Identifier COMPACT_SETTING = new Identifier(TrinketsMain.MOD_ID, "compact_ui");

public static void init() {
CommandRegistrationCallback.EVENT.register(((dispatcher, registryAccess, environment) -> {
dispatcher.register(CommandManager.literal("trinkets")
.executes(ctx -> TrinketsFlatUI.open(ctx.getSource().getPlayerOrThrow()))
.then(CommandManager.literal("compact").executes(TrinketsPoly::toggleCompactCommand))
);
}));

Elements.FILLER.hashCode();
}

private static int toggleCompactCommand(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
public static int toggleCompactCommand(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
var player = ctx.getSource().getPlayerOrThrow();

var isCompact = !getIsCompact(player);
Expand All @@ -43,7 +34,7 @@ public static boolean getIsCompact(ServerPlayerEntity player) {
var data = PlayerDataApi.getGlobalDataFor(player, COMPACT_SETTING, NbtByte.TYPE);

if (data == null) {
return true;
return CONFIG.compactUi;
} else {
return data.byteValue() > 0;
}
Expand Down

0 comments on commit 066360c

Please sign in to comment.