Skip to content

Commit

Permalink
v1.3.3 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
byteful committed May 12, 2023
1 parent e73dea2 commit 0fa4ab4
Show file tree
Hide file tree
Showing 16 changed files with 232 additions and 385 deletions.
84 changes: 42 additions & 42 deletions src/main/java/me/byteful/plugin/leveltools/LevelToolsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@

@Command("leveltools")
public class LevelToolsCommand {
@Dependency
private LevelToolsPlugin plugin;

@DefaultFor("leveltools")
@Subcommand("help")
@Description("Shows the list of LevelTools commands.")
public void onHelp(CommandSender sender, CommandHelp<String> help, @Default("1") int page) {
sender.sendMessage(colorize("&6&lLevelTools Command Help:"));
for (String entry : help.paginate(page, 7)) {
sender.sendMessage(colorize(entry));
}
@Dependency
private LevelToolsPlugin plugin;

@DefaultFor("leveltools")
@Subcommand("help")
@Description("Shows the list of LevelTools commands.")
public void onHelp(CommandSender sender, CommandHelp<String> help, @Default("1") int page) {
sender.sendMessage(colorize("&6&lLevelTools Command Help:"));
for (String entry : help.paginate(page, 7)) {
sender.sendMessage(colorize(entry));
}
}

@Subcommand("reload")
@Description("Reloads LevelTools' plugin configuration.")
@Subcommand("reload")
@Description("Reloads LevelTools' plugin configuration.")
public void onReload(CommandSender sender) {
if (!checkPerm(sender)) {
return;
Expand All @@ -40,38 +40,38 @@ public void onReload(CommandSender sender) {
sender.sendMessage(colorize(Objects.requireNonNull(plugin.getConfig().getString("messages.successful_reload"))));
}

@Subcommand("reset")
@Description("Resets all XP/Levels for all the items in the target player.")
public void onReset(CommandSender sender, Player target, @Switch("all") boolean all) {
if (!all) {
if (!LevelToolsUtil.isSupportedTool(target.getInventory().getItemInMainHand().getType())) {
sender.sendMessage(colorize(plugin.getConfig().getString("messages.item_not_tool")));

return;
}

final LevelToolsItem tool = LevelToolsUtil.createLevelToolsItem(target.getInventory().getItemInMainHand());
tool.setLevel(0);
tool.setXp(0);
target.getInventory().setItemInMainHand(tool.getItemStack());
sender.sendMessage(colorize(plugin.getConfig().getString("messages.successfully_reset_hand_tool", "&aSuccessfully reset tool in hand's XP/Levels for {player}.").replace("{player}", target.getName())));

return;
}

final PlayerInventory inv = target.getInventory();
final ItemStack[] contents = inv.getContents();
for (int i = 0; i < contents.length; i++) {
final ItemStack item = contents[i];
if (item == null || !LevelToolsUtil.isSupportedTool(item.getType())) {
continue;
}
final LevelToolsItem tool = LevelToolsUtil.createLevelToolsItem(item);
tool.setLevel(0);
@Subcommand("reset")
@Description("Resets all XP/Levels for all the items in the target player.")
public void onReset(CommandSender sender, Player target, @Switch("all") boolean all) {
if (!all) {
if (!LevelToolsUtil.isSupportedTool(target.getInventory().getItemInMainHand().getType())) {
sender.sendMessage(colorize(plugin.getConfig().getString("messages.item_not_tool")));

return;
}

final LevelToolsItem tool = LevelToolsUtil.createLevelToolsItem(target.getInventory().getItemInMainHand());
tool.setLevel(0);
tool.setXp(0);
target.getInventory().setItemInMainHand(tool.getItemStack());
sender.sendMessage(colorize(plugin.getConfig().getString("messages.successfully_reset_hand_tool", "&aSuccessfully reset tool in hand's XP/Levels for {player}.").replace("{player}", target.getName())));

return;
}

final PlayerInventory inv = target.getInventory();
final ItemStack[] contents = inv.getContents();
for (int i = 0; i < contents.length; i++) {
final ItemStack item = contents[i];
if (item == null || !LevelToolsUtil.isSupportedTool(item.getType())) {
continue;
}
final LevelToolsItem tool = LevelToolsUtil.createLevelToolsItem(item);
tool.setLevel(0);
tool.setXp(0);
inv.setItem(i, tool.getItemStack());
}
sender.sendMessage(colorize(Objects.requireNonNull(plugin.getConfig().getString("messages.successfully_reset_tools")).replace("{player}", target.getName())));
sender.sendMessage(colorize(Objects.requireNonNull(plugin.getConfig().getString("messages.successfully_reset_tools")).replace("{player}", target.getName())));
}

@Subcommand("xp")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ public boolean persist() {
return null;
}

final ItemStack hand =
RedLib.MID_VERSION <= 8
? player.getItemInHand()
: player.getInventory().getItemInMainHand();
final ItemStack hand = RedLib.MID_VERSION <= 8 ? player.getItemInHand() : player.getInventory().getItemInMainHand();

if (!LevelToolsUtil.isSupportedTool(hand.getType())) {
return "N/A";
Expand Down
59 changes: 27 additions & 32 deletions src/main/java/me/byteful/plugin/leveltools/LevelToolsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
public final class LevelToolsPlugin extends JavaPlugin {
private static LevelToolsPlugin instance;

private BlockDataManager blockDataManager;
private BukkitCommandHandler commandManager;
private AnvilCombineMode anvilCombineMode;
private BlockDataManager blockDataManager;
private BukkitCommandHandler commandManager;
private AnvilCombineMode anvilCombineMode;
private UpdateChecker updateChecker;

public static LevelToolsPlugin getInstance() {
Expand All @@ -45,15 +45,11 @@ public void onEnable() {
final Path oldFile = getDataFolder().toPath().resolve("blocks.db");
if (Files.exists(oldFile)) {
if (Files.exists(blocksFile)) {
getLogger()
.warning(
"Found old 'blocks.db' file, but ignored it because a newer 'player_placed_blocks.db' file exists!");
getLogger().warning("Found old 'blocks.db' file, but ignored it because a newer 'player_placed_blocks.db' file exists!");
} else {
try {
Files.move(oldFile, blocksFile, StandardCopyOption.COPY_ATTRIBUTES);
getLogger()
.warning(
"Found old 'blocks.db' file... Renamed to newer 'player_placed_blocks.db' file.");
getLogger().warning("Found old 'blocks.db' file... Renamed to newer 'player_placed_blocks.db' file.");
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -72,34 +68,34 @@ public void onEnable() {
blockDataManager.migrate();
getLogger().info("Loaded BlockDataManager...");

saveDefaultConfig();
getConfig().options().copyDefaults();
setAnvilCombineMode();
saveDefaultConfig();
getConfig().options().copyDefaults();
setAnvilCombineMode();
getLogger().info("Loaded configuration...");

if (getConfig().getBoolean("update.start")) {
updateChecker.check();
}

if (getConfig().getBoolean("update.periodically")) {
final long delay = 20L * TimeUnit.DAYS.toSeconds(1);
Task.syncRepeating(() -> updateChecker.check(), delay, delay);
}
if (getConfig().getBoolean("update.periodically")) {
final long delay = 20L * TimeUnit.DAYS.toSeconds(1);
Task.syncRepeating(() -> updateChecker.check(), delay, delay);
}

registerListeners();
getLogger().info("Registered listeners...");
registerListeners();
getLogger().info("Registered listeners...");

commandManager = BukkitCommandHandler.create(this);
commandManager.setHelpWriter((command, actor) -> String.format("&7- &b/%s %s&7: &e%s", command.getPath().toRealString(), command.getUsage(), command.getDescription()));
commandManager.register(new LevelToolsCommand());
commandManager.registerBrigadier();
getLogger().info("Registered commands...");
commandManager = BukkitCommandHandler.create(this);
commandManager.setHelpWriter((command, actor) -> String.format("&7- &b/%s %s&7: &e%s", command.getPath().toRealString(), command.getUsage(), command.getDescription()));
commandManager.register(new LevelToolsCommand());
commandManager.registerBrigadier();
getLogger().info("Registered commands...");

if (getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
new LevelToolsPlaceholders().register();
}
if (getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
new LevelToolsPlaceholders().register();
}

getLogger().info("Successfully started " + getDescription().getFullName() + "!");
getLogger().info("Successfully started " + getDescription().getFullName() + "!");
}

@Override
Expand All @@ -122,8 +118,7 @@ private void registerListeners() {
}

public void setAnvilCombineMode() {
anvilCombineMode =
AnvilCombineMode.fromName(Objects.requireNonNull(getConfig().getString("anvil_combine")));
anvilCombineMode = AnvilCombineMode.fromName(Objects.requireNonNull(getConfig().getString("anvil_combine")));
}

public BlockDataManager getBlockDataManager() {
Expand All @@ -134,7 +129,7 @@ public AnvilCombineMode getAnvilCombineMode() {
return anvilCombineMode;
}

public BukkitCommandHandler getCommandManager() {
return commandManager;
}
public BukkitCommandHandler getCommandManager() {
return commandManager;
}
}
101 changes: 49 additions & 52 deletions src/main/java/me/byteful/plugin/leveltools/api/AnvilCombineMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,63 @@
import java.util.function.BinaryOperator;

public enum AnvilCombineMode {
HIGHER_OF_BOTH(
(item1, item2) -> {
final int level1 = item1.getLevel();
final int level2 = item2.getLevel();

final double xp1 = item1.getXp();
final double xp2 = item2.getXp();

int level;
double xp;

if (level1 == level2) {
level = level1;
xp = Math.max(xp1, xp2);
HIGHER_OF_BOTH((item1, item2) -> {
final int level1 = item1.getLevel();
final int level2 = item2.getLevel();

final double xp1 = item1.getXp();
final double xp2 = item2.getXp();

int level;
double xp;

if (level1 == level2) {
level = level1;
xp = Math.max(xp1, xp2);
} else {
level = Math.max(level1, level2);
if (level == level1) {
xp = xp1;
} else {
level = Math.max(level1, level2);
if (level == level1) {
xp = xp1;
} else {
xp = xp2;
}
xp = xp2;
}
}

return new LevelAndXPModel(level, xp);
}),
LOWER_OF_BOTH(
(item1, item2) -> {
final int level1 = item1.getLevel();
final int level2 = item2.getLevel();

final double xp1 = item1.getXp();
final double xp2 = item2.getXp();

int level;
double xp;

if (level1 == level2) {
level = level1;
xp = Math.min(xp1, xp2);
return new LevelAndXPModel(level, xp);
}),
LOWER_OF_BOTH((item1, item2) -> {
final int level1 = item1.getLevel();
final int level2 = item2.getLevel();

final double xp1 = item1.getXp();
final double xp2 = item2.getXp();

int level;
double xp;

if (level1 == level2) {
level = level1;
xp = Math.min(xp1, xp2);
} else {
level = Math.min(level1, level2);
if (level == level1) {
xp = xp1;
} else {
level = Math.min(level1, level2);
if (level == level1) {
xp = xp1;
} else {
xp = xp2;
}
xp = xp2;
}
}

return new LevelAndXPModel(level, xp);
}),
ADD_BOTH(
(item1, item2) -> {
final int level1 = item1.getLevel();
final int level2 = item2.getLevel();
return new LevelAndXPModel(level, xp);
}),
ADD_BOTH((item1, item2) -> {
final int level1 = item1.getLevel();
final int level2 = item2.getLevel();

final double xp1 = item1.getXp();
final double xp2 = item2.getXp();
final double xp1 = item1.getXp();
final double xp2 = item2.getXp();

return new LevelAndXPModel(level1 + level2, xp1 + xp2);
});
return new LevelAndXPModel(level1 + level2, xp1 + xp2);
});

@NotNull
private final BinaryOperator<LevelAndXPModel> handler;
Expand Down
Loading

0 comments on commit 0fa4ab4

Please sign in to comment.