-
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
3388f64
commit 8a51e26
Showing
13 changed files
with
726 additions
and
208 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
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 |
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
79 changes: 79 additions & 0 deletions
79
src/main/java/net/scenariopla/nbtexporter/command/CommandExportItemStack.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,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))); | ||
} | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
src/main/java/net/scenariopla/nbtexporter/command/CommandExportItemStackGive.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,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))); | ||
} | ||
} | ||
} |
Oops, something went wrong.