Skip to content

Commit

Permalink
refactor: structure part & rich i18n (#101)
Browse files Browse the repository at this point in the history
Signed-off-by: Liyan Zhao <return65535@qq.com>
  • Loading branch information
zly2006 authored Mar 21, 2024
1 parent bf198b6 commit ee1ac3c
Show file tree
Hide file tree
Showing 43 changed files with 1,461 additions and 1,087 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.zly2006.reden.access

import net.minecraft.text.Text

interface TranslationStorageAccess {

@Suppress("INAPPLICABLE_JVM_NAME")
@get:JvmName("getTextMap\$reden")
val textMap: Map<String, Text>
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ class RedenException : Exception {
constructor(message: Text, cause: Throwable) : super(message.string, cause) {
this.displayMessage = message
}

override fun toString(): String {
return displayMessage.string
}
}
8 changes: 7 additions & 1 deletion src/main/java/com/github/zly2006/reden/malilib/GuiConfigs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ class GuiConfigs(parent: Screen? = null): GuiConfigsBase(
button.width + x + 2
}
val creditsButton = ButtonGeneric(finalX, 26, -1, 20, "Credits")
val sponsorsButton = ButtonGeneric(finalX, 26, -1, 20, StringUtils.translate("reden.widget.config.sponsor"))
val sponsorsButton = ButtonGeneric(
finalX + creditsButton.width + 2,
26,
-1,
20,
StringUtils.translate("reden.widget.config.sponsor")
)
addButton(creditsButton) { _, _ ->
onFunctionUsed("credits_malilibConfigScreen")
client!!.setScreen(CreditScreen(this))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,8 @@
import com.github.zly2006.reden.Reden;
import com.github.zly2006.reden.access.ServerData;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.ServerNetworkIo;
import net.minecraft.server.function.CommandFunctionManager;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

import java.util.List;

@Mixin(value = MinecraftServer.class, priority = Reden.REDEN_HIGHEST_MIXIN_PRIORITY)
public abstract class MixinServer implements ServerData.ServerDataAccess {
@Shadow
@Final
private CommandFunctionManager commandFunctionManager;

@Shadow
@Final
private List<Runnable> serverGuiTickables;

@Shadow
@Final
private ServerNetworkIo networkIo;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ private boolean forceOutline(boolean x) {
return x || !BlockOutline.INSTANCE.getBlocks().isEmpty();
}

@SuppressWarnings("deprecation")
@Inject(
method = "render",
at = @At(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.github.zly2006.reden.mixin.richTranslation;

import com.github.zly2006.reden.utils.richTranslation.RichTranslationKt;
import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.text.StringVisitable;
import net.minecraft.text.TranslatableTextContent;
import net.minecraft.util.Language;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Collections;
import java.util.List;

// overwrite owo
@Mixin(value = TranslatableTextContent.class, priority = 10)
public class MixinTranslatable {
@Shadow
@Final
private String key;

@Shadow
@Final
private Object[] args;

@Shadow
private List<StringVisitable> translations;

@Inject(
method = "updateTranslations",
at = @At(
value = "FIELD",
target = "Lnet/minecraft/text/TranslatableTextContent;languageCache:Lnet/minecraft/util/Language;",
ordinal = 1,
shift = At.Shift.AFTER
),
cancellable = true
)
private void translate(CallbackInfo ci, @Local Language language) {
var text = RichTranslationKt.processTranslate(language, key, args);
if (text != null) {
translations = Collections.singletonList(text);
ci.cancel();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.github.zly2006.reden.mixin.richTranslation;

import com.github.zly2006.reden.access.TranslationStorageAccess;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.client.resource.language.TranslationStorage;
import net.minecraft.resource.ResourceManager;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Mixin(TranslationStorage.class)
public class MixinTranslationStorage implements com.github.zly2006.reden.access.TranslationStorageAccess {
@Unique
private final Map<String, Text> textMap = new HashMap<>();
@Unique
private static final Map<String, Text> tempTextMap = new HashMap<>();

@Override
public @NotNull Map<String, Text> getTextMap$reden() {
return textMap;
}

@Inject(
method = "load(Lnet/minecraft/resource/ResourceManager;Ljava/util/List;Z)Lnet/minecraft/client/resource/language/TranslationStorage;",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/resource/language/TranslationStorage;load(Ljava/lang/String;Ljava/util/List;Ljava/util/Map;)V"
)
)
private static void loadCustomText(ResourceManager resourceManager, List<String> definitions, boolean rightToLeft, CallbackInfoReturnable<TranslationStorage> cir, @Local Identifier identifier) {
Gson gson = new Gson();
resourceManager.getAllResources(identifier).forEach(resource -> {
try {
var jo = gson.fromJson(new InputStreamReader(resource.getInputStream()), JsonObject.class);
jo.entrySet().stream().filter(it -> it.getValue() instanceof JsonArray).forEach(it -> {
MutableText text = Text.Serialization.fromJsonTree(it.getValue());
tempTextMap.put(it.getKey(), text);
});
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}

@Inject(
method = "load(Lnet/minecraft/resource/ResourceManager;Ljava/util/List;Z)Lnet/minecraft/client/resource/language/TranslationStorage;",
at = @At("TAIL")
)
private static void finish(ResourceManager resourceManager, List<String> definitions, boolean rightToLeft, CallbackInfoReturnable<TranslationStorage> cir) {
((TranslationStorageAccess) cir.getReturnValue()).getTextMap$reden().putAll(tempTextMap);
tempTextMap.clear();
}
}
6 changes: 2 additions & 4 deletions src/main/java/com/github/zly2006/reden/rvc/CuboidStructure.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.zly2006.reden.rvc

import net.minecraft.util.math.BlockPos
import net.minecraft.world.World
import com.github.zly2006.reden.rvc.tracking.PlacementInfo

class CuboidStructure(
name: String,
Expand Down Expand Up @@ -34,8 +33,7 @@ class CuboidStructure(
&& pos.y in 0 until ySize
&& pos.z in 0 until zSize
}

override fun createPlacement(world: World, origin: BlockPos): IPlacement {
override fun createPlacement(placementInfo: PlacementInfo): IPlacement {
TODO()
}
}
7 changes: 6 additions & 1 deletion src/main/java/com/github/zly2006/reden/rvc/IStructure.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.zly2006.reden.rvc

import com.github.zly2006.reden.rvc.io.StructureIO
import com.github.zly2006.reden.rvc.tracking.PlacementInfo
import com.github.zly2006.reden.rvc.tracking.WorldInfo
import net.minecraft.block.BlockState
import net.minecraft.nbt.NbtCompound
import net.minecraft.util.math.BlockPos
Expand Down Expand Up @@ -30,7 +32,10 @@ interface IStructure {
*/
fun load(path: Path)
fun isInArea(pos: RelativeCoordinate): Boolean
fun createPlacement(world: World, origin: BlockPos): IPlacement
fun createPlacement(world: World, origin: BlockPos) =
createPlacement(PlacementInfo(WorldInfo.of(world), origin))

fun createPlacement(placementInfo: PlacementInfo): IPlacement
fun getBlockState(pos: RelativeCoordinate): BlockState
fun getBlockEntityData(pos: RelativeCoordinate): NbtCompound?
fun getOrCreateBlockEntityData(pos: RelativeCoordinate): NbtCompound
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import java.util.*
interface IWritableStructure: IStructure {
fun setBlockState(pos: RelativeCoordinate, state: BlockState)
fun setBlockEntityData(pos: RelativeCoordinate, nbt: NbtCompound)
override fun getOrCreateBlockEntityData(pos: RelativeCoordinate): NbtCompound {
getBlockEntityData(pos)?.let { return it }
val nbt = NbtCompound()
setBlockEntityData(pos, nbt)
return nbt
}

override val entities: MutableMap<UUID, NbtCompound>

Expand Down Expand Up @@ -35,4 +41,4 @@ interface IWritableStructure: IStructure {
/**
* grammar sugar to [IWritableStructure.assign]
*/
operator fun IWritableStructure.remAssign(another: IStructure) = assign(another)
operator fun IWritableStructure.remAssign(another: IStructure) = assign(another)
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ abstract class ReadWriteStructure(override var name: String) : IWritableStructur
override var xSize: Int = 0; protected set
override var ySize: Int = 0; protected set
override var zSize: Int = 0; protected set
protected var minX: Int = Int.MAX_VALUE
protected var minY: Int = Int.MAX_VALUE
protected var minZ: Int = Int.MAX_VALUE
protected open var minX: Int = Int.MAX_VALUE
protected open var minY: Int = Int.MAX_VALUE
protected open var minZ: Int = Int.MAX_VALUE
private fun checkSize(pos: RelativeCoordinate) {
if (pos.x < minX) minX = pos.x
if (pos.y < minY) minY = pos.y
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/github/zly2006/reden/rvc/RelativeCoordinate.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.github.zly2006.reden.rvc

import kotlinx.serialization.Serializable
import net.minecraft.util.math.BlockPos

@Serializable
data class RelativeCoordinate(
override val x: Int,
override val y: Int,
Expand All @@ -8,4 +12,12 @@ data class RelativeCoordinate(
override fun getForOrigin(origin: Coordinate): Coordinate {
return AbsoluteCoordinate(x + origin.x, y + origin.y, z + origin.z)
}

companion object {
fun origin(origin: BlockPos) = RelativeBuilder(origin)

class RelativeBuilder(private val origin: BlockPos) {
fun block(pos: BlockPos) = RelativeCoordinate(pos.x - origin.x, pos.y - origin.y, pos.z - origin.z)
}
}
}
Loading

0 comments on commit ee1ac3c

Please sign in to comment.