Hello all.
I have kept DataContainer.toString
in database.
I am trying to recreate it.
Example string:
MemoryDataContainer{map={ItemType=minecraft:sponge, Count=1, UnsafeDamage=0}}
How to create DataContainer from string?
Hello all.
I have kept DataContainer.toString
in database.
I am trying to recreate it.
Example string:
MemoryDataContainer{map={ItemType=minecraft:sponge, Count=1, UnsafeDamage=0}}
How to create DataContainer from string?
I have a small hunch that you completely misunderstood the point of data…
Okay, a better answer:
The toString method is meant for printing out to the console, and as a result, does not do a good job at formating the contents in a recoverable fashion. The point of a data container is that it can be saved using minecraft’s NBT data system. @gabizou can give you the better explanation, but the data API is meant for accessing and storing information on various things in minecraft. If you are planning on writing it to a database or other storage location that isn’t built into minecraft, I suggest just using something like JSON to store it.
Data is stored as a tree, therefore you need a tree structure. Two formats of tree structure are NBT and JSON. a JSON object can be serialized to a string and back again so perhaps you could make a DataTranslator to go from DataContainer <–> JSON <–> String
@simon816, That’s what I thought.
“Sigh”. It will be hard.
Not sure how it’s hard when you have Configurate and a StringWriter
to store to a string, and likewise, reading from a string. Backpack does this already, and loading is even simpler.
@Zidane Error:
You need to give it a Callable<BufferedWriter>
instead of a CharSink
.
@JBYoshi, I want details.
Replace the argument to setSink()
with this:
new Callable<BufferedWriter>() {
@Override
public BufferedWriter call() {
return new BufferedWriter(writer);
}
}
Or, if you want a shorter version:
() -> new BufferedWriter(writer)
(no semicolon at the end)
Super. Happened.
Yep, that looks right.
Topics solved. Thanks to all.
Just leave this code here:
[spoiler]```
public class JsonCmd implements CommandExecutor {
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
Game game = Grave.getInstance().getGame();
if (src instanceof Player) {
try {
//Writer
Player player = (Player) src;
ItemStackSnapshot itemSnapshot = player.getItemInHand().get().createSnapshot();
DataContainer dataContainer = itemSnapshot.toContainer();
Text msg1ContainerName = Texts.of(TextColors.YELLOW, "First container: " + dataContainer.toString());
player.sendMessage(msg1ContainerName);
StringWriter writer = new StringWriter();
HoconConfigurationLoader loader = HoconConfigurationLoader.builder().setSink(new Callable<BufferedWriter>() {
@Override
public BufferedWriter call() {
return new BufferedWriter(writer);
}
}).build();
loader.save(ConfigurateTranslator.instance().translateData(dataContainer));
//Reader
String toString = writer.toString();
StringReader reader = new StringReader(toString);
DataView view = ConfigurateTranslator.instance().translateFrom(HoconConfigurationLoader.builder().setSource(new Callable<BufferedReader>() {
@Override
public BufferedReader call() {
return new BufferedReader(reader);
}
}).build().load());
DataContainer container = view.getContainer();
Text msg2ContainerName = Texts.of(TextColors.YELLOW, "Second container: " + container.toString());
player.sendMessage(msg2ContainerName);
} catch (IOException ex) {
Logger.getLogger(JsonCmd.class.getName()).log(Level.SEVERE, null, ex);
}
} else if (src instanceof ConsoleSource) {
src.sendMessage(Texts.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "Must be an in-game player to use /grave item!"));
} else if (src instanceof CommandBlockSource) {
src.sendMessage(Texts.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "Must be an in-game player to use /grave item!"));
}
return CommandResult.success();
}
}
[/spoiler]