Skip to content

Commit

Permalink
Update to 1.20.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Sep 18, 2023
1 parent fecf59e commit 6d4783f
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private Localization() {}

@Nullable
public static NbtCompound itemStackNbt(ItemStack stack, ServerPlayerEntity target) {
return itemStackNbt(stack, (LocalizationTarget) target);
return itemStackNbt(stack, LocalizationTarget.of(target));
}

@Nullable
Expand All @@ -28,7 +28,7 @@ public static NbtCompound itemStackNbt(ItemStack stack, ServerLanguage language)
}

public static ItemStack itemStack(ItemStack stack, ServerPlayerEntity target) {
return itemStack(stack, (LocalizationTarget) target);
return itemStack(stack, LocalizationTarget.of(target));
}

public static ItemStack itemStack(ItemStack stack, LocalizationTarget target) {
Expand All @@ -42,7 +42,7 @@ public static ItemStack itemStack(ItemStack stack, ServerLanguage language) {
}

public static Text text(Text text, ServerPlayerEntity target) {
return text(text, (LocalizationTarget) target);
return text(text, LocalizationTarget.of(target));
}

public static Text text(Text text, LocalizationTarget target) {
Expand All @@ -54,7 +54,7 @@ public static Text text(Text text, ServerLanguage language) {
}

public static Text text(Text text, ServerPlayerEntity target, boolean deep) {
return text(text, (LocalizationTarget) target, deep);
return text(text, LocalizationTarget.of(target), deep);
}

public static Text text(Text text, LocalizationTarget target, boolean deep) {
Expand All @@ -67,7 +67,7 @@ public static Text text(Text text, ServerLanguage language, boolean deep) {

@Nullable
public static String raw(String key, ServerPlayerEntity target) {
return raw(key, (LocalizationTarget) target);
return raw(key, LocalizationTarget.of(target));
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import xyz.nucleoid.server.translations.api.language.ServerLanguage;
import xyz.nucleoid.server.translations.impl.LanguageGetter;
import xyz.nucleoid.server.translations.impl.ServerTranslations;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.packettweaker.PacketContext;
Expand All @@ -11,7 +12,13 @@ public interface LocalizationTarget {
@Nullable
static LocalizationTarget forPacket() {
PacketContext context = PacketContext.get();
return (LocalizationTarget) context.getTarget();
if (context != null) {
var options = context.getClientOptions();
if (options != null) {
return options::language;
}
}
return null;
}

@Nullable
Expand All @@ -22,14 +29,18 @@ default ServerLanguage getLanguage() {
}

static LocalizationTarget of(ServerPlayerEntity player) {
return (LocalizationTarget) player;
return ((LanguageGetter) player)::stapi$getLanguage;
}

static LocalizationTarget of(ServerPlayNetworkHandler handler) {
return (LocalizationTarget) handler;
return of(handler.getPlayer());
}

static LocalizationTarget of() {
static LocalizationTarget ofSystem() {
return ServerTranslations.INSTANCE.systemTarget;
}
@Deprecated
static LocalizationTarget of() {
return ofSystem();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package xyz.nucleoid.server.translations.impl;

public interface LanguageGetter {
String stapi$getLanguage();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.google.common.collect.Multimap;
import com.mojang.logging.LogUtils;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.server.MinecraftServer;
import xyz.nucleoid.server.translations.api.LocalizationTarget;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectRBTreeMap;
Expand All @@ -27,7 +29,7 @@
import java.util.concurrent.Executor;
import java.util.function.Supplier;

public final class ServerTranslations implements IdentifiableResourceReloadListener, ModInitializer {
public final class ServerTranslations implements IdentifiableResourceReloadListener, ModInitializer, ServerLifecycleEvents.ServerStopped {
public static final String ID = "server_translations_api";
public static final Logger LOGGER = LogUtils.getLogger();

Expand Down Expand Up @@ -181,10 +183,17 @@ public String getCodeAlias(String code) {
@Override
public void onInitialize() {
ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(ServerTranslations.INSTANCE);
ServerLifecycleEvents.SERVER_STOPPED.register(ServerTranslations.INSTANCE);
}

@Override
public Identifier getFabricId() {
return new Identifier(ID, "translations");
}

@Override
public void onServerStopped(MinecraftServer server) {
this.translations.clear();
this.serverLanguages.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ public static TranslationMap loadBuiltInTranslation() {

public static Multimap<String, Supplier<TranslationMap>> collectDataPackTranslations(ResourceManager manager) {
Multimap<String, Supplier<TranslationMap>> translationSuppliers = HashMultimap.create();

for (Identifier path : manager.findResources("lang", path -> path.getPath().endsWith(".json")).keySet()) {
String code = getLanguageCodeForPath(path);

translationSuppliers.put(code, () -> {
TranslationMap map = new TranslationMap();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@ public SystemDelegatedLanguage(Language vanilla) {
public String get(String key) {
String override = this.getSystemLanguage().serverTranslations().getOrNull(key);
if (override != null) {
return override;
return this.vanilla.get(key, override);
}
return this.vanilla.get(key);
}

@Override
public String get(String key, String fallback) {
String override = this.getSystemLanguage().serverTranslations().getOrNull(key);
if (override != null) {
return override;
}
return this.vanilla.get(key, fallback);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package xyz.nucleoid.server.translations.mixin;

import xyz.nucleoid.server.translations.api.LocalizationTarget;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import xyz.nucleoid.server.translations.impl.LanguageGetter;

@Mixin(ServerPlayerEntity.class)
public class ServerPlayerEntityMixin implements LocalizationTarget {
@Shadow public ServerPlayNetworkHandler networkHandler;
public class ServerPlayerEntityMixin implements LanguageGetter {
@Shadow private String language;

@Override
public String getLanguageCode() {
return this.networkHandler != null ? ((LocalizationTarget) this.networkHandler).getLanguageCode() : null;
public String stapi$getLanguage() {
return this.language;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@Mixin(Language.class)
public class LanguageMixin {
@ModifyVariable(method = "setInstance", at = @At("HEAD"), argsOnly = true)
@ModifyVariable(method = "setInstance", at = @At("HEAD"))
private static Language stapi$modifyLanguage(Language language) {
String languageCode = MinecraftClient.getInstance().getLanguageManager().getLanguage();
ServerLanguageDefinition languageDefinition = ServerTranslations.INSTANCE.getLanguageDefinition(languageCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -14,15 +15,15 @@
public class BlockEntityDataMixin {
@Shadow @Final private BlockEntityType<?> type;

@ModifyArg(method = "write", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/PacketByteBuf;writeNbt(Lnet/minecraft/nbt/NbtCompound;)Lnet/minecraft/network/PacketByteBuf;"))
private NbtCompound stapi$translateNbt(NbtCompound compound) {
if (SignNbtLocalizer.isSign(this.type) && compound != null) {
@ModifyArg(method = "write", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/PacketByteBuf;writeNbt(Lnet/minecraft/nbt/NbtElement;)Lnet/minecraft/network/PacketByteBuf;"))
private NbtElement stapi$translateNbt(NbtElement nbt) {
if (SignNbtLocalizer.isSign(this.type) && nbt instanceof NbtCompound compound) {
var target = LocalizationTarget.forPacket();

if (target != null) {
return SignNbtLocalizer.translateNbt(compound, LocalizationTarget.forPacket());
}
}
return compound;
return nbt;
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
package xyz.nucleoid.server.translations.mixin.packet;

import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtString;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.server.translations.api.Localization;
import xyz.nucleoid.server.translations.api.LocalizationTarget;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import xyz.nucleoid.server.translations.impl.ServerTranslations;
import xyz.nucleoid.server.translations.impl.nbt.SignNbtLocalizer;

@Mixin(BlockEntityUpdateS2CPacket.class)
public class BlockEntityUpdateS2CPacketMixin {

@Shadow @Final private BlockEntityType<?> blockEntityType;

@ModifyArg(method = "write", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/PacketByteBuf;writeNbt(Lnet/minecraft/nbt/NbtCompound;)Lnet/minecraft/network/PacketByteBuf;"))
private NbtCompound stapi$translateNbt(NbtCompound nbtCompound) {
@Shadow @Final @Nullable private NbtCompound nbt;

if (SignNbtLocalizer.isSign(this.blockEntityType) && nbtCompound != null) {
@ModifyArg(method = "write", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/PacketByteBuf;writeNbt(Lnet/minecraft/nbt/NbtElement;)Lnet/minecraft/network/PacketByteBuf;"))
private NbtElement stapi$translateNbt(NbtElement nbt) {

if (SignNbtLocalizer.isSign(this.blockEntityType) && nbt instanceof NbtCompound nbtCompound) {
var target = LocalizationTarget.forPacket();

if (target != null) {
return SignNbtLocalizer.translateNbt(nbtCompound, LocalizationTarget.forPacket());
}
}
return nbtCompound;
return nbt;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package xyz.nucleoid.server.translations.mixin.packet;

import net.minecraft.nbt.NbtElement;
import xyz.nucleoid.server.translations.api.LocalizationTarget;
import xyz.nucleoid.server.translations.impl.nbt.StackNbtLocalizer;
import net.minecraft.item.Item;
Expand All @@ -23,7 +24,7 @@ public class PacketByteBufMixin {
method = "writeItemStack",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/network/PacketByteBuf;writeNbt(Lnet/minecraft/nbt/NbtCompound;)Lnet/minecraft/network/PacketByteBuf;",
target = "Lnet/minecraft/network/PacketByteBuf;writeNbt(Lnet/minecraft/nbt/NbtElement;)Lnet/minecraft/network/PacketByteBuf;",
shift = At.Shift.BEFORE
)
)
Expand All @@ -35,17 +36,18 @@ public class PacketByteBufMixin {
method = "writeItemStack",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/network/PacketByteBuf;writeNbt(Lnet/minecraft/nbt/NbtCompound;)Lnet/minecraft/network/PacketByteBuf;"
target = "Lnet/minecraft/network/PacketByteBuf;writeNbt(Lnet/minecraft/nbt/NbtElement;)Lnet/minecraft/network/PacketByteBuf;"
)
)
private NbtCompound stapi$writeItemStackTag(NbtCompound tag) {
LocalizationTarget target = LocalizationTarget.forPacket();
if (target != null) {
tag = StackNbtLocalizer.localize(this.stapi$cachedStack, tag, target);
private NbtElement stapi$writeItemStackTag(NbtElement nbt) {
if (nbt instanceof NbtCompound tag) {
LocalizationTarget target = LocalizationTarget.forPacket();
if (target != null) {
nbt = StackNbtLocalizer.localize(this.stapi$cachedStack, tag, target);
}
this.stapi$cachedStack = null;
}
this.stapi$cachedStack = null;

return tag;
return nbt;
}

@Inject(method = "readItemStack", at = @At(value = "RETURN", ordinal = 1), locals = LocalCapture.CAPTURE_FAILHARD)
Expand Down
1 change: 0 additions & 1 deletion api/src/main/resources/server_translations_api.mixin.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"mixins": [
"LanguageMixin",
"ServerPlayerEntityMixin",
"ServerPlayNetworkHandlerMixin",
"packet.BlockEntityDataMixin",
"packet.BlockEntityUpdateS2CPacketMixin",
"packet.PacketByteBufMixin",
Expand Down
12 changes: 6 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ plugins {
}

class Globals {
static def baseVersion = "2.0.0"
static def mcVersion = "1.20"
static def baseVersion = "2.1.0"
static def mcVersion = "1.20.2-rc2"
static def yarnVersion = "+build.1"
}

Expand Down Expand Up @@ -43,17 +43,17 @@ allprojects {
name = "Jitpack"
url = "https://jitpack.io"
}
mavenLocal()
//mavenLocal()
}

dependencies {
minecraft "com.mojang:minecraft:$Globals.mcVersion"
mappings "net.fabricmc:yarn:${Globals.mcVersion}${Globals.yarnVersion}:v2"
modImplementation "net.fabricmc:fabric-loader:0.14.21"
modImplementation "net.fabricmc:fabric-loader:0.14.22"

modImplementation include("xyz.nucleoid:packet-tweaker:0.4.0+1.19.4")
modImplementation include("xyz.nucleoid:packet-tweaker:0.5.0+1.20.2-rc1")

modImplementation "net.fabricmc.fabric-api:fabric-api:0.83.0+1.20"
modImplementation "net.fabricmc.fabric-api:fabric-api:0.88.5+1.20.2"
}

processResources {
Expand Down

This file was deleted.

Loading

0 comments on commit 6d4783f

Please sign in to comment.