Skip to content

Commit

Permalink
feat: rough port to 1.20.5
Browse files Browse the repository at this point in the history
  • Loading branch information
CamperSamu committed Apr 30, 2024
1 parent 896d5a5 commit d027af8
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
matrix:
# Use these Java versions
java: [
17, # Current Java LTS & minimum supported by Minecraft
21, # Current Java LTS & minimum supported by Minecraft
]
# and run on both Linux and Windows
os: [ubuntu-20.04]
Expand Down
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
plugins {
id 'fabric-loom' version '1.5-SNAPSHOT'
id 'fabric-loom' version '1.6-SNAPSHOT'
id 'maven-publish'
id 'idea'
}

sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21

archivesBaseName = project.archives_base_name
version = project.mod_version + "+" + project.minecraft_version
Expand Down Expand Up @@ -45,7 +45,7 @@ processResources {

tasks.withType(JavaCompile).configureEach {
// Minecraft 1.18 (1.18-pre2) upwards uses Java 17.
it.options.release = 17
it.options.release = 21
}

java {
Expand Down
14 changes: 7 additions & 7 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Done to increase the memory available to gradle.
org.gradle.jvmargs=-Xmx1G
org.gradle.jvmargs=-Xmx2G

# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_version=1.20.4
yarn_mappings=1.20.4+build.3
loader_version=0.15.3
minecraft_version=1.20.5
yarn_mappings=1.20.5+build.1
loader_version=0.15.10

# Mod Properties
mod_version = 1.6.2
mod_version = 1.7.0
maven_group = com.campersamu
archives_base_name = itemcommander

# Dependencies
fabric_version=0.91.3+1.20.4
placeholder_version=2.3.0+1.20.3
fabric_version=0.97.8+1.20.5
placeholder_version=2.4.0-pre.1+1.20.5
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void onInitialize() {
GiveCommanderCommand.init();
//endregion
LOGGER.info(">item commander loading");
LOGGER.info(">placeholder-api support: " + (PLACEHOLDERS_LOADED ? "enabled" : "disabled"));
LOGGER.info(">placeholder-api support: {}", PLACEHOLDERS_LOADED ? "enabled" : "disabled");
LOGGER.info(">when a stick is more powerful than you");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
Expand Down Expand Up @@ -49,8 +52,8 @@ private static int execute(final @NotNull CommandContext<ServerCommandSource> co
}

try {
final Commander commander = Commander.fromNbt(player.getMainHandStack().getOrCreateNbt()).appendCommand(command);
player.getMainHandStack().getOrCreateNbt()
final Commander commander = Commander.fromNbt(player.getMainHandStack().getOrDefault(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(new NbtCompound())).getNbt()).appendCommand(command);
player.getMainHandStack().getOrDefault(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(new NbtCompound())).getNbt()
.put(COMMANDER_TAG_KEY, commander.toNbt());
} catch (CommanderException e) {
ctxSource.sendError(errorNotCommanderItemText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.command.argument.ItemStackArgument;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
Expand Down Expand Up @@ -98,11 +101,11 @@ private static int execute(final @NotNull CommandContext<ServerCommandSource> co
ctxSource.sendError(errorNoItemInHandText);
return -1;
}
player.getMainHandStack().getOrCreateNbt()
player.getMainHandStack().getOrDefault(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(new NbtCompound())).getNbt()
.put(COMMANDER_TAG_KEY, commander.toNbt());
} else {
final ItemStack stack = stackArg.createStack(1, true);
stack.getOrCreateNbt().put(COMMANDER_TAG_KEY, commander.toNbt());
stack.getOrDefault(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(new NbtCompound())).getNbt().put(COMMANDER_TAG_KEY, commander.toNbt());
player.giveItemStack(stack);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private static int execute(final @NotNull CommandContext<ServerCommandSource> co
players.forEach(player -> player.giveItemStack(stack));
} catch (IOException e) {
ctxSource.sendFeedback(() -> errorText(fileName), true);
LOGGER.error("Failed to load item from file " + fileName, e);
LOGGER.error("Failed to load item from file {}", fileName, e);
return -1;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.campersamu.itemcommander.config;

import com.mojang.datafixers.DataFixerUpper;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtIo;
import net.minecraft.server.MinecraftServer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.stream.Stream;

import static com.campersamu.itemcommander.ItemCommanderInit.LOGGER;
Expand Down Expand Up @@ -64,7 +67,8 @@ static void saveItemToFile(final @NotNull ItemStack stack, @NotNull String fileN
? fileName
: fileName + ".nbt";
try {
NbtIo.write(stack.writeNbt(new NbtCompound()), CONFIG_FOLDER.resolve(fileName));
if (!(FabricLoader.getInstance().getGameInstance() instanceof MinecraftServer server)) return;
NbtIo.write((NbtCompound) stack.encodeAllowEmpty(server.getRegistryManager()), CONFIG_FOLDER.resolve(fileName));
} catch (IOException e) {
LOGGER.error("Failed to save item to file", e);
throw new RuntimeException(e);
Expand All @@ -83,6 +87,8 @@ static ItemStack loadFromFile(@NotNull String fileName) throws IOException {
fileName = (fileName.contains(".nbt"))
? fileName
: fileName + ".nbt";
return ItemStack.fromNbt(NbtIo.read(CONFIG_FOLDER.resolve(fileName)));
if (!(FabricLoader.getInstance().getGameInstance() instanceof MinecraftServer server)) return ItemStack.EMPTY;
// server.getDataFixer().update()
return ItemStack.fromNbtOrEmpty(server.getRegistryManager(), Objects.requireNonNull(NbtIo.read(CONFIG_FOLDER.resolve(fileName))));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.campersamu.itemcommander.exception;

public abstract class CommanderException extends Exception{
public abstract class CommanderException extends Exception {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import static com.campersamu.itemcommander.ItemCommanderInit.LOGGER;

public class CommanderNoCommandException extends CommanderException{
public class CommanderNoCommandException extends CommanderException {
public static final String error = "No command was specified for Commander item!";
public CommanderNoCommandException(){

public CommanderNoCommandException() {
super();
LOGGER.error(error);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.campersamu.itemcommander.exception;

public class CommanderNoTagException extends CommanderException {
public CommanderNoTagException(){
super();
}
public CommanderNoTagException() { super(); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.campersamu.itemcommander.exception.CommanderException;
import com.campersamu.itemcommander.nbt.Commander;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.network.ServerPlayerInteractionManager;
import net.minecraft.util.ActionResult;
Expand All @@ -27,8 +30,8 @@ public class ItemInteractMixin {
)
private void checkInteraction(ServerPlayerEntity player, World world, ItemStack stack, Hand hand, CallbackInfoReturnable<ActionResult> cir) {
try {
if (stack.hasNbt()) {
cir.setReturnValue(Commander.fromNbt(Objects.requireNonNull(stack.getNbt())).executeCommand(player, stack, player.raycast(4, 1.0F, false).getPos()));
if (stack.get(DataComponentTypes.CUSTOM_DATA) != null) {
cir.setReturnValue(Commander.fromNbt(Objects.requireNonNull(stack.getOrDefault(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(new NbtCompound())).getNbt())).executeCommand(player, stack, player.raycast(4, 1.0F, false).getPos()));
}
} catch (CommanderException ignored) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
import net.minecraft.block.BlockWithEntity;
import net.minecraft.block.LecternBlock;
import net.minecraft.block.entity.LecternBlockEntity;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.util.ActionResult;
Expand Down Expand Up @@ -46,12 +49,12 @@ private LecternMixin(Settings settings) {
at = @At("HEAD"),
cancellable = true
)
private void onUseCommand(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit, CallbackInfoReturnable<ActionResult> cir) {
private void onUseCommand(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit, CallbackInfoReturnable<ActionResult> cir) {
if (player instanceof ServerPlayerEntity serverPlayer && state.get(HAS_BOOK))
try {
final LecternBlockEntity lectern = requireNonNullElse((LecternBlockEntity) world.getBlockEntity(pos), new LecternBlockEntity(pos, state));
final ItemStack stack = lectern.getBook();
final Commander commander = Commander.fromNbt(stack.getOrCreateNbt());
final Commander commander = Commander.fromNbt(stack.getOrDefault(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(new NbtCompound())).getNbt());
final ActionResult result = commander.executeCommand(serverPlayer, stack);
if (commander.action() == CommanderAction.CONSUME) {
lectern.setBook(stack.split(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public static String applyPlaceholdersApiIfPresent(final @NotNull ServerCommandS

public static void executeCommandDangerously(@NotNull MinecraftServer server, @NotNull ServerPlayerEntity player, String command) {
synchronized (server.getPlayerManager().getOpList()) {
var op = server.getPlayerManager().isOperator(player.getGameProfile());
final var op = server.getPlayerManager().isOperator(player.getGameProfile());
if (!op) server.getPlayerManager().addToOperators(player.getGameProfile());
server.getCommandManager().executeWithPrefix(player.getCommandSource(), command);
if (!op) server.getPlayerManager().removeFromOperators(player.getGameProfile());
Expand Down

0 comments on commit d027af8

Please sign in to comment.