Skip to content

Commit

Permalink
Add aliases as submodule; API improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bensku committed Jul 19, 2018
1 parent cc33a20 commit d3c9b17
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "skript-aliases"]
path = skript-aliases
url = https://github.com/SkriptLang/skript-aliases
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ jar {
attributes("Name": "ch/njol/skript",
"Sealed": "true")
}

from('skript-aliases') {
into('aliases')
}
}

license {
Expand Down
1 change: 1 addition & 0 deletions skript-aliases
Submodule skript-aliases added at 5745dd
25 changes: 24 additions & 1 deletion src/main/java/ch/njol/skript/aliases/Aliases.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,23 @@ private static AliasesProvider createProvider() {
provider.registerCondition("minecraft version", (str) -> {
int orNewer = str.indexOf("or newer"); // For example: 1.12 or newer
if (orNewer != -1) {
@SuppressWarnings("null")
Version ver = new Version(str.substring(0, orNewer - 1));
return Skript.getMinecraftVersion().compareTo(ver) >= 0;
}

int orOlder = str.indexOf("or older"); // For example: 1.11 or older
if (orOlder != -1) {
@SuppressWarnings("null")
Version ver = new Version(str.substring(0, orOlder - 1));
return Skript.getMinecraftVersion().compareTo(ver) <= 0;
}

int to = str.indexOf("to"); // For example: 1.11 to 1.12
if (to != -1) {
@SuppressWarnings("null")
Version first = new Version(str.substring(0, to - 1));
@SuppressWarnings("null")
Version second = new Version(str.substring(to + 3));
Version current = Skript.getMinecraftVersion();
return current.compareTo(first) >= 0 && current.compareTo(second) <= 0;
Expand Down Expand Up @@ -332,7 +336,7 @@ private final static ItemType parseType(final String s, final ItemType t, final
t.add(new ItemData(Material.AIR));
return t;
} else if (type.matches("\\d+")) {
// TODO error: numeric ids are not supported anymore
Skript.error("Numeric ids are not supported anymore.");
return null;
} else if ((i = getAlias(type)) != null) {
for (ItemData d : i) {
Expand Down Expand Up @@ -521,4 +525,23 @@ public static boolean isSupertypeOf(ItemData first, ItemData second) {
public static String getMinecraftId(ItemData data) {
return provider.getMinecraftId(data);
}

/**
* Gets an item type that matches the given name.
* If it doesn't exist, an exception is thrown instead.
*
* <p>Item types provided by this method are updated when aliases are
* reloaded. However, this also means they are tracked by aliases system.
* Generally, they should be put in (static final) fields.
* @param name Name of item to search from aliases.
* @return An item.
* @throws IllegalArgumentException When item is not found.
*/
public static ItemType javaItemType(String name) {
ItemType type = parseItemType(name);
if (type == null)
throw new IllegalArgumentException("type not found");
// TODO type tracking
return type;
}
}
16 changes: 15 additions & 1 deletion src/main/java/ch/njol/skript/aliases/ItemType.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,15 @@ public ItemType(BlockState b) {
// TODO metadata - spawners, skulls, etc.
}

/**
* Copy constructor.
* @param i Another ItemType.
*/
private ItemType(ItemType i) {
setTo(i);
}

public void setTo(ItemType i) {
all = i.all;
amount = i.amount;
final ItemType bl = i.block, it = i.item;
Expand All @@ -217,7 +225,8 @@ public void modified() {
}

/**
* @return amount or 1 if amount == -1
* Returns amount of the item in stack that this type represents.
* @return amount.
*/
@Override
public int getAmount() {
Expand Down Expand Up @@ -247,6 +256,11 @@ public void setAmount(final int amount) {
block.amount = amount;
}

/**
* Checks if this item type represents one of its items (OR) or all of
* them (AND). If this has only one item, it doesn't matter.
* @return Whether all of the items are represented.
*/
public boolean isAll() {
return all;
}
Expand Down

0 comments on commit d3c9b17

Please sign in to comment.