-
-
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 9 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 |
---|---|---|
|
@@ -44,6 +44,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.eclipse.jdt.annotation.Nullable; | ||
|
||
|
@@ -1200,7 +1201,7 @@ public boolean hasEnchantments(EnchantmentType... enchantments) { | |
assert type != null; // Bukkit working different than we expect | ||
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; | ||
|
@@ -1250,6 +1251,109 @@ public void clearEnchantments() { | |
} | ||
setItemMeta(meta); | ||
} | ||
|
||
/** | ||
* Gets all stored enchantments of this item. | ||
* @return the stored enchantments of this item type. | ||
*/ | ||
@Nullable | ||
public EnchantmentType[] getStoredEnchantmentTypes() { | ||
EnchantmentStorageMeta meta = getEnchantmentStorageMeta(); | ||
if (meta == null) | ||
return new EnchantmentType[0]; | ||
AyhamAl-Ali marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Set<Entry<Enchantment, Integer>> enchants = meta.getStoredEnchants().entrySet(); | ||
AyhamAl-Ali marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return enchants.stream() | ||
.map(enchant -> new EnchantmentType(enchant.getKey(), enchant.getValue())) | ||
.toArray(EnchantmentType[]::new); | ||
AyhamAl-Ali marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* Checks whether this item type has stored enchantments. | ||
* Used for materials such as {@link Material#ENCHANTED_BOOK} | ||
*/ | ||
public boolean hasStoredEnchantments() { | ||
ItemType type = getBaseType(); | ||
ItemMeta meta = type.getItemMeta(); | ||
if (meta instanceof EnchantmentStorageMeta) | ||
return ((EnchantmentStorageMeta) meta).hasStoredEnchants(); | ||
else | ||
return false; | ||
} | ||
|
||
/** | ||
* Checks whether this item type 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
|
||
*/ | ||
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 (!hasStoredEnchantments()) | ||
return false; | ||
|
||
for (EnchantmentType enchantment : enchantments) { | ||
Enchantment type = enchantment.getType(); | ||
assert type != null; // Bukkit working different than 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; // Bukkit working different than we expect | ||
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; // Bukkit working different than we expect | ||
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. | ||
|
@@ -1259,6 +1363,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
|
||
if (globalMeta != null && globalMeta instanceof EnchantmentStorageMeta) | ||
return (EnchantmentStorageMeta) globalMeta; | ||
else if (types.get(0).getItemMeta() instanceof EnchantmentStorageMeta) | ||
return (EnchantmentStorageMeta) types.get(0).getItemMeta(); | ||
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,89 @@ | ||||||||||
/** | ||||||||||
* 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"}) | ||||||||||
AyhamAl-Ali marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
@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 [(1¦stored)] enchant[ment]s with %enchantment%", "itemtypes"); | ||||||||||
AyhamAl-Ali marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
} | ||||||||||
|
||||||||||
@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.mark > 0; | ||||||||||
setNegated(matchedPattern == 1); | ||||||||||
return true; | ||||||||||
} | ||||||||||
|
||||||||||
@Override | ||||||||||
public boolean check(Event e) { | ||||||||||
AyhamAl-Ali marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
Enchantment ench = this.ench.getSingle(e); | ||||||||||
if (ench == null) | ||||||||||
return false; | ||||||||||
|
||||||||||
return items.check(e, item -> | ||||||||||
{ | ||||||||||
AyhamAl-Ali marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
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 e, boolean debug) { | ||||||||||
return PropertyCondition.toString(this, PropertyType.HAVE, e, debug, items, | ||||||||||
"conflicting " + (isStored ? "stored " : "") + "enchantments with " + ench.toString(e, debug)); | ||||||||||
AyhamAl-Ali marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
} | ||||||||||
|
||||||||||
} |
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()
?