Skip to content

Commit

Permalink
refactor: transform the code to adapt 1.20.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sakurawald committed Jan 3, 2025
1 parent 468ed77 commit 399e087
Show file tree
Hide file tree
Showing 18 changed files with 37 additions and 204 deletions.
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ mod_id=fuji
mod_version=6.2.0

# loader
minecraft_version=1.21.4
minecraft_version=1.20.1
fabric_loader_version=0.16.9
yarn_mappings_version=1.21.4+build.1
yarn_mappings_version=1.20.1+build.1

# mod deps
carpet_core_version=1.21.4-1.4.161+v241203
sgui_version=1.8.1+1.21.4
placeholder_api_version=2.5.1+1.21.3
carpet_core_version=1.20-1.4.112+v230608
sgui_version=1.2.2+1.20
placeholder_api_version=2.1.3+1.20.1
luckperms_api_version=5.4

# common deps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mojang.authlib.GameProfile;
import lombok.experimental.UtilityClass;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.packet.c2s.common.SyncedClientOptions;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.UserCache;
Expand Down Expand Up @@ -41,14 +40,14 @@ public static ServerPlayerEntity loadOfflinePlayer(String playerName) {
throw new IllegalArgumentException("can't find player %s in usercache.json".formatted(playerName));
}

ServerPlayerEntity player = ServerHelper.getPlayerManager().createPlayer(gameProfile.get(), SyncedClientOptions.createDefault());
ServerPlayerEntity player = ServerHelper.getPlayerManager().createPlayer(gameProfile.get());

/*
the default dimension for ServerPlayerEntity instance is minecraft:overworld.
in order to keep original dimension, here we should set dimension for the loaded player entity.
*/
Optional<NbtCompound> playerDataOpt = ServerHelper.getPlayerManager().loadPlayerData(player);
applyPlayerDataNbt(player, playerDataOpt.orElse(null));
NbtCompound playerDataOpt = ServerHelper.getPlayerManager().loadPlayerData(player);
applyPlayerDataNbt(player, playerDataOpt);
return player;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import eu.pb4.placeholders.api.node.LiteralNode;
import eu.pb4.placeholders.api.node.TextNode;
import eu.pb4.placeholders.api.parsers.NodeParser;
import eu.pb4.placeholders.api.parsers.tag.TagRegistry;
import eu.pb4.placeholders.api.parsers.tag.TextTag;
import io.github.sakurawald.core.auxiliary.LogUtil;
import io.github.sakurawald.core.auxiliary.ReflectionUtil;
import io.github.sakurawald.core.config.Configs;
Expand Down Expand Up @@ -62,16 +60,6 @@ public class TextHelper {

static {
writeDefaultLanguageFilesIfAbsent();

TagRegistry.registerDefault(
TextTag.self(
"newline",
"formatting",
true,
(nodes, data, parser) -> new LiteralNode("\n")
)
);

}

private static void writeDefaultLanguageFilesIfAbsent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import io.github.sakurawald.Fuji;
import io.github.sakurawald.core.structure.SpatialBlock;
import lombok.experimental.UtilityClass;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.math.BlockPos;
Expand All @@ -19,10 +17,10 @@ public class UuidHelper {

private static final String FUJI_UUID = Fuji.MOD_ID + "$uuid";

public static @Nullable String getAttachedUuid(@Nullable NbtComponent nbtComponent) {
if (nbtComponent == null) return null;
public static @Nullable String getAttachedUuid(@Nullable NbtCompound nbtCompound) {
if (nbtCompound == null) return null;

NbtCompound root = nbtComponent.copyNbt();
NbtCompound root = nbtCompound.copy();
if (!root.contains(FUJI_UUID)) return null;
return root.getString(FUJI_UUID);
}
Expand All @@ -43,25 +41,25 @@ public static String toUuid(World world, BlockPos blockPos) {
}

public static @NotNull String getOrSetAttachedUuid(ItemStack itemStack) {
NbtComponent nbtComponent = itemStack.get(DataComponentTypes.CUSTOM_DATA);
NbtCompound nbtComponent = itemStack.getNbt();
if (getAttachedUuid(nbtComponent) == null) {
nbtComponent = setGeneratedUuidIfAbsent(nbtComponent);
itemStack.set(DataComponentTypes.CUSTOM_DATA, nbtComponent);
itemStack.setNbt(nbtComponent);
}

//noinspection DataFlowIssue
return getAttachedUuid(nbtComponent);
}

private static @NotNull NbtComponent setGeneratedUuidIfAbsent(@Nullable NbtComponent nbtComponent) {
private static @NotNull NbtCompound setGeneratedUuidIfAbsent(@Nullable NbtCompound nbtComponent) {
/* extract nbt compound */
NbtCompound root = nbtComponent == null ? new NbtCompound() : nbtComponent.copyNbt();
NbtCompound root = nbtComponent == null ? new NbtCompound() : nbtComponent.copy();

/* put uuid if not exists */
if (!root.contains(FUJI_UUID)) {
root.putString(FUJI_UUID, String.valueOf(UUID.randomUUID()));
}

return NbtComponent.of(root);
return root;
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@
import io.github.sakurawald.module.initializer.command_attachment.structure.EntityCommandAttachmentNode;
import io.github.sakurawald.module.initializer.command_attachment.structure.ItemStackCommandAttachmentNode;
import lombok.SneakyThrows;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -117,7 +115,7 @@ public static void triggerAttachmentModel(String uuid, PlayerEntity player, List

/* execute as */
ExecuteAsType executeAsType = e.getExecuteAsType();
ServerCommandSource source = player.getCommandSource((ServerWorld) player.getWorld());
ServerCommandSource source = player.getCommandSource();
switch (executeAsType) {
case CONSOLE -> CommandExecutor.execute(ExtendedCommandSource.asConsole(source), e.getCommand());
case PLAYER ->
Expand Down Expand Up @@ -267,7 +265,7 @@ private static void doDetachAttachment(ServerPlayerEntity player, String uuid) {
private static int queryItem(@CommandSource ServerPlayerEntity player) {
ItemStack mainHandStack = player.getMainHandStack();
checkItemStackInHand(player, mainHandStack);
String uuid = UuidHelper.getAttachedUuid(mainHandStack.get(DataComponentTypes.CUSTOM_DATA));
String uuid = UuidHelper.getAttachedUuid(mainHandStack.getNbt());

doQueryAttachment(player, uuid);
return CommandHelper.Return.SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import io.github.sakurawald.module.initializer.chat.history.ChatHistoryInitializer;
import net.minecraft.network.ClientConnection;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.network.ConnectedClientData;
import net.minecraft.server.network.ServerPlayerEntity;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -15,8 +13,8 @@
public class PlayerManagerMixin {

@Inject(at = @At(value = "TAIL"), method = "onPlayerConnect")
private void sendChatHistoryToNewJoinedPlayer(ClientConnection connection, @NotNull ServerPlayerEntity serverPlayer, ConnectedClientData commonListenerCookie, CallbackInfo ci) {
ChatHistoryInitializer.getChatHistory().forEach(serverPlayer::sendMessage);
private void sendChatHistoryToNewJoinedPlayer(ClientConnection clientConnection, ServerPlayerEntity serverPlayerEntity, CallbackInfo ci) {
ChatHistoryInitializer.getChatHistory().forEach(serverPlayerEntity::sendMessage);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.github.sakurawald.core.auxiliary.minecraft.UuidHelper;
import io.github.sakurawald.module.initializer.command_attachment.CommandAttachmentInitializer;
import io.github.sakurawald.module.initializer.command_attachment.command.argument.wrapper.InteractType;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Hand;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -23,7 +22,7 @@ public abstract class ServerPlayNetworkHandlerMixin {
@Inject(method = "swingHand", at = @At("HEAD"))
void onPlayerLeftClick(Hand hand, CallbackInfo ci) {
if (hand.equals(Hand.MAIN_HAND)) {
String uuid = UuidHelper.getAttachedUuid(player.getMainHandStack().get(DataComponentTypes.CUSTOM_DATA));
String uuid = UuidHelper.getAttachedUuid(player.getMainHandStack().getNbt());
if (uuid == null) return;

ServerPlayerEntity player = (ServerPlayerEntity) (Object) this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.github.sakurawald.core.auxiliary.minecraft.UuidHelper;
import io.github.sakurawald.module.initializer.command_attachment.CommandAttachmentInitializer;
import io.github.sakurawald.module.initializer.command_attachment.command.argument.wrapper.InteractType;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.network.ServerPlayerInteractionManager;
Expand Down Expand Up @@ -32,13 +31,13 @@ public class ServerPlayerInteractionManagerMixin {

@Inject(method = "interactItem", at = @At("HEAD"))
void onPlayerRightClick(ServerPlayerEntity serverPlayerEntity, World world, @NotNull ItemStack itemStack, Hand hand, @NotNull CallbackInfoReturnable<ActionResult> cir) {
String uuid = UuidHelper.getAttachedUuid(itemStack.get(DataComponentTypes.CUSTOM_DATA));
String uuid = UuidHelper.getAttachedUuid(itemStack.getNbt());
if (uuid == null) return;

CommandAttachmentInitializer.triggerAttachmentModel(uuid, player, List.of(InteractType.RIGHT, InteractType.BOTH));
}

@Inject(method = "onBlockBreakingAction", at = @At("HEAD"))
@Inject(method = "method_41250", at = @At("HEAD"))
void onPlayerLeftClickBlock(BlockPos blockPos, boolean bl, int i, String string, CallbackInfo ci) {
if (string.equals("actual start of destroying")) {
String uuid = UuidHelper.getAttachedUuid(player.getServerWorld(), blockPos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
import io.github.sakurawald.core.command.executor.CommandExecutor;
import io.github.sakurawald.core.command.structure.ExtendedCommandSource;
import io.github.sakurawald.module.initializer.command_event.CommandEventInitializer;
import net.minecraft.entity.Entity;
import net.minecraft.network.ClientConnection;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.network.ConnectedClientData;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.stat.Stats;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -20,7 +17,7 @@
public class PlayerListManagerMixin {

@Inject(method = "onPlayerConnect", at = @At("TAIL"))
void onPlayerJoined(ClientConnection clientConnection, @NotNull ServerPlayerEntity player, ConnectedClientData connectedClientData, CallbackInfo ci) {
void onPlayerJoined(ClientConnection clientConnection, ServerPlayerEntity player, CallbackInfo ci) {
CommandExecutor.execute(ExtendedCommandSource.asConsole(player.getCommandSource()), CommandEventInitializer.config.model().event.on_player_joined.command_list);

if (player.getStatHandler().getStat(Stats.CUSTOM.getOrCreateStat(Stats.LEAVE_GAME)) < 1) {
Expand All @@ -29,7 +26,7 @@ void onPlayerJoined(ClientConnection clientConnection, @NotNull ServerPlayerEnti
}

@Inject(method = "respawnPlayer", at = @At("TAIL"))
private void afterRespawn(ServerPlayerEntity oldPlayer, boolean alive, Entity.RemovalReason removalReason, CallbackInfoReturnable<ServerPlayerEntity> cir) {
private void afterRespawn(ServerPlayerEntity serverPlayerEntity, boolean bl, CallbackInfoReturnable<ServerPlayerEntity> cir) {
ServerPlayerEntity newPlayer = cir.getReturnValue();
CommandExecutor.execute(ExtendedCommandSource.asConsole(newPlayer.getCommandSource()), CommandEventInitializer.config.model().event.after_player_respawn.command_list);
}
Expand Down
Loading

0 comments on commit 399e087

Please sign in to comment.