Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/RoyalCommands/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<includes>
<include>*.yml</include>
<include>*.txt</include>
<include>items.csv</include>
<include>item_aliases.csv</include>
</includes>
</resource>
</resources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,15 +366,15 @@ public void reloadConfiguration() {

Reader in = null;
try {
in = new FileReader(new File(this.plugin.getDataFolder() + File.separator + "items.csv"));
in = new FileReader(new File(this.plugin.getDataFolder() + File.separator + "item_aliases.csv"));
CSVReader reader = new CSVReader(in);
RoyalCommands.inm = new ItemNameManager(reader.readAll());
reader.close();
} catch (FileNotFoundException e) {
this.plugin.getLogger().warning("items.csv was not found! Item aliases will not be used.");
this.plugin.getLogger().warning("item_aliases.csv was not found! Item aliases will not be used.");
RoyalCommands.inm = null;
} catch (final IOException e) {
this.plugin.getLogger().warning("Internal input/output error loading items.csv. Item aliases will not be used.");
this.plugin.getLogger().warning("Internal input/output error loading item_aliases.csv. Item aliases will not be used.");
RoyalCommands.inm = null;
} finally {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,22 @@ public class ItemNameManager {
public ItemNameManager(Iterable<String[]> values) {
for (String[] s : values) {
if (s.length < 1) continue;
if (s[0].startsWith("#")) continue;
String[] aliases;
try {
aliases = s[2].split(",");
aliases = s[1].split(",");
} catch (IndexOutOfBoundsException e) {
Logger l = Logger.getLogger("Minecraft");
l.warning("[RoyalCommands] Values passed in ItemNameManager invalid: ");
for (String ss : s) l.warning("[RoyalCommands] - " + ss);
continue;
}
Material m;
short data;
short data = 0;
try {
m = Material.valueOf(s[0]);
} catch (IllegalArgumentException ex) {
RoyalCommands.getInstance().getLogger().warning("Material in items.csv is invalid: " + s[0]);
continue;
}
try {
data = Short.valueOf(s[1]);
} catch (NumberFormatException e) {
RoyalCommands.getInstance().getLogger().warning("Data in items.csv file is invalid: " + s[1]);
RoyalCommands.getInstance().getLogger().warning("Material in item_aliases.csv is invalid: " + s[0]);
continue;
}
synchronized (items) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package org.royaldev.royalcommands;

import org.apache.commons.lang3.ArrayUtils;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these imports needed?

import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld;
import org.mvplugins.multiverse.core.world.MultiverseWorld;

Expand Down Expand Up @@ -86,6 +87,7 @@
import org.royaldev.royalcommands.rcommands.CmdBack;
import org.royaldev.royalcommands.spawninfo.SpawnInfo;
import org.royaldev.royalcommands.tools.NameFetcher;
import org.royaldev.royalcommands.tools.Pair;
import org.royaldev.royalcommands.tools.UUIDFetcher;

@SuppressWarnings("unused")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ public boolean isVanished(Player p, CommandSender cs) {

public void loadConfiguration() {
if (!new File(getDataFolder(), "config.yml").exists()) saveDefaultConfig();
if (!new File(getDataFolder(), "items.csv").exists()) saveResource("items.csv", false);
if (!new File(getDataFolder(), "item_aliases.csv").exists()) saveResource("item_aliases.csv", false);
if (!new File(getDataFolder(), "rules.txt").exists()) saveResource("rules.txt", false);
if (!new File(getDataFolder(), "help.txt").exists()) saveResource("help.txt", false);
if (!new File(getDataFolder(), "warps.yml").exists()) saveResource("warps.yml", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class CmdGive extends TabCommand {

public CmdGive(final RoyalCommands instance, final String name) {
super(instance, name, true, new Short[]{CompletionType.ONLINE_PLAYER.getShort(), CompletionType.ITEM_ALIAS.getShort()});
super(instance, name, true, new Short[]{CompletionType.ONLINE_PLAYER.getShort(), CompletionType.ITEM.getShort()});
}

public static boolean giveItemStandalone(CommandSender cs, Player target, String itemname, int amount) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public class CmdHelmet extends TabCommand {

public CmdHelmet(final RoyalCommands instance, final String name) {
super(instance, name, true, new Short[]{CompletionType.ITEM_ALIAS.getShort()});
super(instance, name, true, new Short[]{CompletionType.ITEM.getShort()});
}
/**
* TODO: Add support to add a helmet to another player
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class CmdItem extends TabCommand {
private static final Flag<String> loreFlag = new Flag<>(String.class, "description", "lore", "l");

public CmdItem(final RoyalCommands instance, final String name) {
super(instance, name, true, new Short[]{CompletionType.ITEM_ALIAS.getShort()});
super(instance, name, true, new Short[]{CompletionType.ITEM.getShort()});
this.addExpectedFlag(CmdItem.nameFlag);
this.addExpectedFlag(CmdItem.loreFlag);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@
import org.royaldev.royalcommands.RoyalCommands;
import org.royaldev.royalcommands.exceptions.InvalidItemNameException;


@ReflectCommand
public class CmdRecipe extends TabCommand {

private final Map<String, Integer> tasks = new HashMap<>();

public CmdRecipe(final RoyalCommands instance, final String name) {
super(instance, name, true, new Short[]{CompletionType.ITEM_ALIAS.getShort()});
super(instance, name, true, new Short[]{CompletionType.ITEM.getShort()});
this.plugin.getServer().getPluginManager().registerEvents(new WorkbenchCloseListener(), this.plugin);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class CmdUses extends TabCommand {
private final Map<String, Integer> tasks = new HashMap<>();

public CmdUses(final RoyalCommands instance, final String name) {
super(instance, name, true, new Short[]{CompletionType.ITEM_ALIAS.getShort()});
super(instance, name, true, new Short[]{CompletionType.ITEM.getShort()});
this.plugin.getServer().getPluginManager().registerEvents(new WorkbenchCloseListener(), this.plugin);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ protected List<String> getCompletionsFor(final CommandSender cs, final Command c
final String name = m.getKeyOrNull().getKey();
final String lowerCaseName = name.toLowerCase();
if (!lowerCaseName.startsWith(arg)) continue;
// Minecraft default item names
possibilities.add(lowerCaseName.equals(arg) ? 0 : possibilities.size(), name);
// Custom item names
possibilities.addAll(RoyalCommands.inm.getPossibleNames(arg));
}
break;
case ATTRIBUTE:
Expand Down Expand Up @@ -279,7 +282,7 @@ protected enum CompletionType {
*/
ONLINE_PLAYER((short) 1),
/**
* Completes for any item alias in items.csv.
* Completes for any item alias in item_aliases.csv.
*/
ITEM_ALIAS((short) 2),
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.royaldev.royalcommands.RUtils;
import org.royaldev.royalcommands.RoyalCommands;
import org.royaldev.royalcommands.shaded.com.sk89q.util.config.ConfigurationNode;
import org.royaldev.royalcommands.wrappers.player.RPlayer;

import java.util.ArrayList;
import java.util.List;

/**
* A class representing a kit.
*/
Expand Down Expand Up @@ -68,11 +66,11 @@ private Enchantment getEnchantment(final ConfigurationNode enchantment) {
* @param item Node representing an ItemStack
* @return ItemStack of null
*/
// TODO Handle invalid item names better
private ItemStack getItemStack(final ConfigurationNode item) {
final ItemStack is = RoyalCommands.inm.getItemStackFromAlias(item.getString("type"));
final Damageable imd = (Damageable) is.getItemMeta();
final ItemStack is = RUtils.getItem(item.getString("type"), item.getInt("amount", 1));
if (is == null) return null;
is.setAmount(item.getInt("amount", 1));
final Damageable imd = (Damageable) is.getItemMeta();
imd.setDamage(item.getShort("damage", (short) 0));
Comment on lines +73 to 74
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two lines could probably be removed, as it's just setting damage to 0, which is the default, and then never actually updating the is meta.

final ItemMeta im = is.getItemMeta();
im.setDisplayName(RUtils.colorize(item.getString("name")));
Expand All @@ -92,7 +90,9 @@ private List<ItemStack> getItemStacks(final List<ConfigurationNode> items) {
final List<ItemStack> itemStacks = new ArrayList<>();
for (final ConfigurationNode item : items) {
final ItemStack is = this.getItemStack(item);
if (is == null) continue;
if (is == null) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I agree this change is probably something we want to move towards, this shouldn't be done as part of this PR and should be part of a bigger code cleanup commit that can then be ignored using Git's ignore refs.

continue;
}
itemStacks.add(is);
}
return itemStacks;
Expand Down
6 changes: 3 additions & 3 deletions modules/RoyalCommands/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ kits:
description: An example kit.
# List of items for this kit
items:
- type: LOG:OAK
- type: OAK_LOG
amount: 1
# You can use colors in lore and names
name: "&rWood of Champions"
Expand All @@ -299,9 +299,9 @@ kits:
wool:
description: A kit containing wool.
items:
- type: WOOL:BLACK
- type: BLACK_WOOL
amount: 64
- type: WOOL:BLUE
- type: BLUE_WOOL
amount: 35
# This kit can only be used once (cooldown of -1)
onetime:
Expand Down
1 change: 1 addition & 0 deletions modules/RoyalCommands/src/main/resources/item_aliases.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#"DIAMOND_SWORD","stabby_knife"
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package org.royaldev.royalcommands;

import org.bukkit.Material;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.royaldev.royalcommands.opencsv.CSVReader;

import java.io.File;
import java.io.FileReader;
import java.io.Reader;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
Expand All @@ -19,7 +16,6 @@
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;


public class TestAliases {

private YamlConfiguration pluginYml = null;
Expand All @@ -33,25 +29,6 @@ private YamlConfiguration getPluginYml() {
return this.pluginYml;
}

@Test
public void testAliases() throws Throwable {
final Logger l = Logger.getLogger("org.royaldev.royalcommands");
final File csv = new File("src/main/resources/items.csv");
assertTrue("No items.csv found!", csv.exists());
final Reader in = new FileReader(csv);
CSVReader r = new CSVReader(in);
final ItemNameManager inm = new ItemNameManager(r.readAll());
boolean allAliasesExist = true;
for (final Material m : Material.values()) {
if (inm.aliasExists(m)) continue;
if (m.name().startsWith("LEGACY")) continue;
if (allAliasesExist) allAliasesExist = false;
l.warning("Missing alias for Material " + m.name() + ".");
}
r.close();
assertTrue("Missing aliases!", allAliasesExist);
}

private RoyalCommands makeRoyalCommands() {
final RoyalCommands rc = mock(RoyalCommands.class);
when(rc.getLogger()).thenReturn(Logger.getAnonymousLogger());
Expand Down