1.17 port of 2.4.0

2.x-1.17
Szum123321 2022-06-23 10:29:29 +02:00
parent 4fa30024cf
commit a10dab5f98
12 changed files with 47 additions and 27 deletions

View File

@ -1,18 +1,18 @@
# Done to increase the memory available to gradle. # Done to increase the memory available to gradle.
org.gradle.jvmargs=-Xmx1G org.gradle.jvmargs=-Xmx1G
minecraft_version=1.19 minecraft_version=1.17.1
yarn_mappings=1.19+build.4 yarn_mappings=1.17.1+build.65
loader_version=0.14.8 loader_version=0.14.8
#Fabric api #Fabric api
fabric_version=0.56.1+1.19 fabric_version=0.46.1+1.17
#Cloth Config #Cloth Config
cloth_version=7.0.72 cloth_version=5.0.37
#ModMenu #ModMenu
modmenu_version=4.0.0 modmenu_version=2.0.4
#Lazy DFU for faster dev start #Lazy DFU for faster dev start
lazydfu_version=v0.1.3 lazydfu_version=v0.1.3

View File

@ -23,7 +23,7 @@ import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import me.shedaniel.autoconfig.AutoConfig; import me.shedaniel.autoconfig.AutoConfig;
import me.shedaniel.autoconfig.serializer.JanksonConfigSerializer; import me.shedaniel.autoconfig.serializer.JanksonConfigSerializer;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
@ -82,7 +82,7 @@ public class TextileBackup implements ModInitializer {
} }
}); });
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register( CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(
LiteralArgumentBuilder.<ServerCommandSource>literal("backup") LiteralArgumentBuilder.<ServerCommandSource>literal("backup")
.requires((ctx) -> { .requires((ctx) -> {
try { try {

View File

@ -20,6 +20,7 @@ package net.szum123321.textile_backup;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.text.MutableText; import net.minecraft.text.MutableText;
import net.minecraft.util.Formatting; import net.minecraft.util.Formatting;
@ -54,11 +55,11 @@ public class TextileLogger {
this.messageFactory = ParameterizedMessageFactory.INSTANCE; this.messageFactory = ParameterizedMessageFactory.INSTANCE;
this.logger = LogManager.getLogger(StackLocatorUtil.getCallerClass(2), messageFactory); this.logger = LogManager.getLogger(StackLocatorUtil.getCallerClass(2), messageFactory);
this.prefix = "[" + prefix + "]" + " "; this.prefix = "[" + prefix + "]" + " ";
this.prefixText = Text.literal(this.prefix).styled(style -> style.withColor(0x5B23DA)); this.prefixText = new LiteralText(this.prefix).styled(style -> style.withColor(0x5B23DA));
} }
public MutableText getPrefixText() { public MutableText getPrefixText() {
return prefixText.copy(); return prefixText.shallowCopy();
} }
public void log(Level level, String msg, Object... data) { public void log(Level level, String msg, Object... data) {
@ -94,14 +95,14 @@ public class TextileLogger {
} }
boolean sendFeedback(Level level, ServerCommandSource source, String msg, Object... args) { boolean sendFeedback(Level level, ServerCommandSource source, String msg, Object... args) {
if(source != null && source.isExecutedByPlayer()) { if(source != null && source.getEntity() instanceof PlayerEntity) {
MutableText text = Text.literal(messageFactory.newMessage(msg, args).getFormattedMessage()); MutableText text = new LiteralText(messageFactory.newMessage(msg, args).getFormattedMessage());
if(level.intLevel() == Level.TRACE.intLevel()) text.formatted(Formatting.GREEN); if(level.intLevel() == Level.TRACE.intLevel()) text.formatted(Formatting.GREEN);
else if(level.intLevel() <= Level.WARN.intLevel()) text.formatted(Formatting.RED); else if(level.intLevel() <= Level.WARN.intLevel()) text.formatted(Formatting.RED);
else text.formatted(Formatting.WHITE); else text.formatted(Formatting.WHITE);
source.sendFeedback(prefixText.copy().append(text), false); source.sendFeedback(prefixText.shallowCopy().append(text), false);
return true; return true;
} else { } else {

View File

@ -19,7 +19,7 @@
package net.szum123321.textile_backup.commands; package net.szum123321.textile_backup.commands;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType; import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import net.minecraft.text.Text; import net.minecraft.text.LiteralText;
import net.minecraft.text.MutableText; import net.minecraft.text.MutableText;
import java.time.format.DateTimeParseException; import java.time.format.DateTimeParseException;
@ -28,7 +28,7 @@ public class CommandExceptions {
public static final DynamicCommandExceptionType DATE_TIME_PARSE_COMMAND_EXCEPTION_TYPE = new DynamicCommandExceptionType(o -> { public static final DynamicCommandExceptionType DATE_TIME_PARSE_COMMAND_EXCEPTION_TYPE = new DynamicCommandExceptionType(o -> {
DateTimeParseException e = (DateTimeParseException)o; DateTimeParseException e = (DateTimeParseException)o;
MutableText message = Text.literal("An exception occurred while trying to parse:\n") MutableText message = new LiteralText("An exception occurred while trying to parse:\n")
.append(e.getParsedString()) .append(e.getParsedString())
.append("\n"); .append("\n");

View File

@ -21,6 +21,7 @@ package net.szum123321.textile_backup.commands.manage;
import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
import net.szum123321.textile_backup.TextileBackup; import net.szum123321.textile_backup.TextileBackup;
@ -35,7 +36,6 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeParseException; import java.time.format.DateTimeParseException;
import java.util.Optional;
import java.util.stream.Stream; import java.util.stream.Stream;
public class DeleteCommand { public class DeleteCommand {
@ -69,10 +69,11 @@ public class DeleteCommand {
Files.delete(file); Files.delete(file);
log.sendInfo(source, "File {} successfully deleted!", file); log.sendInfo(source, "File {} successfully deleted!", file);
if(source.isExecutedByPlayer()) if(source.getEntity() instanceof PlayerEntity)
log.info("Player {} deleted {}.", source.getPlayer().getName(), file); log.info("Player {} deleted {}.", source.getPlayer().getName(), file);
} catch (IOException e) { } catch (IOException e) {
log.sendError(source, "Something went wrong while deleting file!"); log.sendError(source, "Something went wrong while deleting file!");
} catch (CommandSyntaxException ignored) {
} }
} else { } else {
log.sendError(source, "Couldn't delete the file because it's being restored right now."); log.sendError(source, "Couldn't delete the file because it's being restored right now.");

View File

@ -18,11 +18,11 @@
package net.szum123321.textile_backup.core; package net.szum123321.textile_backup.core;
import net.minecraft.network.message.MessageType; import net.minecraft.network.MessageType;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.LiteralText;
import net.minecraft.text.MutableText; import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting; import net.minecraft.util.Formatting;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.szum123321.textile_backup.TextileBackup; import net.szum123321.textile_backup.TextileBackup;
@ -32,7 +32,6 @@ import net.szum123321.textile_backup.config.ConfigPOJO;
import net.szum123321.textile_backup.Statics; import net.szum123321.textile_backup.Statics;
import net.szum123321.textile_backup.mixin.MinecraftServerSessionAccessor; import net.szum123321.textile_backup.mixin.MinecraftServerSessionAccessor;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.file.SimplePathVisitor;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.File; import java.io.File;
@ -40,22 +39,28 @@ import java.io.IOException;
import java.nio.file.FileVisitResult; import java.nio.file.FileVisitResult;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime; import java.nio.file.attribute.FileTime;
import java.time.*; import java.time.*;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Arrays; import java.util.Arrays;
import java.util.Optional; import java.util.Optional;
import java.util.UUID;
public class Utilities { public class Utilities {
private final static ConfigHelper config = ConfigHelper.INSTANCE; private final static ConfigHelper config = ConfigHelper.INSTANCE;
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME); private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
public static void notifyPlayers(@NotNull MinecraftServer server, String msg) { public static void notifyPlayers(@NotNull MinecraftServer server, UUID sender, String msg) {
MutableText message = log.getPrefixText(); MutableText message = log.getPrefixText();
message.append(Text.literal(msg).formatted(Formatting.WHITE)); message.append(new LiteralText(msg).formatted(Formatting.WHITE));
server.getPlayerManager().broadcast(message, MessageType.SYSTEM); server.getPlayerManager().broadcastChatMessage(
message,
MessageType.SYSTEM,
sender
);
} }
public static String getLevelName(MinecraftServer server) { public static String getLevelName(MinecraftServer server) {
@ -65,7 +70,7 @@ public class Utilities {
public static Path getWorldFolder(MinecraftServer server) { public static Path getWorldFolder(MinecraftServer server) {
return ((MinecraftServerSessionAccessor)server) return ((MinecraftServerSessionAccessor)server)
.getSession() .getSession()
.getWorldDirectory(World.OVERWORLD); .getWorldDirectory(World.OVERWORLD).toPath();
} }
public static Path getBackupRootPath(String worldName) { public static Path getBackupRootPath(String worldName) {
@ -83,7 +88,7 @@ public class Utilities {
} }
public static void deleteDirectory(Path path) throws IOException { public static void deleteDirectory(Path path) throws IOException {
Files.walkFileTree(path, new SimplePathVisitor() { Files.walkFileTree(path, new SimpleFileVisitor<>() {
@Override @Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file); Files.delete(file);

View File

@ -41,6 +41,10 @@ public record BackupContext(@NotNull MinecraftServer server,
return save; return save;
} }
public UUID getInitiatorUUID() {
return initiator.equals(ActionInitiator.Player) && commandSource.getEntity() != null ? commandSource.getEntity().getUuid(): Util.NIL_UUID;
}
public static class Builder { public static class Builder {
private MinecraftServer server; private MinecraftServer server;
private ServerCommandSource commandSource; private ServerCommandSource commandSource;

View File

@ -18,6 +18,7 @@
package net.szum123321.textile_backup.core.create; package net.szum123321.textile_backup.core.create;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
import net.szum123321.textile_backup.Statics; import net.szum123321.textile_backup.Statics;
import net.szum123321.textile_backup.TextileBackup; import net.szum123321.textile_backup.TextileBackup;
@ -42,6 +43,7 @@ public class BackupHelper {
public static Runnable create(BackupContext ctx) { public static Runnable create(BackupContext ctx) {
if(config.get().broadcastBackupStart) { if(config.get().broadcastBackupStart) {
Utilities.notifyPlayers(ctx.server(), Utilities.notifyPlayers(ctx.server(),
ctx.getInitiatorUUID(),
"Warning! Server backup will begin shortly. You may experience some lag." "Warning! Server backup will begin shortly. You may experience some lag."
); );
} else { } else {
@ -177,7 +179,7 @@ public class BackupHelper {
Files.delete(f); Files.delete(f);
log.sendInfoAL(ctx, "Deleting: {}", f); log.sendInfoAL(ctx, "Deleting: {}", f);
} catch (IOException e) { } catch (IOException e) {
if(ctx.isExecutedByPlayer()) log.sendError(ctx, "Something went wrong while deleting: {}.", f); if(ctx.getEntity() instanceof PlayerEntity) log.sendError(ctx, "Something went wrong while deleting: {}.", f);
log.error("Something went wrong while deleting: {}.", f, e); log.error("Something went wrong while deleting: {}.", f, e);
return 0; return 0;
} }

View File

@ -113,6 +113,7 @@ public class MakeBackupRunnable implements Runnable {
if(config.get().broadcastBackupDone) { if(config.get().broadcastBackupDone) {
Utilities.notifyPlayers( Utilities.notifyPlayers(
context.server(), context.server(),
context.getInitiatorUUID(),
"Done!" "Done!"
); );
} else { } else {

View File

@ -21,15 +21,21 @@ package net.szum123321.textile_backup.core.restore;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.util.Util;
import net.szum123321.textile_backup.core.ActionInitiator; import net.szum123321.textile_backup.core.ActionInitiator;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.UUID;
public record RestoreContext(RestoreHelper.RestoreableFile restoreableFile, public record RestoreContext(RestoreHelper.RestoreableFile restoreableFile,
MinecraftServer server, MinecraftServer server,
@Nullable String comment, @Nullable String comment,
ActionInitiator initiator, ActionInitiator initiator,
ServerCommandSource commandSource) { ServerCommandSource commandSource) {
public UUID getInitiatorUUID() {
return initiator.equals(ActionInitiator.Player) && commandSource.getEntity() != null ? commandSource.getEntity().getUuid(): Util.NIL_UUID;
}
public static final class Builder { public static final class Builder {
private RestoreHelper.RestoreableFile file; private RestoreHelper.RestoreableFile file;
private MinecraftServer server; private MinecraftServer server;

View File

@ -67,6 +67,7 @@ public class RestoreHelper {
Utilities.notifyPlayers( Utilities.notifyPlayers(
ctx.server(), ctx.server(),
ctx.getInitiatorUUID(),
"Warning! The server is going to shut down in " + config.get().restoreDelay + " seconds!" "Warning! The server is going to shut down in " + config.get().restoreDelay + " seconds!"
); );

View File

@ -40,12 +40,11 @@
"depends": { "depends": {
"fabricloader": ">=0.14.6", "fabricloader": ">=0.14.6",
"fabric": "*", "fabric": "*",
"minecraft": "~1.19", "minecraft": "~1.17",
"cloth-config2": "*", "cloth-config2": "*",
"java": ">=16" "java": ">=16"
}, },
"recommends": { "recommends": {
"modmenu": "*" "modmenu": "*"
}, },