-
-
Notifications
You must be signed in to change notification settings - Fork 382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🚀 Enchantment Improvements #4366
Changes from all commits
a672464
bc52165
4564525
782ce15
e8b83b3
9189377
029720e
11e0e8e
c6b7cf9
de7e9dd
e616818
eaf7f57
cfe81d2
429a6df
8643058
48f75ba
2398bde
eddd8b6
7078f60
a139e22
1cc0840
d0e6451
fd4c257
e52049e
0560dc1
581813e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -49,6 +49,7 @@ | |||||||||||||
import org.bukkit.inventory.Inventory; | ||||||||||||||
import org.bukkit.inventory.ItemStack; | ||||||||||||||
import org.bukkit.inventory.PlayerInventory; | ||||||||||||||
import org.bukkit.inventory.meta.EnchantmentStorageMeta; | ||||||||||||||
import org.bukkit.inventory.meta.ItemMeta; | ||||||||||||||
import org.bukkit.inventory.meta.SkullMeta; | ||||||||||||||
import org.eclipse.jdt.annotation.Nullable; | ||||||||||||||
|
@@ -1283,10 +1284,10 @@ public boolean hasEnchantments(EnchantmentType... enchantments) { | |||||||||||||
ItemMeta meta = getItemMeta(); | ||||||||||||||
for (EnchantmentType enchantment : enchantments) { | ||||||||||||||
Enchantment type = enchantment.getType(); | ||||||||||||||
assert type != null; // Bukkit working different than we expect | ||||||||||||||
assert type != null; | ||||||||||||||
if (!meta.hasEnchant(type)) | ||||||||||||||
return false; | ||||||||||||||
if (enchantment.getInternalLevel() != -1 && meta.getEnchantLevel(type) < enchantment.getLevel()) | ||||||||||||||
if (enchantment.getInternalLevel() != -1 && meta.getEnchantLevel(type) != enchantment.getLevel()) | ||||||||||||||
return false; | ||||||||||||||
} | ||||||||||||||
return true; | ||||||||||||||
|
@@ -1301,7 +1302,7 @@ public void addEnchantments(EnchantmentType... enchantments) { | |||||||||||||
|
||||||||||||||
for (EnchantmentType enchantment : enchantments) { | ||||||||||||||
Enchantment type = enchantment.getType(); | ||||||||||||||
assert type != null; // Bukkit working different than we expect | ||||||||||||||
assert type != null; | ||||||||||||||
meta.addEnchant(type, enchantment.getLevel(), true); | ||||||||||||||
} | ||||||||||||||
setItemMeta(meta); | ||||||||||||||
|
@@ -1316,7 +1317,7 @@ public void removeEnchantments(EnchantmentType... enchantments) { | |||||||||||||
|
||||||||||||||
for (EnchantmentType enchantment : enchantments) { | ||||||||||||||
Enchantment type = enchantment.getType(); | ||||||||||||||
assert type != null; // Bukkit working different than we expect | ||||||||||||||
assert type != null; | ||||||||||||||
meta.removeEnchant(type); | ||||||||||||||
} | ||||||||||||||
setItemMeta(meta); | ||||||||||||||
|
@@ -1337,6 +1338,94 @@ public void clearEnchantments() { | |||||||||||||
setItemMeta(meta); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Gets all stored enchantments of this item. | ||||||||||||||
* @return the stored enchantments of this item type. | ||||||||||||||
*/ | ||||||||||||||
public EnchantmentType @Nullable [] getStoredEnchantmentTypes() { | ||||||||||||||
EnchantmentStorageMeta meta = getEnchantmentStorageMeta(); | ||||||||||||||
if (meta == null) | ||||||||||||||
return null; | ||||||||||||||
|
||||||||||||||
return meta.getStoredEnchants().entrySet().stream() | ||||||||||||||
.map(enchant -> new EnchantmentType(enchant.getKey(), enchant.getValue())) | ||||||||||||||
.toArray(EnchantmentType[]::new); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Checks whether this item type has stored enchantments or contains the given stored enchantments. | ||||||||||||||
* Also, checks the enchantment level. | ||||||||||||||
* @param enchantments The enchantments to be checked. | ||||||||||||||
AyhamAl-Ali marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
* @return Whether is has stored enchantments or not. | ||||||||||||||
*/ | ||||||||||||||
public boolean hasStoredEnchantments(EnchantmentType... enchantments) { | ||||||||||||||
AyhamAl-Ali marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
EnchantmentStorageMeta meta = getEnchantmentStorageMeta(); | ||||||||||||||
if (meta == null) | ||||||||||||||
return false; | ||||||||||||||
if (!meta.hasStoredEnchants()) | ||||||||||||||
return false; | ||||||||||||||
Comment on lines
+1363
to
+1366
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
||||||||||||||
for (EnchantmentType enchantment : enchantments) { | ||||||||||||||
Enchantment type = enchantment.getType(); | ||||||||||||||
assert type != null; // Bukkit working different from what we expect | ||||||||||||||
if (!meta.hasStoredEnchant(type)) | ||||||||||||||
return false; | ||||||||||||||
if (enchantment.getInternalLevel() != -1 && meta.getStoredEnchantLevel(type) != enchantment.getLevel()) | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -1 represents an enchantment type without a specific level (e.g. it has any level of X stored) right? Maybe we should move this into a public field on EnchantmentType with docs. Or maybe better, a method like |
||||||||||||||
return false; | ||||||||||||||
} | ||||||||||||||
return true; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Adds the given enchantments to the stored enchantments of this item type. | ||||||||||||||
* @param enchantments The enchantments to be added. | ||||||||||||||
*/ | ||||||||||||||
public void addStoredEnchantments(EnchantmentType... enchantments) { | ||||||||||||||
EnchantmentStorageMeta meta = getEnchantmentStorageMeta(); | ||||||||||||||
if (meta == null) | ||||||||||||||
return; | ||||||||||||||
|
||||||||||||||
for (EnchantmentType enchantment : enchantments) { | ||||||||||||||
Enchantment type = enchantment.getType(); | ||||||||||||||
assert type != null; | ||||||||||||||
meta.addStoredEnchant(type, enchantment.getLevel(), true); | ||||||||||||||
} | ||||||||||||||
setItemMeta(meta); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Removes the given enchantments from the stored enchantments of this item type. | ||||||||||||||
* @param enchantments The enchantments to be removed. | ||||||||||||||
*/ | ||||||||||||||
public void removeStoredEnchantments(EnchantmentType... enchantments) { | ||||||||||||||
EnchantmentStorageMeta meta = getEnchantmentStorageMeta(); | ||||||||||||||
if (meta == null) | ||||||||||||||
return; | ||||||||||||||
|
||||||||||||||
for (EnchantmentType enchantment : enchantments) { | ||||||||||||||
Enchantment type = enchantment.getType(); | ||||||||||||||
assert type != null; | ||||||||||||||
meta.removeStoredEnchant(type); | ||||||||||||||
} | ||||||||||||||
setItemMeta(meta); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Clears all stored enchantments from this item type | ||||||||||||||
*/ | ||||||||||||||
public void clearStoredEnchantments() { | ||||||||||||||
EnchantmentStorageMeta meta = getEnchantmentStorageMeta(); | ||||||||||||||
if (meta == null) | ||||||||||||||
return; | ||||||||||||||
|
||||||||||||||
Set<Enchantment> enchants = meta.getStoredEnchants().keySet(); | ||||||||||||||
for (Enchantment ench : enchants) { | ||||||||||||||
assert ench != null; | ||||||||||||||
meta.removeStoredEnchant(ench); | ||||||||||||||
} | ||||||||||||||
setItemMeta(meta); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Gets item meta that applies to all items represented by this type. | ||||||||||||||
* @return Item meta. | ||||||||||||||
|
@@ -1345,6 +1434,20 @@ public ItemMeta getItemMeta() { | |||||||||||||
return globalMeta != null ? globalMeta : types.get(0).getItemMeta(); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Gets {@link EnchantmentStorageMeta} that applies to all items represented by this type if possible. | ||||||||||||||
* @return Item meta. | ||||||||||||||
*/ | ||||||||||||||
@Nullable | ||||||||||||||
public EnchantmentStorageMeta getEnchantmentStorageMeta() { | ||||||||||||||
AyhamAl-Ali marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
ItemMeta itemMeta = getItemMeta(); | ||||||||||||||
if (itemMeta instanceof EnchantmentStorageMeta) { | ||||||||||||||
return (EnchantmentStorageMeta) itemMeta; | ||||||||||||||
} else { | ||||||||||||||
return null; | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Sets item meta that is applied for everything this type represents. | ||||||||||||||
* Note that previous item meta is overridden if it exists. | ||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,90 @@ | ||||||||||
/** | ||||||||||
* This file is part of Skript. | ||||||||||
* | ||||||||||
* Skript is free software: you can redistribute it and/or modify | ||||||||||
* it under the terms of the GNU General Public License as published by | ||||||||||
* the Free Software Foundation, either version 3 of the License, or | ||||||||||
* (at your option) any later version. | ||||||||||
* | ||||||||||
* Skript is distributed in the hope that it will be useful, | ||||||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||
* GNU General Public License for more details. | ||||||||||
* | ||||||||||
* You should have received a copy of the GNU General Public License | ||||||||||
* along with Skript. If not, see <http://www.gnu.org/licenses/>. | ||||||||||
* | ||||||||||
* Copyright Peter Güttinger, SkriptLang team and contributors | ||||||||||
*/ | ||||||||||
package ch.njol.skript.conditions; | ||||||||||
|
||||||||||
import ch.njol.skript.aliases.ItemType; | ||||||||||
import ch.njol.skript.conditions.base.PropertyCondition; | ||||||||||
import ch.njol.skript.conditions.base.PropertyCondition.PropertyType; | ||||||||||
import ch.njol.skript.doc.Description; | ||||||||||
import ch.njol.skript.doc.Examples; | ||||||||||
import ch.njol.skript.doc.Name; | ||||||||||
import ch.njol.skript.doc.Since; | ||||||||||
import ch.njol.skript.lang.Condition; | ||||||||||
import ch.njol.skript.lang.Expression; | ||||||||||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||||||||||
import ch.njol.util.Kleenean; | ||||||||||
import org.bukkit.enchantments.Enchantment; | ||||||||||
import org.bukkit.event.Event; | ||||||||||
import org.bukkit.inventory.meta.EnchantmentStorageMeta; | ||||||||||
import org.bukkit.inventory.meta.ItemMeta; | ||||||||||
import org.eclipse.jdt.annotation.Nullable; | ||||||||||
|
||||||||||
@Name("Has Conflicting Enchantments") | ||||||||||
@Description("Checks whether an item has conflicting enchantments with the given enchantment.") | ||||||||||
@Examples({ | ||||||||||
"player's tool has conflicting enchantments with efficiency", | ||||||||||
"event-item has conflicting stored enchantments with power" | ||||||||||
}) | ||||||||||
@Since("INSERT VERSION") | ||||||||||
public class CondHasConflictingEnchantments extends Condition { | ||||||||||
|
||||||||||
static { | ||||||||||
PropertyCondition.register(CondHasConflictingEnchantments.class, PropertyType.HAVE, | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about a "can be enchanted with" condition? Do you think that reflects the property well? |
||||||||||
"conflicting [:stored] enchant[ment]s with %enchantment%", "itemtypes"); | ||||||||||
} | ||||||||||
|
||||||||||
@SuppressWarnings("NotNullFieldNotInitialized") | ||||||||||
private Expression<ItemType> items; | ||||||||||
@SuppressWarnings("NotNullFieldNotInitialized") | ||||||||||
private Expression<Enchantment> ench; | ||||||||||
private boolean isStored; | ||||||||||
|
||||||||||
@Override | ||||||||||
@SuppressWarnings({"unchecked", "null"}) | ||||||||||
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||||||||||
items = (Expression<ItemType>) exprs[0]; | ||||||||||
ench = (Expression<Enchantment>) exprs[1]; | ||||||||||
isStored = parseResult.hasTag("stored"); | ||||||||||
setNegated(matchedPattern == 1); | ||||||||||
return true; | ||||||||||
} | ||||||||||
|
||||||||||
@Override | ||||||||||
public boolean check(Event event) { | ||||||||||
Enchantment ench = this.ench.getSingle(event); | ||||||||||
if (ench == null) | ||||||||||
return false; | ||||||||||
|
||||||||||
return items.check(event, item -> { | ||||||||||
ItemMeta meta = item.getItemMeta(); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could use the method added to ItemType right? |
||||||||||
if ((isStored && meta instanceof EnchantmentStorageMeta)) { | ||||||||||
return ((EnchantmentStorageMeta) meta).hasConflictingStoredEnchant(ench); | ||||||||||
Comment on lines
+76
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
} else { | ||||||||||
return meta.hasConflictingEnchant(ench); | ||||||||||
} | ||||||||||
}, isNegated()); | ||||||||||
} | ||||||||||
|
||||||||||
@Override | ||||||||||
public String toString(@Nullable Event event, boolean debug) { | ||||||||||
return PropertyCondition.toString(this, PropertyType.HAVE, event, debug, items, | ||||||||||
"conflicting " + (isStored ? "stored " : "") + "enchantments with " + ench.toString(event, debug)); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,9 +18,7 @@ | |
*/ | ||
package ch.njol.skript.conditions; | ||
|
||
import org.bukkit.event.Event; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.aliases.ItemType; | ||
import ch.njol.skript.conditions.base.PropertyCondition; | ||
import ch.njol.skript.conditions.base.PropertyCondition.PropertyType; | ||
|
@@ -33,50 +31,63 @@ | |
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
import ch.njol.skript.util.EnchantmentType; | ||
import ch.njol.util.Kleenean; | ||
import org.bukkit.event.Event; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
/** | ||
* @author Peter Güttinger | ||
*/ | ||
@Name("Is Enchanted") | ||
@Description("Checks whether an item is enchanted.") | ||
@Examples({"tool of the player is enchanted with efficiency 2", | ||
"helm, chestplate, leggings or boots are enchanted"}) | ||
@Since("1.4.6") | ||
"helm, chestplate, leggings or boots are enchanted", | ||
"", | ||
"# For enchanted books", | ||
"player's tool is stored enchanted", | ||
"event-item is enchanted with stored power 2" | ||
}) | ||
@Since("1.4.6, INSERT VERSION (stored enchantments)") | ||
public class CondIsEnchanted extends Condition { | ||
AyhamAl-Ali marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
static { | ||
PropertyCondition.register(CondIsEnchanted.class, "enchanted [with %-enchantmenttype%]", "itemtypes"); | ||
Skript.registerCondition(CondIsEnchanted.class, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should support |
||
"%itemtypes% ((is|are)|not:(isn't|is not|aren't|are not)) enchanted [with %-enchantmenttypes%]", | ||
"%itemtypes% ((has|have)|not:(doesn't have|don't have)) %enchantmenttypes% stored", | ||
"%enchantmenttypes% ((is|are)|not:(is not|isn't|are not|aren't)) stored on %itemtypes%" | ||
); | ||
} | ||
|
||
@SuppressWarnings("NotNullFieldNotInitialized") | ||
private Expression<ItemType> items; | ||
@Nullable | ||
private Expression<EnchantmentType> enchs; | ||
private boolean isStored; | ||
|
||
@SuppressWarnings({"unchecked", "null"}) | ||
@Override | ||
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) { | ||
items = (Expression<ItemType>) exprs[0]; | ||
enchs = (Expression<EnchantmentType>) exprs[1]; | ||
setNegated(matchedPattern == 1); | ||
@SuppressWarnings({"unchecked", "null"}) | ||
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
items = (Expression<ItemType>) (matchedPattern == 2 ? exprs[1] : exprs[0]); | ||
enchs = (Expression<EnchantmentType>) (matchedPattern == 2 ? exprs[0] : exprs[1]); | ||
isStored = matchedPattern > 0; | ||
setNegated(parseResult.hasTag("not")); | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean check(final Event e) { | ||
if (enchs != null) | ||
return items.check(e, item -> enchs.check(e, item::hasEnchantments), isNegated()); | ||
else | ||
return items.check(e, ItemType::hasEnchantments, isNegated()); | ||
|
||
public boolean check(Event event) { | ||
if (enchs != null) { | ||
return items.check(event, item -> | ||
(isStored && item.getEnchantmentStorageMeta() != null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i don't think it's correct to check the regular enchantments if an item does not have stored enchantment meta. if that is the case but isStored is true, the result should just be false (hasStoredEnchantments will handle the null check right?) |
||
? enchs.check(event, item::hasStoredEnchantments) : enchs.check(event, item::hasEnchantments), isNegated()); | ||
} else { | ||
return items.check(event, item -> | ||
(isStored && item.getEnchantmentStorageMeta() != null) | ||
? item.hasStoredEnchantments() : item.hasEnchantments(), isNegated()); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public String toString(final @Nullable Event e, final boolean debug) { | ||
final Expression<EnchantmentType> es = enchs; | ||
|
||
return PropertyCondition.toString(this, PropertyType.BE, e, debug, items, | ||
"enchanted" + (es == null ? "" : " with " + es.toString(e, debug))); | ||
public String toString(@Nullable Event event, boolean debug) { | ||
return PropertyCondition.toString(this, PropertyType.BE, event, debug, items, "enchanted" + | ||
(isStored ? " and stored inside" : "") + (enchs == null ? "" : " with " + enchs.toString(event, debug))); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this also check
!meta.hasStoredEnchants()
?