Added new debug command "/shopkeepers yaml".

This prints Bukkit's yaml serialization and the item data representation
used in the config for the currently held item.
This commit is contained in:
blablubbabc 2019-08-19 06:39:55 +02:00
parent 48f0a06dd4
commit 1b6c4146de
5 changed files with 160 additions and 60 deletions

View File

@ -57,6 +57,7 @@ Internal:
* Internal: Added ItemData tests. This requires CraftBukkit as new test dependency due to relying on item serialization.
Debugging:
* Debugging: Added new debug command "/shopkeepers yaml", which prints Bukkit's yaml serialization and the item data representation used in the config for the currently held item.
* Debugging: Added entity, invalid entity and dead entity count to shopkeeper's "check" command. Also restructured the output slightly to make it more compact and clearer.
* Debugging: Small changes and additions to some debug messages, especially related to shopkeeper interactions and shopkeeper spawning.
* Debugging: Added setting 'debug-options', which can be used to enable additional debugging tools.

View File

@ -0,0 +1,85 @@
package com.nisovin.shopkeepers.commands.shopkeepers;
import java.io.PrintStream;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import com.nisovin.shopkeepers.api.ShopkeepersPlugin;
import com.nisovin.shopkeepers.commands.lib.CommandArgs;
import com.nisovin.shopkeepers.commands.lib.CommandContext;
import com.nisovin.shopkeepers.commands.lib.CommandException;
import com.nisovin.shopkeepers.commands.lib.CommandInput;
import com.nisovin.shopkeepers.commands.lib.PlayerCommand;
import com.nisovin.shopkeepers.util.ConfigUtils;
import com.nisovin.shopkeepers.util.ItemData;
import com.nisovin.shopkeepers.util.ItemUtils;
class CommandYaml extends PlayerCommand {
private static final int MAX_OUTPUT_LENGTH = 15;
CommandYaml() {
super("yaml");
// set permission:
this.setPermission(ShopkeepersPlugin.DEBUG_PERMISSION);
// set description:
this.setDescription("Prints the YAML representation of the held item.");
// hidden debugging command:
this.setHiddenInParentHelp(true);
}
@Override
protected void execute(CommandInput input, CommandContext context, CommandArgs args) throws CommandException {
assert (input.getSender() instanceof Player);
Player player = (Player) input.getSender();
ItemStack itemInHand = player.getInventory().getItemInMainHand();
if (ItemUtils.isEmpty(itemInHand)) {
player.sendMessage(ChatColor.GRAY + "No item in hand!");
return;
}
// serialized ItemStack and ItemData:
String[] serializedItemStack = ConfigUtils.getYAMLOutput(itemInHand);
String[] serializedItemData = ConfigUtils.getYAMLOutput(new ItemData(itemInHand).serialize());
// print to player:
player.sendMessage(ChatColor.YELLOW + "Serialized ItemStack:");
if (serializedItemStack.length > MAX_OUTPUT_LENGTH) {
player.sendMessage(ChatColor.GRAY + "The output is too large for the chat.");
} else {
for (String line : serializedItemStack) {
player.sendMessage(line);
}
}
player.sendMessage(ChatColor.YELLOW + "Config item data:");
if (serializedItemData.length > MAX_OUTPUT_LENGTH) {
player.sendMessage(ChatColor.GRAY + "The output is too large for the chat.");
} else {
for (String line : serializedItemData) {
player.sendMessage(line);
}
}
player.sendMessage(ChatColor.GRAY + "Note: The output gets also logged to the console for easier copying.");
// print to console:
PrintStream stream = System.out;
synchronized (stream) { // assumes the implementation synchronizes on itself
stream.println("Serialized ItemStack:");
for (String line : serializedItemStack) {
stream.println(line);
}
stream.println("Config item data:");
for (String line : serializedItemData) {
stream.println(line);
}
}
}
}

View File

@ -80,6 +80,7 @@ public class ShopkeepersCommand extends BaseCommand {
// hidden debugging commands:
childCommands.register(new CommandCheck(plugin));
childCommands.register(new CommandCheckItem());
childCommands.register(new CommandYaml());
childCommands.register(new CommandDebugCreateShops(plugin));
}

View File

@ -5,6 +5,8 @@ import java.util.Map.Entry;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
public class ConfigUtils {
@ -37,6 +39,10 @@ public class ConfigUtils {
}
}
public static String yamlLineBreak() {
return "\n"; // YAML used unix line breaks by default
}
public static void convertSectionsToMaps(Map<String, Object> sectionMap) {
for (Entry<String, Object> entry : sectionMap.entrySet()) {
Object value = entry.getValue();
@ -49,6 +55,17 @@ public class ConfigUtils {
}
}
public static String[] getYAMLOutput(ConfigurationSerializable serializable) {
return getYAMLOutput(serializable.serialize());
}
public static String[] getYAMLOutput(Object serializedObject) {
YamlConfiguration config = new YamlConfiguration();
config.set("yaml-output", serializedObject);
String configOutput = config.saveToString();
return configOutput.split(yamlLineBreak());
}
private ConfigUtils() {
}
}

View File

@ -45,10 +45,6 @@ public class ItemDataTest extends AbstractTestBase {
Assert.assertEquals(originalItemData, deserialized);
}
private static String yamlLineBreak() {
return "\n"; // YAML used unix line breaks by default
}
// COMPACT
private static ItemStack createItemStackSimple() {
@ -72,7 +68,7 @@ public class ItemDataTest extends AbstractTestBase {
YamlConfiguration config = new YamlConfiguration();
config.set("item", serialized);
String yamlString = config.saveToString();
Assert.assertEquals("item: DIAMOND_SWORD" + yamlLineBreak(), yamlString);
Assert.assertEquals("item: DIAMOND_SWORD" + ConfigUtils.yamlLineBreak(), yamlString);
}
@Test
@ -108,9 +104,9 @@ public class ItemDataTest extends AbstractTestBase {
YamlConfiguration config = new YamlConfiguration();
config.set("item", serialized);
String yamlString = config.saveToString();
Assert.assertEquals("item:" + yamlLineBreak() +
" type: DIAMOND_SWORD" + yamlLineBreak() +
" display-name: '&cCustom Name'" + yamlLineBreak(), yamlString);
Assert.assertEquals("item:" + ConfigUtils.yamlLineBreak() +
" type: DIAMOND_SWORD" + ConfigUtils.yamlLineBreak() +
" display-name: '&cCustom Name'" + ConfigUtils.yamlLineBreak(), yamlString);
}
@Test
@ -170,46 +166,46 @@ public class ItemDataTest extends AbstractTestBase {
YamlConfiguration config = new YamlConfiguration();
config.set("item", serialized);
String yamlString = config.saveToString();
Assert.assertEquals("item:" + yamlLineBreak() +
" type: DIAMOND_SWORD" + yamlLineBreak() +
" display-name: '&cCustom Name'" + yamlLineBreak() +
" loc-name: loc name" + yamlLineBreak() +
" lore:" + yamlLineBreak() +
" - '&alore1'" + yamlLineBreak() +
" - lore2" + yamlLineBreak() +
" custom-model-data: 1" + yamlLineBreak() +
" enchants:" + yamlLineBreak() +
" DURABILITY: 1" + yamlLineBreak() +
" DAMAGE_ALL: 2" + yamlLineBreak() +
" attribute-modifiers:" + yamlLineBreak() +
" GENERIC_ATTACK_SPEED:" + yamlLineBreak() +
" - ==: org.bukkit.attribute.AttributeModifier" + yamlLineBreak() +
" amount: 2.0" + yamlLineBreak() +
" name: attack speed bonus" + yamlLineBreak() +
" slot: HAND" + yamlLineBreak() +
" uuid: 00000000-0000-0001-0000-000000000001" + yamlLineBreak() +
" operation: 0" + yamlLineBreak() +
" - ==: org.bukkit.attribute.AttributeModifier" + yamlLineBreak() +
" amount: 0.5" + yamlLineBreak() +
" name: attack speed bonus 2" + yamlLineBreak() +
" slot: OFF_HAND" + yamlLineBreak() +
" uuid: 00000000-0000-0002-0000-000000000002" + yamlLineBreak() +
" operation: 2" + yamlLineBreak() +
" GENERIC_MAX_HEALTH:" + yamlLineBreak() +
" - ==: org.bukkit.attribute.AttributeModifier" + yamlLineBreak() +
" amount: 2.0" + yamlLineBreak() +
" name: attack speed bonus" + yamlLineBreak() +
" slot: HAND" + yamlLineBreak() +
" uuid: 00000000-0000-0003-0000-000000000003" + yamlLineBreak() +
" operation: 0" + yamlLineBreak() +
" ItemFlags:" + yamlLineBreak() +
" - HIDE_ENCHANTS" + yamlLineBreak() +
" Unbreakable: true" + yamlLineBreak() +
" Damage: 2" + yamlLineBreak() +
" PublicBukkitValues:" + yamlLineBreak() +
" some_plugin:some-other-key:" + yamlLineBreak() +
" inner_plugin:inner-key: 0.3f" + yamlLineBreak() +
" some_plugin:some-key: some value" + yamlLineBreak(), yamlString);
Assert.assertEquals("item:" + ConfigUtils.yamlLineBreak() +
" type: DIAMOND_SWORD" + ConfigUtils.yamlLineBreak() +
" display-name: '&cCustom Name'" + ConfigUtils.yamlLineBreak() +
" loc-name: loc name" + ConfigUtils.yamlLineBreak() +
" lore:" + ConfigUtils.yamlLineBreak() +
" - '&alore1'" + ConfigUtils.yamlLineBreak() +
" - lore2" + ConfigUtils.yamlLineBreak() +
" custom-model-data: 1" + ConfigUtils.yamlLineBreak() +
" enchants:" + ConfigUtils.yamlLineBreak() +
" DURABILITY: 1" + ConfigUtils.yamlLineBreak() +
" DAMAGE_ALL: 2" + ConfigUtils.yamlLineBreak() +
" attribute-modifiers:" + ConfigUtils.yamlLineBreak() +
" GENERIC_ATTACK_SPEED:" + ConfigUtils.yamlLineBreak() +
" - ==: org.bukkit.attribute.AttributeModifier" + ConfigUtils.yamlLineBreak() +
" amount: 2.0" + ConfigUtils.yamlLineBreak() +
" name: attack speed bonus" + ConfigUtils.yamlLineBreak() +
" slot: HAND" + ConfigUtils.yamlLineBreak() +
" uuid: 00000000-0000-0001-0000-000000000001" + ConfigUtils.yamlLineBreak() +
" operation: 0" + ConfigUtils.yamlLineBreak() +
" - ==: org.bukkit.attribute.AttributeModifier" + ConfigUtils.yamlLineBreak() +
" amount: 0.5" + ConfigUtils.yamlLineBreak() +
" name: attack speed bonus 2" + ConfigUtils.yamlLineBreak() +
" slot: OFF_HAND" + ConfigUtils.yamlLineBreak() +
" uuid: 00000000-0000-0002-0000-000000000002" + ConfigUtils.yamlLineBreak() +
" operation: 2" + ConfigUtils.yamlLineBreak() +
" GENERIC_MAX_HEALTH:" + ConfigUtils.yamlLineBreak() +
" - ==: org.bukkit.attribute.AttributeModifier" + ConfigUtils.yamlLineBreak() +
" amount: 2.0" + ConfigUtils.yamlLineBreak() +
" name: attack speed bonus" + ConfigUtils.yamlLineBreak() +
" slot: HAND" + ConfigUtils.yamlLineBreak() +
" uuid: 00000000-0000-0003-0000-000000000003" + ConfigUtils.yamlLineBreak() +
" operation: 0" + ConfigUtils.yamlLineBreak() +
" ItemFlags:" + ConfigUtils.yamlLineBreak() +
" - HIDE_ENCHANTS" + ConfigUtils.yamlLineBreak() +
" Unbreakable: true" + ConfigUtils.yamlLineBreak() +
" Damage: 2" + ConfigUtils.yamlLineBreak() +
" PublicBukkitValues:" + ConfigUtils.yamlLineBreak() +
" some_plugin:some-other-key:" + ConfigUtils.yamlLineBreak() +
" inner_plugin:inner-key: 0.3f" + ConfigUtils.yamlLineBreak() +
" some_plugin:some-key: some value" + ConfigUtils.yamlLineBreak(), yamlString);
}
@Test
@ -246,14 +242,14 @@ public class ItemDataTest extends AbstractTestBase {
YamlConfiguration config = new YamlConfiguration();
config.set("item", serialized);
String yamlString = config.saveToString();
Assert.assertEquals("item:" + yamlLineBreak() +
" type: LEATHER_CHESTPLATE" + yamlLineBreak() +
" display-name: '&cCustom Name'" + yamlLineBreak() +
" color:" + yamlLineBreak() +
" ==: Color" + yamlLineBreak() +
" RED: 0" + yamlLineBreak() +
" BLUE: 255" + yamlLineBreak() +
" GREEN: 0" + yamlLineBreak(), yamlString);
Assert.assertEquals("item:" + ConfigUtils.yamlLineBreak() +
" type: LEATHER_CHESTPLATE" + ConfigUtils.yamlLineBreak() +
" display-name: '&cCustom Name'" + ConfigUtils.yamlLineBreak() +
" color:" + ConfigUtils.yamlLineBreak() +
" ==: Color" + ConfigUtils.yamlLineBreak() +
" RED: 0" + ConfigUtils.yamlLineBreak() +
" BLUE: 255" + ConfigUtils.yamlLineBreak() +
" GREEN: 0" + ConfigUtils.yamlLineBreak(), yamlString);
}
@Test
@ -286,7 +282,7 @@ public class ItemDataTest extends AbstractTestBase {
YamlConfiguration config = new YamlConfiguration();
config.set("item", serialized);
String yamlString = config.saveToString();
Assert.assertEquals("item: CHEST" + yamlLineBreak(), yamlString);
Assert.assertEquals("item: CHEST" + ConfigUtils.yamlLineBreak(), yamlString);
}
@Test
@ -322,9 +318,9 @@ public class ItemDataTest extends AbstractTestBase {
YamlConfiguration config = new YamlConfiguration();
config.set("item", serialized);
String yamlString = config.saveToString();
Assert.assertEquals("item:" + yamlLineBreak() +
" type: CHEST" + yamlLineBreak() +
" display-name: '&cCustom Name'" + yamlLineBreak(), yamlString);
Assert.assertEquals("item:" + ConfigUtils.yamlLineBreak() +
" type: CHEST" + ConfigUtils.yamlLineBreak() +
" display-name: '&cCustom Name'" + ConfigUtils.yamlLineBreak(), yamlString);
}
@Test