Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
Updates and bug fixes
Browse files Browse the repository at this point in the history
+ Allow console to give crystals to players
+ Fixed some issues with messages failing to replace placeholders or having incorrect replacements
  • Loading branch information
Puyodead1 committed May 15, 2022
1 parent 7367835 commit b26688d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 26 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.puyodead1</groupId>
<artifactId>EnchantCrystals</artifactId>
<version>2.1.5</version>
<version>2.1.6</version>
<packaging>jar</packaging>

<name>EnchantCrystals</name>
Expand Down Expand Up @@ -112,7 +112,7 @@
<configuration>
<target>
<copy file="target/${project.name}-${project.version}.jar"
toFile="C:\Users\Puyodead1\Desktop\TestServer\1.18.2\plugins\${project.name}-${project.version}.jar"/>
toFile="C:\Users\23562\Desktop\TestServer\1.18.2\plugins\${project.name}-${project.version}.jar"/>
</target>
</configuration>
<goals>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ public static String replaceExceed(String string, Enchantment enchantment) {
.replace("%ENCHANTMENT_NAME%", translateEnchantmentName(enchantment));
}

public static String replace(final String string, final Enchantment enchantment, final int level, final int amount, final Player player) {
public static String replace(final String string, final Enchantment enchantment, final int level, final int amount, final CommandSender sender) {
return string
.replace("%ENCHANTMENT_NAME%", translateEnchantmentName(enchantment))
.replace("%CRYSTAL_DISPLAY_NAME%", Objects.requireNonNull(Objects.requireNonNull(EnchantCrystals.getPlugin().getConfig().getString("settings.item.display_name")).replace("%ENCHANTMENT_NAME%", translateEnchantmentName(enchantment))))
.replace("%ENCHANTMENT_LEVEL%", translateEnchantmentLevel(level))
.replace("%PLAYER_NAME%", player.getName())
.replace("%PLAYER_NAME%", sender.getName())
.replace("%CRYSTAL_AMOUNT%", String.valueOf(amount));
}

Expand All @@ -122,4 +122,4 @@ public static boolean isCrystal(final NBTItem nbtItem) {
// TODO: Since we add custom NBT to tell if an item is a crystal, we could convert crystal materials if they are changed in the config
return nbtItem.hasKey("enchantcrystals:isEnchantCrystal");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.enchantments.EnchantmentWrapper;
import org.bukkit.entity.Player;
Expand All @@ -25,11 +26,9 @@ public class EnchantCrystalsCommand implements CommandExecutor {

@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, String[] args) {
final Player player = sender instanceof Player ? (Player) sender : null;
if (player == null) return true;
if (!(sender instanceof Player) && !(sender instanceof ConsoleCommandSender)) return false;

// missing permission
if (!player.hasPermission("enchantcrystals.enchantcrystals")) {
if (!sender.hasPermission("enchantcrystals.enchantcrystals")) {
EnchantCrystalsUtils.sendSender(sender, EnchantCrystals.getPlugin().getConfig().getString("messages.missing_permission"));
return true;
}
Expand Down Expand Up @@ -64,51 +63,67 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
case "enchants":
for (final Enchantment enchantment : EnchantmentWrapper.values()) {
// TODO: config
EnchantCrystalsUtils.sendPlayer(player, EnchantCrystalsUtils.colorize("&e&l" + EnchantCrystalsUtils.translateEnchantmentName(enchantment) + "&7: " + enchantment.getKey().getKey()));
EnchantCrystalsUtils.sendSender(sender, EnchantCrystalsUtils.colorize("&e&l" + EnchantCrystalsUtils.translateEnchantmentName(enchantment) + "&7: " + enchantment.getKey().getKey()));
}
return true;
case "give":
return processGiveCommand(player, argss);
return processGiveCommand(sender, argss);
case "add":
return processAddCommand(player, argss);
if (!(sender instanceof Player)) {
EnchantCrystalsUtils.sendSender(sender, EnchantCrystalsUtils.colorize("&4&lSorry, only players can use this command."));
return true;
}
return processAddCommand((Player) sender, argss);
default:
break;
}
}
return false;
}

private boolean processGiveCommand(Player player, List<String> args) {
private boolean processGiveCommand(CommandSender sender, List<String> args) {
final int l = args.size();

if (l == 0) {
EnchantCrystalsUtils.sendPlayer(player, EnchantCrystalsUtils.colorize("&e/ecr give <enchant key> [level] [amount] [player]"));
EnchantCrystalsUtils.sendSender(sender, EnchantCrystalsUtils.colorize("&e/ecr give <enchant key> [level] [amount] [player]"));
return true;
} else if (l == 1) {
if (!(sender instanceof Player)) {
EnchantCrystalsUtils.sendSender(sender, EnchantCrystalsUtils.colorize("&4Sorry, only players can use this command."));
return true;
}
final String enchantmentName = args.get(0).toLowerCase();
return giveCrystal(player, enchantmentName, 1, 1);
return giveCrystal((Player) sender, enchantmentName, 1, 1);
} else if (l == 2) {
if (!(sender instanceof Player)) {
EnchantCrystalsUtils.sendSender(sender, EnchantCrystalsUtils.colorize("&4Sorry, only players can use this command."));
return true;
}
//player specified enchant name and level
final String enchantmentName = args.get(0).toLowerCase();
final int level = Integer.parseInt(args.get(1));
return giveCrystal(player, enchantmentName, level, 1);
return giveCrystal((Player) sender, enchantmentName, level, 1);
} else if (l == 3) {
if (!(sender instanceof Player)) {
EnchantCrystalsUtils.sendSender(sender, EnchantCrystalsUtils.colorize("&4Sorry, only players can use this command."));
return true;
}
// player specified enchant name, level, and amount
final String enchantmentName = args.get(0).toLowerCase();
final int level = Integer.parseInt(args.get(1));
final int amount = Integer.parseInt(args.get(2));
return giveCrystal(player, enchantmentName, level, amount);
return giveCrystal((Player) sender, enchantmentName, level, amount);
} else if (l == 4) {
// player specified enchant name, level, amount, and player to give to
final String enchantmentName = args.get(0).toLowerCase();
final int level = Integer.parseInt(args.get(1));
final int amount = Integer.parseInt(args.get(2));
final Player player1 = Bukkit.getPlayer(args.get(3));
if (Objects.isNull(player1)) {
EnchantCrystalsUtils.sendPlayer(player, EnchantCrystals.getPlugin().getConfig().getString("messages.invalid_player"));
EnchantCrystalsUtils.sendPlayer((Player) sender, EnchantCrystals.getPlugin().getConfig().getString("messages.invalid_player"));
return true;
}
return giveCrystal(player, enchantmentName, level, amount, player1);
return giveCrystal(sender, enchantmentName, level, amount, player1);
}
return false;
}
Expand Down Expand Up @@ -198,24 +213,29 @@ public boolean giveCrystal(Player player, String enchantmentName, int level, int
}

// Enchantment name, amount, level, and a player were specified
public boolean giveCrystal(Player sender, String enchantmentName, int level, int amount, Player player) {
public boolean giveCrystal(CommandSender sender, String enchantmentName, int level, int amount, Player player) {
final Enchantment enchantment = getEnchantmentByKey(enchantmentName);
if (Objects.isNull(enchantment)) return false;
if (Objects.isNull(enchantment)) {
EnchantCrystalsUtils.sendSender(sender, EnchantCrystals.getPlugin().getConfig().getString("messages.invalid_enchantment"));
return true;
}
final Crystal crystal = makeCrystal(enchantment, level, amount);

if (Objects.isNull(crystal)) {
EnchantCrystalsUtils.sendPlayer(player, EnchantCrystals.getPlugin().getConfig().getString("messages.invalid_enchantment"));
EnchantCrystalsUtils.sendSender(sender, EnchantCrystals.getPlugin().getConfig().getString("messages.invalid_enchantment"));
return true;
}

if (level > enchantment.getMaxLevel()) {
EnchantCrystalsUtils.sendPlayer(player, EnchantCrystalsUtils.colorize(Objects.requireNonNull(EnchantCrystals.getPlugin().getConfig().getString("messages.enchantment_bounds_error")).replace("%ENCHANTMENT_LEVEL_START%", String.valueOf(enchantment.getStartLevel()).replace("%ENCHANTMENT_LEVEL_MAX%", String.valueOf(enchantment.getMaxLevel())))));
System.out.println(enchantment.getStartLevel());
System.out.println(enchantment.getMaxLevel());
EnchantCrystalsUtils.sendSender(sender, Objects.requireNonNull(EnchantCrystals.getPlugin().getConfig().getString("messages.enchantment_bounds_error")).replace("%ENCHANTMENT_LEVEL_START%", String.valueOf(enchantment.getStartLevel())).replace("%ENCHANTMENT_LEVEL_MAX%", String.valueOf(enchantment.getMaxLevel())));
return true;
}

player.getInventory().addItem(crystal.build().getItemStack());
EnchantCrystalsUtils.sendSender(sender, EnchantCrystalsUtils.replace(Objects.requireNonNull(EnchantCrystals.getPlugin().getConfig().getString("messages.crystal_given_to_player")), enchantment, amount, level, player));
EnchantCrystalsUtils.sendSender(player, EnchantCrystalsUtils.replace(Objects.requireNonNull(EnchantCrystals.getPlugin().getConfig().getString("messages.crystal_received")), enchantment, amount, level, player));
EnchantCrystalsUtils.sendSender(player, EnchantCrystalsUtils.replace(Objects.requireNonNull(EnchantCrystals.getPlugin().getConfig().getString("messages.crystal_received")), enchantment, amount, level, sender));
return true;
}
}
4 changes: 2 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ messages:
enchantment_upgraded: "&aSuccessfully upgraded &e%ENCHANTMENT_NAME% &afrom level &e%ENCHANTMENT_LEVEL_OLD% &ato level &e%ENCHANTMENT_LEVEL_NEW%&a!"
invalid_item: "&e%ENCHANTMENT_NAME% &ccannot be applied to &e%ITEM_DISPLAY_NAME%!"
invalid_player: "&cInvalid player specified!"
invalid_enchantment: "&cInvalid Enchantment! Check &e/gc enchants &cfor a list of valid enchantments."
invalid_enchantment: "&cInvalid Enchantment! Check &e/ecr enchants &cfor a list of valid enchantments."
missing_permission: "&cYou do not have permission to run that command!"
crystal_given: "&aAdded &ex%CRYSTAL_AMOUNT% %ENCHANTMENT_NAME% &6Enchantment Crystal &ato your inventory!"
crystal_given_to_player: "&a%PLAYER_NAME% has been given &ex%CRYSTAL_AMOUNT% %ENCHANTMENT_NAME%&a!"
crystal_received: "&aYou have been given &ex%CRYSTAL_AMOUNT% %ENCHANTMENT_NAME% &afrom &e%PLAYER_NAME%&a!"
creative_error: "&cYou cannot apply crystals in creative mode!"
add_invalid_item: "&cYou must be holding an &6Enchantment Crystal &cto apply enchants!"
add_success: "&e%ENCHANTMENT_NAME% &ahas been added to the crystal!"
add_success: "&e%ENCHANTMENT_NAME% &ahas been added to the crystal!"

0 comments on commit b26688d

Please sign in to comment.