-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v2.0: Add ItemStack & /give + SNBT export
- Loading branch information
1 parent
dfc4450
commit b54c191
Showing
16 changed files
with
804 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 60 additions & 8 deletions
68
src/main/java/net/scenariopla/nbtexporter/NBTExporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,92 @@ | ||
package net.scenariopla.nbtexporter; | ||
|
||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.text.SimpleDateFormat; | ||
import java.util.regex.Pattern; | ||
|
||
import net.fabricmc.api.ClientModInitializer; | ||
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; | ||
import net.fabricmc.loader.api.FabricLoader; | ||
|
||
import net.scenariopla.nbtexporter.command.ExportItemStackCommand; | ||
import net.scenariopla.nbtexporter.command.ExportItemStackGiveCommand; | ||
import net.scenariopla.nbtexporter.command.ExportItemStackNBTCommand; | ||
import net.scenariopla.nbtexporter.command.ExportStringifiedItemStackCommand; | ||
import net.scenariopla.nbtexporter.command.ExportStringifiedItemStackNBTCommand; | ||
import net.scenariopla.nbtexporter.command.ICommand; | ||
import net.scenariopla.nbtexporter.init.DirectoryInit; | ||
|
||
public class NBTExporter implements ClientModInitializer { | ||
public class Reference { | ||
public static final String MODID = "nbtexporter"; | ||
public static final String MOD_ID = "nbtexporter"; | ||
public static final String NAME = "NBT Exporter"; | ||
public static final String VERSION = "1.2"; | ||
public static final String VERSION = "2.0"; | ||
|
||
public static final String MODDIR = MODID; | ||
public static final String MOD_DIRECTORY = MOD_ID; | ||
|
||
public static final ICommand[] MOD_CLIENT_COMMANDS = { | ||
new ExportItemStackCommand(), | ||
new ExportItemStackNBTCommand(), | ||
new ExportStringifiedItemStackCommand(), | ||
new ExportStringifiedItemStackNBTCommand(), | ||
new ExportItemStackGiveCommand() | ||
}; | ||
|
||
public static final FabricLoader MINECRAFT = FabricLoader.getInstance(); | ||
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss"); | ||
|
||
public static final String FILE_NAME_CHAR_REPLACE = "<>:\"/\\|?*\r\n\t\b\f"; | ||
public static final String FILE_NAME_CHAR_REPLACE = "<>:\"/\\\\|?*\r\n\t\b\f"; | ||
public static final Pattern FILE_NAME_CHAR_REPLACE_PATTERN = Pattern.compile("[" + FILE_NAME_CHAR_REPLACE + "]"); | ||
public static final char FILE_NAME_CHAR_REPLACE_NEW = '_'; | ||
} | ||
|
||
public static File[] modDirFiles; | ||
public static boolean simplifiedFilenameValidation; | ||
|
||
public static void refreshModDir() { | ||
modDirFiles = DirectoryInit.initModDirectory(); | ||
} | ||
|
||
public static File[] modFiles = null; | ||
private static void checkFilenameValidation() { | ||
String fileStoreType; | ||
String osName; | ||
try { | ||
fileStoreType = Files.getFileStore(DirectoryInit.modDir.toPath()).type().toLowerCase(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
fileStoreType = ""; | ||
} | ||
try { | ||
osName = System.getProperty("os.name").toLowerCase(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
osName = ""; | ||
} | ||
|
||
simplifiedFilenameValidation = !fileStoreType.equals("ntfs") && | ||
!osName.contains("win") && | ||
!osName.contains("mac") && | ||
(fileStoreType.startsWith("ext") || | ||
fileStoreType.startsWith("ufs") || | ||
fileStoreType.equals("btrfs") || | ||
fileStoreType.equals("xfs") || | ||
fileStoreType.equals("zfs") || | ||
fileStoreType.equals("jfs") || | ||
fileStoreType.equals("qfs")); | ||
} | ||
|
||
@Override | ||
public void onInitializeClient() { | ||
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> { | ||
ExportItemStackNBTCommand.register(dispatcher); | ||
for (ICommand clientCommand : Reference.MOD_CLIENT_COMMANDS) { | ||
clientCommand.register(dispatcher); | ||
} | ||
}); | ||
|
||
DirectoryInit.modDir = new File(Reference.MINECRAFT.getGameDir().toFile(), Reference.MODDIR); | ||
modFiles = DirectoryInit.initModDirectory(); | ||
DirectoryInit.modDir = new File(Reference.MINECRAFT.getGameDir().toFile(), | ||
Reference.MOD_DIRECTORY); | ||
refreshModDir(); | ||
checkFilenameValidation(); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
src/main/java/net/scenariopla/nbtexporter/command/ExportItemStackCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package net.scenariopla.nbtexporter.command; | ||
|
||
import java.io.File; | ||
|
||
import com.mojang.brigadier.CommandDispatcher; | ||
import com.mojang.brigadier.arguments.StringArgumentType; | ||
import com.mojang.brigadier.context.CommandContext; | ||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
|
||
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; | ||
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument; | ||
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; | ||
|
||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.item.ItemStack; | ||
|
||
import net.scenariopla.nbtexporter.NBTExporter; | ||
import net.scenariopla.nbtexporter.init.DirectoryInit; | ||
import net.scenariopla.nbtexporter.util.FileSystemNamesakes; | ||
import static net.scenariopla.nbtexporter.command.NBTExporterCommandExceptions.ExceptionCollection; | ||
import static net.scenariopla.nbtexporter.command.NBTExporterCommandExceptions.exception; | ||
|
||
public class ExportItemStackCommand extends NBTExporterCommand { | ||
private static final String FILE_EXTENSION = ".stack.nbt"; | ||
|
||
private enum ExportItemStackExceptions implements ExceptionCollection { | ||
SYNTAX_ERROR_USAGE("commands.nbtexporter.global.usage") { | ||
@Override | ||
public Object[] getArguments(Object[] args) { | ||
return new Object[] {Component.translatable("commands.nbtexporter.exportstack.usage", args)}; | ||
} | ||
}; | ||
|
||
|
||
private final String messageKey; | ||
ExportItemStackExceptions(String messageKey) { | ||
this.messageKey = messageKey; | ||
} | ||
|
||
@Override | ||
public String getMessageKey() { | ||
return messageKey; | ||
} | ||
} | ||
|
||
public void register(CommandDispatcher<FabricClientCommandSource> dispatcher) { | ||
dispatcher.register(literal("exportstack") | ||
.executes(context -> { | ||
return execute(context, getFilename()); | ||
}) | ||
.then(argument("filename", StringArgumentType.greedyString()) | ||
.suggests(globalSuggestion) | ||
.requires(globalRequirement) | ||
.executes(context -> { | ||
return execute(context, getFilename(context, exception(ExportItemStackExceptions.SYNTAX_ERROR_USAGE))); | ||
}))); | ||
} | ||
|
||
private static int execute(CommandContext<FabricClientCommandSource> context, | ||
String filename) | ||
throws CommandSyntaxException { | ||
final FabricClientCommandSource sourceStack = context.getSource(); | ||
NBTExporter.refreshModDir(); | ||
|
||
final Player player = (Player) sourceStack.getEntity(); | ||
final ItemStack heldItem = player.getMainHandItem(); | ||
if (heldItem.isEmpty()) { | ||
throw exception(GlobalExceptions.HELD_ITEM_ERROR_EMPTY).create(); | ||
} | ||
|
||
final CompoundTag nbtTagCompound = heldItem.save(new CompoundTag()); | ||
|
||
filename = FileSystemNamesakes.addIndex(filename, | ||
FILE_EXTENSION, | ||
NBTExporter.modDirFiles); | ||
|
||
File outputFile = new File(DirectoryInit.modDir, filename); | ||
outputFile = writeToFile(outputFile, nbtTagCompound); | ||
|
||
final Component successMessage = Component.translatable("commands.nbtexporter.exportstack.success", | ||
heldItem.getDisplayName(), | ||
buildFileComponent(outputFile)); | ||
sourceStack.sendFeedback(successMessage); | ||
return 1; | ||
} | ||
} |
Oops, something went wrong.