forked from Maxlego08/zEssentials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,149 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
API/src/main/java/fr/maxlego08/essentials/api/enchantment/Enchantments.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package fr.maxlego08.essentials.api.enchantment; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface Enchantments { | ||
|
||
Optional<EssentialsEnchantment> getEnchantments(String enchantment); | ||
|
||
void register(); | ||
|
||
List<String> getEnchantments(); | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
API/src/main/java/fr/maxlego08/essentials/api/enchantment/EssentialsEnchantment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package fr.maxlego08.essentials.api.enchantment; | ||
|
||
import org.bukkit.enchantments.Enchantment; | ||
|
||
import java.util.List; | ||
|
||
public interface EssentialsEnchantment { | ||
|
||
Enchantment enchantment(); | ||
|
||
List<String> aliases(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
version=1.0.0.1 | ||
version=1.0.0.2 | ||
systemProp.file.encoding=utf-8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/main/java/fr/maxlego08/essentials/commands/commands/utils/admins/CommandEnchant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package fr.maxlego08.essentials.commands.commands.utils.admins; | ||
|
||
import fr.maxlego08.essentials.api.EssentialsPlugin; | ||
import fr.maxlego08.essentials.api.commands.CommandResultType; | ||
import fr.maxlego08.essentials.api.commands.Permission; | ||
import fr.maxlego08.essentials.api.messages.Message; | ||
import fr.maxlego08.essentials.zutils.utils.commands.VCommand; | ||
import org.bukkit.Material; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.enchantments.Enchantment; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.meta.EnchantmentStorageMeta; | ||
|
||
import java.util.Arrays; | ||
|
||
public class CommandEnchant extends VCommand { | ||
public CommandEnchant(EssentialsPlugin plugin) { | ||
super(plugin); | ||
this.setPermission(Permission.ESSENTIALS_ENCHANT); | ||
this.setDescription(Message.DESCRIPTION_ENCHANT); | ||
this.addRequireArg("enchantment", (a, b) -> plugin.getEnchantments().getEnchantments()); | ||
this.addRequireArg("level", (a, b) -> Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")); | ||
this.addOptionalArg("player"); | ||
} | ||
|
||
@Override | ||
protected CommandResultType perform(EssentialsPlugin plugin) { | ||
|
||
String enchantAsString = this.argAsString(0); | ||
int level = this.argAsInteger(1); | ||
Player player = this.argAsPlayer(2, this.player); | ||
|
||
if (level < 0) return CommandResultType.SYNTAX_ERROR; | ||
|
||
if (player == null) { | ||
message(sender, Message.COMMAND_SPEED_INVALID); | ||
return CommandResultType.DEFAULT; | ||
} | ||
|
||
var optional = plugin.getEnchantments().getEnchantments(enchantAsString); | ||
if (optional.isEmpty()) { | ||
message(sender, Message.COMMAND_ENCHANT_ERROR_ENCHANT, "%enchant%", enchantAsString); | ||
return CommandResultType.DEFAULT; | ||
} | ||
|
||
Enchantment enchantment = optional.get().enchantment(); | ||
ItemStack itemStack = player.getInventory().getItemInMainHand(); | ||
if (itemStack.getType().isAir()) { | ||
message(sender, player, Message.COMMAND_ENCHANT_ERROR_ITEM_SELF, Message.COMMAND_ENCHANT_ERROR_ITEM_PLAYER); | ||
return CommandResultType.DEFAULT; | ||
} | ||
|
||
enchant(itemStack, enchantment, level); | ||
String translatedEnchantments = "<lang:" + enchantment.translationKey() + ">"; | ||
|
||
if (level == 0) { | ||
message(sender, player, Message.COMMAND_ENCHANT_REMOVE_SELF, Message.COMMAND_ENCHANT_REMOVE_PLAYER, "%enchant%", translatedEnchantments); | ||
} else { | ||
message(sender, player, Message.COMMAND_ENCHANT_SUCCESS_SELF, Message.COMMAND_ENCHANT_SUCCESS_PLAYER, "%enchant%", translatedEnchantments); | ||
} | ||
|
||
return CommandResultType.SUCCESS; | ||
} | ||
|
||
private void message(CommandSender sender, Player player, Message messageSelf, Message messagePlayer, Object... objects) { | ||
|
||
int newSize = objects.length + 2; | ||
Object[] newObjects = new Object[newSize]; | ||
System.arraycopy(objects, 0, newObjects, 0, objects.length); | ||
newObjects[objects.length] = "%player%"; | ||
newObjects[objects.length + 1] = player.getName(); | ||
|
||
message(sender, sender == player ? messageSelf : messagePlayer, newObjects); | ||
} | ||
|
||
private void enchant(ItemStack itemStack, Enchantment enchantment, int level) { | ||
if (itemStack.getType() == Material.ENCHANTED_BOOK) { | ||
var enchantmentStorageMeta = (EnchantmentStorageMeta) itemStack.getItemMeta(); | ||
if (level == 0) enchantmentStorageMeta.removeStoredEnchant(enchantment); | ||
else enchantmentStorageMeta.addStoredEnchant(enchantment, level, true); | ||
itemStack.setItemMeta(enchantmentStorageMeta); | ||
} else { | ||
if (level == 0) itemStack.removeEnchantment(enchantment); | ||
else itemStack.addUnsafeEnchantment(enchantment, level); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.