Skip to content

Commit

Permalink
v2.0: Add ItemStack & /give + SNBT export
Browse files Browse the repository at this point in the history
  • Loading branch information
0KepOnline committed May 26, 2024
1 parent 3388f64 commit 8a51e26
Show file tree
Hide file tree
Showing 13 changed files with 726 additions and 208 deletions.
25 changes: 10 additions & 15 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,9 @@ apply plugin: 'net.minecraftforge.gradle'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'

def mcversion = '1.12.2'
def modversion = '1.2'
def id = 'nbtexporter'
def author = 'scenarioplanet'

group = 'net.scenariopla'
archivesBaseName = id
version = "${mcversion}-${modversion}"
group = "${mod_group_id}"
archivesBaseName = mod_id
version = "${minecraft_version}-${mod_version}"

sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8'

Expand Down Expand Up @@ -87,12 +82,12 @@ dependencies {

processResources
{
inputs.property "version", project.version
inputs.property "mcversion", mcversion
inputs.property "mod_version", project.version
inputs.property "minecraft_version", minecraft_version

from(sourceSets.main.resources.srcDirs) {
include 'mcmod.info'
expand 'version':project.version, 'mcversion':mcversion
expand 'mod_version':project.version, 'minecraft_version':minecraft_version
}

from(sourceSets.main.resources.srcDirs) {
Expand All @@ -103,12 +98,12 @@ processResources
jar {
manifest {
attributes([
"Specification-Title": id,
"Specification-Vendor": author,
"Specification-Title": mod_id,
"Specification-Vendor": mod_author,
"Specification-Version": '1', // We are version 1 of ourselves
"Implementation-Title": id,
"Implementation-Title": mod_id,
"Implementation-Version": project.version,
"Implementation-Vendor": author,
"Implementation-Vendor": mod_author,
"Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")
])
}
Expand Down
15 changes: 14 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
# Sets default memory used for gradle commands. Can be overridden by user or command line properties.
# This is required to provide enough memory for the Minecraft decompilation process.
org.gradle.jvmargs=-Xmx3G
org.gradle.daemon=false
org.gradle.daemon=false


## Environment Properties

minecraft_version=1.12.2


## Mod Properties

mod_id=nbtexporter
mod_version=2.0
mod_group_id=net.scenariopla.nbtexporter
mod_author=scenarioplanet
69 changes: 60 additions & 9 deletions src/main/java/net/scenariopla/nbtexporter/NBTExporter.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package net.scenariopla.nbtexporter;

import java.io.File;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.regex.Pattern;

import net.minecraft.client.Minecraft;
import net.minecraft.command.ICommand;
import net.minecraftforge.client.ClientCommandHandler;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod;
Expand All @@ -13,37 +15,86 @@
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import net.scenariopla.nbtexporter.command.CommandExportItemStack;
import net.scenariopla.nbtexporter.command.CommandExportItemStackGive;
import net.scenariopla.nbtexporter.command.CommandExportItemStackNBT;
import net.scenariopla.nbtexporter.command.CommandExportStringifiedItemStack;
import net.scenariopla.nbtexporter.command.CommandExportStringifiedItemStackNBT;
import net.scenariopla.nbtexporter.init.DirectoryInit;

@Mod(modid = NBTExporter.Reference.MODID,
@Mod(modid = NBTExporter.Reference.MOD_ID,
name = NBTExporter.Reference.NAME,
version = NBTExporter.Reference.VERSION)
public class NBTExporter {
public static 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 CommandExportItemStack(),
new CommandExportItemStackNBT(),
new CommandExportStringifiedItemStack(),
new CommandExportStringifiedItemStackNBT(),
new CommandExportItemStackGive()
};

public static final Minecraft MINECRAFT = Minecraft.getMinecraft();
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[] modFiles = null;
public static File[] modDirFiles;
public static boolean simplifiedFilenameValidation;

public static void refreshModDir() {
modDirFiles = DirectoryInit.initModDirectory();
}

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"));
}

@EventHandler
@SideOnly(Side.CLIENT)
public void init(FMLInitializationEvent event) {
MinecraftForge.EVENT_BUS.register(this);
ClientCommandHandler.instance.registerCommand(new CommandExportItemStackNBT());
for (ICommand clientCommand : Reference.MOD_CLIENT_COMMANDS) {
ClientCommandHandler.instance.registerCommand(clientCommand);
}

DirectoryInit.modDir = new File(Reference.MINECRAFT.mcDataDir,
Reference.MODDIR);
modFiles = DirectoryInit.initModDirectory();
Reference.MOD_DIRECTORY);
refreshModDir();
checkFilenameValidation();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package net.scenariopla.nbtexporter.command;

import java.io.File;

import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.text.TextComponentTranslation;

import net.scenariopla.nbtexporter.NBTExporter;
import net.scenariopla.nbtexporter.init.DirectoryInit;
import net.scenariopla.nbtexporter.util.FileSystemNamesakes;

public class CommandExportItemStack extends CommandNBTExporter {
private static final String FILE_EXTENSION = ".stack.nbt";

@Override
public String getName() {
return "exportstack";
}

@Override
public String getUsage(ICommandSender sender) {
return "commands.nbtexporter.exportstack.usage";
}

private enum ExportItemStackExceptions {
SYNTAX_ERROR_USAGE
}

private static CommandException exception(ExportItemStackExceptions exception) {
return exception(exception, new Object[0]);
}

private static CommandException exception(ExportItemStackExceptions exception,
Object[] args) {
switch (exception) {
case SYNTAX_ERROR_USAGE:
return new WrongUsageException("commands.nbtexporter.exportstack.usage", args);
}
return null;
}

@Override
public void execute(MinecraftServer server,
ICommandSender sender,
String[] args)
throws CommandException {
if (sender instanceof EntityPlayer) {
NBTExporter.refreshModDir();

final EntityPlayer player = (EntityPlayer) sender;
final ItemStack heldItem = player.getHeldItemMainhand();
if (heldItem.isEmpty()) {
throw exception(GlobalExceptions.HELD_ITEM_ERROR_EMPTY);
}

final NBTTagCompound nbtTagCompound = heldItem.writeToNBT(new NBTTagCompound());

String filename = getFilename(args,
(WrongUsageException) exception(ExportItemStackExceptions.SYNTAX_ERROR_USAGE));

filename = FileSystemNamesakes.addIndex(filename,
FILE_EXTENSION,
NBTExporter.modDirFiles);

File outputFile = new File(DirectoryInit.modDir, filename);
outputFile = writeToFile(outputFile, nbtTagCompound);

player.sendMessage(new TextComponentTranslation("commands.nbtexporter.exportstack.success",
heldItem.getTextComponent(),
buildFileComponent(outputFile)));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package net.scenariopla.nbtexporter.command;

import java.io.File;
import java.util.ArrayList;

import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraftforge.fml.common.registry.ForgeRegistries;

import net.scenariopla.nbtexporter.NBTExporter;
import net.scenariopla.nbtexporter.init.DirectoryInit;
import net.scenariopla.nbtexporter.util.FileSystemNamesakes;

public class CommandExportItemStackGive extends CommandNBTExporter {
private static final String FILE_EXTENSION = ".txt";

@Override
public String getName() {
return "exportgive";
}

@Override
public String getUsage(ICommandSender sender) {
return "commands.nbtexporter.exportgive.usage";
}

private enum ExportStringifiedItemStackNBTExceptions {
SYNTAX_ERROR_USAGE
}

private static CommandException exception(ExportStringifiedItemStackNBTExceptions exception) {
return exception(exception, new Object[0]);
}

private static CommandException exception(ExportStringifiedItemStackNBTExceptions exception,
Object[] args) {
switch (exception) {
case SYNTAX_ERROR_USAGE:
return new WrongUsageException("commands.nbtexporter.exportgive.usage", args);
default:
return null;
}
}


@Override
public void execute(MinecraftServer server,
ICommandSender sender,
String[] args)
throws CommandException {
if (sender instanceof EntityPlayer) {
NBTExporter.refreshModDir();

final EntityPlayer player = (EntityPlayer) sender;
final ItemStack heldItem = player.getHeldItemMainhand();
if (heldItem.isEmpty()) {
throw exception(GlobalExceptions.HELD_ITEM_ERROR_EMPTY);
}

ArrayList<String> giveCommand = new ArrayList<String>();
giveCommand.add("/give");
giveCommand.add(player.getDisplayName().getUnformattedText());
giveCommand.add(ForgeRegistries.ITEMS.getKey(heldItem.getItem()).toString());
final int heldItemCount = heldItem.getCount();
final int heldItemDamage = heldItem.getItemDamage();
final NBTTagCompound nbtTagCompound = heldItem.getTagCompound();
if (nbtTagCompound != null) {
giveCommand.add(Integer.toString(heldItemCount));
giveCommand.add(Integer.toString(heldItemDamage));
giveCommand.add(nbtTagCompound.toString());
} else {
if (heldItemDamage != 0 || heldItemCount != 1) {
giveCommand.add(Integer.toString(heldItemCount));
if (heldItemDamage != 0) {
giveCommand.add(Integer.toString(heldItemDamage));
}
}
}

String filename = getFilename(args,
(WrongUsageException) exception(ExportStringifiedItemStackNBTExceptions.SYNTAX_ERROR_USAGE));

filename = FileSystemNamesakes.addIndex(filename,
FILE_EXTENSION,
NBTExporter.modDirFiles);

File outputFile = new File(DirectoryInit.modDir, filename);
outputFile = writeToFile(outputFile, String.join(" ", giveCommand).getBytes());

player.sendMessage(new TextComponentTranslation("commands.nbtexporter.exportgive.success",
heldItem.getTextComponent(),
buildFileComponent(outputFile)));
}
}
}
Loading

0 comments on commit 8a51e26

Please sign in to comment.