fix: 修复了Windows下中文乱码及与PCA不兼容的问题

This commit is contained in:
Disy authored and zly2006 committed 2024-07-11 12:14:02 +08:00
1 parent 42d59762a7
commit 15764d67bc
16 files changed
+87 -165

No files matched your search

+1
View File
@@ -48,6 +48,7 @@ dependencies {
// Fabric Permission API
include(modImplementation('me.lucko:fabric-permissions-api:0.2-SNAPSHOT'))
include(modImplementation('com.github.Fallen-Breath:conditional-mixin:v0.4.0'))
// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
+1 -1
View File
@@ -9,7 +9,7 @@ org.gradle.jvmargs=-Xmx1G
# Mod Properties
mod_version = 0.6.6
mod_version = 0.7.1
maven_group = com.github.zly2006
archives_base_name = carpet-sls-addition
@@ -2,8 +2,11 @@ package com.github.zly2006.carpetslsaddition;
import carpet.CarpetExtension;
import carpet.CarpetServer;
import com.github.zly2006.carpetslsaddition.command.BotCommand;
import com.github.zly2006.carpetslsaddition.command.HatCommand;
import com.github.zly2006.carpetslsaddition.command.SitCommand;
import com.github.zly2006.carpetslsaddition.network.pca.PcaSyncProtocol;
import com.github.zly2006.carpetslsaddition.util.SitEntity;
import com.github.zly2006.carpetslsaddition.util.access.SettingsManagerAccessor;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@@ -13,28 +16,14 @@ import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.Version;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.decoration.ArmorStandEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.ShapedRecipe;
import net.minecraft.recipe.book.CraftingRecipeCategory;
import net.minecraft.server.MinecraftServer;
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.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.world.World;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.nio.charset.StandardCharsets;
import java.util.Map;
public class ServerMain implements ModInitializer, CarpetExtension {
@@ -51,13 +40,12 @@ public class ServerMain implements ModInitializer, CarpetExtension {
public static MinecraftServer server;
public static final boolean tisCarpetLoaded = FabricLoader.getInstance().isModLoaded("carpet-tis-addition");
public static final String ITEM_NAME = "item_name";
public static final String OBSIDIAN_PICKAXE = "obsidian_pickaxe";
@Override
public void onInitialize() {
INSTANCE = this;
CarpetServer.manageExtension(this);
PcaSyncProtocol.init();
}
@Override
@@ -65,86 +53,41 @@ public class ServerMain implements ModInitializer, CarpetExtension {
ServerMain.server = server;
CarpetServer.settingsManager.parseSettingsClass(SLSCarpetSettings.class);
((SettingsManagerAccessor) CarpetServer.settingsManager).loadSettings();
if (false) {
ItemStack stack = new ItemStack(Items.STONE_PICKAXE);
stack.setCustomName(Text.empty().formatted(Formatting.LIGHT_PURPLE).append("黑曜石镐"));
stack.getOrCreateSubNbt(MOD_ID).putString(ITEM_NAME, OBSIDIAN_PICKAXE);
DefaultedList<Ingredient> ingredients = DefaultedList.ofSize(9, Ingredient.EMPTY);
ingredients.set(0, Ingredient.ofItems(Items.OBSIDIAN));
ingredients.set(1, Ingredient.ofItems(Items.OBSIDIAN));
ingredients.set(2, Ingredient.ofItems(Items.OBSIDIAN));
ingredients.set(4, Ingredient.ofItems(Items.STICK));
ingredients.set(7, Ingredient.ofItems(Items.STICK));
server.getRecipeManager().setRecipes(List.of(new ShapedRecipe(new Identifier(MOD_ID, "recipe.obsidian_pickaxe"), MOD_ID, CraftingRecipeCategory.EQUIPMENT, 3, 3, ingredients, stack)));
}
}
@Override
public Map<String, String> canHasTranslations(String lang) {
Map<String, String> translation = Maps.newHashMap();
PcaSyncProtocol.init();
String jsonFile;
try {
try (InputStream stream = ServerMain.class.getResourceAsStream("/assets/slsaddition/lang/%s.json".formatted(lang))) {
assert stream != null;
jsonFile = new String(stream.readAllBytes());
jsonFile = new String(stream.readAllBytes(), StandardCharsets.UTF_8);
}
} catch (IOException | NullPointerException ignored) {
try {
try (InputStream stream = ServerMain.class.getResourceAsStream("/assets/slsaddition/lang/%s.json".formatted("en_us"))) {
assert stream != null;
jsonFile = new String(stream.readAllBytes());
jsonFile = new String(stream.readAllBytes(), StandardCharsets.UTF_8);
}
} catch (IOException | NullPointerException e) {
return translation;
}
}
GSON.fromJson(jsonFile, JsonObject.class).entrySet().stream()
.filter(entry -> entry.getKey().startsWith("carpet."))
.filter(entry -> entry.getValue().isJsonPrimitive())
.map(entry -> Map.entry(entry.getKey(), entry.getValue().getAsString()))
.forEach(entry -> translation.put(entry.getKey(), entry.getValue()));
.forEach(entry -> {
translation.put(entry.getKey(), entry.getValue());
});
return translation;
}
@Override
public void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess commandBuildContext) {
dispatcher.register(CommandManager.literal("hat")
.requires(ServerCommandSource::isExecutedByPlayer)
.requires((commandSource) -> SLSCarpetSettings.canUseHatCommand)
.executes(context -> {
ServerPlayerEntity player = context.getSource().getPlayer();
assert player != null;
ItemStack stack = player.getMainHandStack();
ItemStack head = player.getEquippedStack(EquipmentSlot.HEAD);
player.equipStack(EquipmentSlot.HEAD, stack);
player.getInventory().setStack(player.getInventory().selectedSlot, head);
player.currentScreenHandler.syncState();
return 1;
}));
dispatcher.register(CommandManager.literal("sit")
.requires(ServerCommandSource::isExecutedByPlayer)
.requires((commandSource) -> SLSCarpetSettings.canUseSitCommand)
.executes(context -> {
ServerPlayerEntity player = context.getSource().getPlayer();
assert player != null;
if (player.getVehicle() != null) { // 防止重复的坐下行为
return 1;
}
World world = player.getWorld();
ArmorStandEntity armorStandEntity = new ArmorStandEntity(world, player.getX(), player.getY(), player.getZ());
((SitEntity) armorStandEntity).setSitEntity(true);
world.spawnEntity(armorStandEntity);
player.setSneaking(false);
player.startRiding(armorStandEntity);
return 1;
})
);
HatCommand.register(dispatcher);
SitCommand.register(dispatcher);
BotCommand.register(dispatcher);
}
}
@@ -2,6 +2,7 @@ package com.github.zly2006.carpetslsaddition.block;
import com.github.zly2006.carpetslsaddition.SLSCarpetSettings;
import com.mojang.logging.LogUtils;
import io.netty.util.internal.logging.Slf4JLoggerFactory;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.util.math.BlockPos;
@@ -10,13 +11,14 @@ import net.minecraft.world.World;
import net.minecraft.world.block.NeighborUpdater;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
public class RewriteChainRestrictedNeighborUpdater implements NeighborUpdater {
private static final Logger LOGGER = LogUtils.getLogger();
private static final Logger LOGGER = LoggerFactory.getLogger("Minecraft");
private final World world;
private final int maxChainDepth;
private final ArrayDeque<Entry> queue = new ArrayDeque<>();
@@ -49,12 +51,14 @@ public class RewriteChainRestrictedNeighborUpdater implements NeighborUpdater {
}
private void enqueue(BlockPos pos, Entry entry) {
int maxSize = SLSCarpetSettings.maxUpdateQueueSize;
if (maxSize > 0) {
needUpdate = this.pending.size() + 1 <= maxSize;
if (!needUpdate) {
return;
}
else {
needUpdate = true;
int maxSize = SLSCarpetSettings.maxUpdateQueueSize;
if (maxSize > 0) {
needUpdate = this.pending.size() + this.queue.size() + 1 <= maxSize;
}
boolean notEmpty = this.depth > 0;
@@ -92,20 +96,20 @@ public class RewriteChainRestrictedNeighborUpdater implements NeighborUpdater {
Entry entry = this.queue.peek();
assert entry != null;
if (needUpdate) {
while(this.pending.isEmpty()) {
if (needUpdate) {
if (!entry.update(this.world)) {
this.queue.pop();
break;
}
}
}
else {
entry.update(this.world);
throw new OutOfMemoryError("Create by Carpet-SLS-Addition");
}
}
}
}
catch (OutOfMemoryError e) {
if (e.getMessage().endsWith("Carpet-SLS-Addition")) {
LOGGER.error("The update queue exceeded the maximum allowed allocation of heap space set by SLS Addition!", e);
@@ -118,6 +122,7 @@ public class RewriteChainRestrictedNeighborUpdater implements NeighborUpdater {
this.queue.clear();
this.pending.clear();
this.depth = 0;
needUpdate = true;
}
}
@@ -283,7 +283,7 @@ public class BotCommand {
return 0;
}
else {
((PlayerAccess) player).setDisplayName(Text.empty().append(Text.literal("[%s] ".formatted(source.getName())).setStyle(Style.EMPTY.withColor(Formatting.GRAY))).append(Text.literal(playerName).setStyle(Style.EMPTY)));
((PlayerAccess) player).setDisplayName(Text.empty().append(Text.literal("[%s] ".formatted(source.getName())).setStyle(Style.EMPTY.withColor(Formatting.AQUA))).append(Text.literal(playerName).setStyle(Style.EMPTY)));
ServerMain.server.getPlayerManager().broadcast(Text.empty()
.append(Text.literal("假人").setStyle(Style.EMPTY.withColor(Formatting.GREEN)))
.append(Text.literal(playerName).setStyle(Style.EMPTY.withColor(Formatting.GOLD).withBold(true)))
@@ -1,32 +0,0 @@
package com.github.zly2006.carpetslsaddition.mixin.block;
import com.github.zly2006.carpetslsaddition.util.access.PlayerAccess;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.util.List;
@Mixin(Block.class)
public class MixinBlock {
@Inject(method = "getDroppedStacks(Lnet/minecraft/block/BlockState;Lnet/minecraft/server/world/ServerWorld;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/entity/BlockEntity;Lnet/minecraft/entity/Entity;Lnet/minecraft/item/ItemStack;)Ljava/util/List;", at = @At("HEAD"), cancellable = true)
private static void onDrop(BlockState state, ServerWorld world, BlockPos pos, @Nullable BlockEntity blockEntity, @Nullable Entity entity, ItemStack stack, CallbackInfoReturnable<List<ItemStack>> cir) {
if (entity instanceof ServerPlayerEntity player) {
if (((PlayerAccess) player).holdingObsidianPickaxe() && state.getBlock() == Blocks.OBSIDIAN) {
cir.setReturnValue(List.of(new ItemStack(Items.OBSIDIAN)));
}
}
}
}
@@ -24,7 +24,7 @@ public abstract class MixinSettingsManager implements SettingsManagerAccessor {
this.loadConfigurationFromConf();
}
@Inject(method = "setRule", at = @At(value = "INVOKE", target = "Lcarpet/utils/Messenger;m(Lnet/minecraft/server/command/ServerCommandSource;[Ljava/lang/Object;)V", shift = At.Shift.BEFORE))
@Inject(method = "setRule", at = @At(value = "INVOKE", target = "Lcarpet/utils/Messenger;m(Lnet/minecraft/server/command/ServerCommandSource;[Ljava/lang/Object;)V", shift = At.Shift.BEFORE), remap = true)
private void injectSetRule(ServerCommandSource source, CarpetRule<?> rule, String newValue, CallbackInfoReturnable<Integer> cir) {
if ("maxUpdateQueueSize".equals(rule.name())) {
MinecraftServer server = ServerMain.server;
@@ -1,6 +1,6 @@
package com.github.zly2006.carpetslsaddition.mixin.entity;
import com.github.zly2006.carpetslsaddition.PlayerAccess;
import com.github.zly2006.carpetslsaddition.util.access.PlayerAccess;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.EquipmentSlot;
@@ -8,8 +8,11 @@ import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.stat.Stat;
import net.minecraft.text.Text;
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.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@@ -22,26 +25,20 @@ public abstract class MixinPlayerEntity implements PlayerAccess {
@Shadow public abstract void resetStat(Stat<?> stat);
@Shadow public abstract Text getName();
@Unique
private Text displayName = null;
@Override
public boolean holdingObsidianPickaxe() {
ItemStack itemStack = getEquippedStack(EquipmentSlot.MAINHAND);
NbtCompound compound = itemStack.getSubNbt(MOD_ID);
return compound != null && compound.contains(ITEM_NAME) && compound.getString(ITEM_NAME).equals(OBSIDIAN_PICKAXE);
public void setDisplayName(Text name) {
this.displayName = name;
}
@Inject(method = "getBlockBreakingSpeed", at = @At("HEAD"), cancellable = true)
private void onGetBlockBreakingSpeed(BlockState block, CallbackInfoReturnable<Float> cir) {
if (holdingObsidianPickaxe() && block.isOf(Blocks.OBSIDIAN)) {
cir.setReturnValue(750f);// f = speed / 1500
}
}
@Inject(method = "canHarvest", at = @At("HEAD"), cancellable = true)
private void onGetCanMine(BlockState state, CallbackInfoReturnable<Boolean> cir) {
if (holdingObsidianPickaxe() && state.isOf(Blocks.OBSIDIAN)) {
cir.setReturnValue(true);
@Inject(method = "getDisplayName", at = @At("HEAD"), cancellable = true)
public void injectGetDisplayName(CallbackInfoReturnable<Text> cir) {
if (this.displayName != null) {
cir.setReturnValue(displayName);
}
}
}
@@ -17,8 +17,6 @@ import org.spongepowered.asm.mixin.Shadow;
public abstract class MixinServerPlayerEntity extends PlayerEntity {
@Shadow
public ServerPlayNetworkHandler networkHandler;
private int sneakTimes = 0;
private long lastSneakTime = 0;
@@ -1,24 +0,0 @@
package com.github.zly2006.carpetslsaddition.mixin.item;
import com.github.zly2006.carpetslsaddition.util.access.PlayerAccess;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(Item.class)
public class MixinItem {
@Inject(method = "canMine", at = @At("HEAD"), cancellable = true)
private void onCheckMine(BlockState state, World world, BlockPos pos, PlayerEntity miner, CallbackInfoReturnable<Boolean> cir) {
if (((PlayerAccess) miner).holdingObsidianPickaxe() && state.getBlock() == Blocks.OBSIDIAN) {
cir.setReturnValue(true);
}
}
}
@@ -2,6 +2,8 @@ package com.github.zly2006.carpetslsaddition.mixin.player;
import com.github.zly2006.carpetslsaddition.SLSCarpetSettings;
import com.github.zly2006.carpetslsaddition.util.ShulkerBoxItemUtil;
import me.fallenbreath.conditionalmixin.api.annotation.Condition;
import me.fallenbreath.conditionalmixin.api.annotation.Restriction;
import net.minecraft.entity.player.PlayerAbilities;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
@@ -12,6 +14,9 @@ import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
@Restriction(
conflict = @Condition("pca")
)
@Mixin(PlayerInventory.class)
public abstract class MixinPlayerInventory implements Inventory, Nameable {
@Redirect(method = "insertStack(ILnet/minecraft/item/ItemStack;)Z", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;getAbilities()Lnet/minecraft/entity/player/PlayerAbilities;"))
@@ -33,8 +38,7 @@ public abstract class MixinPlayerInventory implements Inventory, Nameable {
}
// 修改潜影盒拾取逻辑,使从地上捡起潜影盒时仍可堆叠
@Redirect(method = "canStackAddMore",
at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;isStackable()Z", ordinal = 0))
@Redirect(method = "canStackAddMore", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;isStackable()Z", ordinal = 0))
private boolean canStackAddMoreIsStackable(ItemStack itemStack) {
return ShulkerBoxItemUtil.isStackable(itemStack);
}
@@ -0,0 +1,23 @@
package com.github.zly2006.carpetslsaddition.mixin.plugin;
import me.fallenbreath.conditionalmixin.api.mixin.RestrictiveMixinConfigPlugin;
import java.util.List;
import java.util.Set;
public class BasicMixinPlugin extends RestrictiveMixinConfigPlugin {
@Override
public String getRefMapperConfig() {
return null;
}
@Override
public void acceptTargets(Set<String> myTargets, Set<String> otherTargets) {
}
@Override
public List<String> getMixins() {
return null;
}
}
@@ -1,12 +1,17 @@
package com.github.zly2006.carpetslsaddition.mixin.screen;
import com.github.zly2006.carpetslsaddition.util.ShulkerBoxItemUtil;
import me.fallenbreath.conditionalmixin.api.annotation.Condition;
import me.fallenbreath.conditionalmixin.api.annotation.Restriction;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.ScreenHandler;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
@Restriction(
conflict = @Condition("pca")
)
@Mixin(ScreenHandler.class)
public class MixinScreenHandler {
@Redirect(method = "insertItem", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;isStackable()Z", ordinal = 0))
@@ -1,12 +1,17 @@
package com.github.zly2006.carpetslsaddition.mixin.screen;
import com.github.zly2006.carpetslsaddition.util.ShulkerBoxItemUtil;
import me.fallenbreath.conditionalmixin.api.annotation.Condition;
import me.fallenbreath.conditionalmixin.api.annotation.Restriction;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.Slot;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
@Restriction(
conflict = @Condition("pca")
)
@Mixin(Slot.class)
public class MixinSlot {
@Redirect(method = "getMaxItemCount(Lnet/minecraft/item/ItemStack;)I", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getMaxCount()I", ordinal = 0))
@@ -1,7 +1,5 @@
package com.github.zly2006.carpetslsaddition.util;
import net.minecraft.world.block.NeighborUpdater;
public interface NeighborUpdaterChanger {
void useRewriteChainNeighborUpdater(boolean use);
}
+1 -2
View File
@@ -3,6 +3,7 @@
"minVersion": "0.8",
"package": "com.github.zly2006.carpetslsaddition.mixin",
"compatibilityLevel": "JAVA_17",
"plugin": "com.github.zly2006.carpetslsaddition.mixin.plugin.BasicMixinPlugin",
"server": [
"server.MixinMinecraftDedicatedServer"
],
@@ -10,7 +11,6 @@
],
"mixins": [
"block.BlockPatternTestTransformInvoker",
"block.MixinBlock",
"block.MixinRedstoneWireBlock",
"carpet.MixinFakePlayer",
"carpet.MixinSettingsManager",
@@ -22,7 +22,6 @@
"entity.MixinServerPlayerEntity",
"entity.MixinVillagerEntity",
"item.MixinDyeItem",
"item.MixinItem",
"item.MixinPotionItem",
"player.AccessorPlayerAbilities",
"player.MixinPlayerInventory",