diff --git a/realmines-api/src/main/java/joserodpt/realmines/api/mine/RMine.java b/realmines-api/src/main/java/joserodpt/realmines/api/mine/RMine.java index 28cd231..d3eb39e 100644 --- a/realmines-api/src/main/java/joserodpt/realmines/api/mine/RMine.java +++ b/realmines-api/src/main/java/joserodpt/realmines/api/mine/RMine.java @@ -823,7 +823,20 @@ public void processBlockBreakAction(final MineBlockBreakEvent e, final Double ra if (e.isBroken() && this.getMineItems().containsKey(e.getMaterial())) { MineItem item = this.getMineItems().get(e.getMaterial()); if (item != null) { - item.getBreakActions().forEach(mineAction -> mineAction.execute(e.getPlayer(), e.getBlock().getLocation(), random)); + MineAction exec = null; + for (MineAction breakAction : item.getBreakActions()) { + if (random < breakAction.getChance()) { + if (exec == null) { + exec = breakAction; + } else if (breakAction.getChance() < exec.getChance()) { //if the new action has a lower chance than the current one, replace it + exec = breakAction; + } + } + } + + if (exec != null) { //execute the action + exec.execute(e.getPlayer(), e.getBlock().getLocation()); + } } } } @@ -842,7 +855,7 @@ public void processBlockBreakEvent(final MineBlockBreakEvent event, final boolea private void processBlockBreakEvent(boolean reset) { if (reset) { //if mine reset percentage is lower, reset it - if (this.isResetBy(RMine.Reset.PERCENTAGE) & ((double) this.getRemainingBlocksPer() < this.getResetValue(RMine.Reset.PERCENTAGE))) { + if (this.isResetBy(Reset.PERCENTAGE) & ((double) this.getRemainingBlocksPer() < this.getResetValue(Reset.PERCENTAGE))) { this.kickPlayers(TranslatableLine.MINE_RESET_PERCENTAGE.get()); Bukkit.getScheduler().scheduleSyncDelayedTask(RealMinesAPI.getInstance().getPlugin(), this::reset, 10); } @@ -1127,23 +1140,17 @@ public String getName() { } public boolean isResetBy(final Reset e) { - switch (e) { - case PERCENTAGE: - return this.resetByPercentage; - case TIME: - return this.resetByTime; - } - return false; + return switch (e) { + case PERCENTAGE -> this.resetByPercentage; + case TIME -> this.resetByTime; + }; } public int getResetValue(final Reset e) { - switch (e) { - case PERCENTAGE: - return this.resetByPercentageValue; - case TIME: - return this.resetByTimeValue; - } - return -1; + return switch (e) { + case PERCENTAGE -> this.resetByPercentageValue; + case TIME -> this.resetByTimeValue; + }; } public void setSilent(boolean silent) { @@ -1235,7 +1242,7 @@ public Map getFaces() { return this.faces; } - public abstract RMine.Type getType(); + public abstract Type getType(); public abstract void clearContents(); diff --git a/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineAction.java b/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineAction.java index a5ca8f7..4101089 100644 --- a/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineAction.java +++ b/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineAction.java @@ -88,7 +88,7 @@ public void setChance(Double d) { this.chance = d; } - public abstract void execute(final Player p, final Location loc, double randomChance); + public abstract void execute(final Player p, final Location loc); public abstract MineActionType getType(); diff --git a/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineActionCommand.java b/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineActionCommand.java index b494554..c41cf5d 100644 --- a/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineActionCommand.java +++ b/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineActionCommand.java @@ -42,18 +42,16 @@ public MineActionCommand(final String mineID, final Double chance, final String this.command = command; } - public void execute(final Player p, final Location l, final double randomChance) { + public void execute(final Player p, final Location l) { if (super.getMine() == null) { return; } - if (randomChance < super.getChance()) { - String cmd2Exec = this.command.replace("%player%", p.getName()).replace("%blockloc%", Text.location2Command(l)); - if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { - cmd2Exec = PlaceholderAPI.setPlaceholders(p, cmd2Exec); - } - Bukkit.getServer().dispatchCommand(cmdSndr, cmd2Exec); + String cmd2Exec = this.command.replace("%player%", p.getName()).replace("%blockloc%", Text.location2Command(l)); + if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { + cmd2Exec = PlaceholderAPI.setPlaceholders(p, cmd2Exec); } + Bukkit.getServer().dispatchCommand(cmdSndr, cmd2Exec); } @Override diff --git a/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineActionDropItem.java b/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineActionDropItem.java index 1c7a54e..d82084f 100644 --- a/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineActionDropItem.java +++ b/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineActionDropItem.java @@ -42,17 +42,15 @@ public MineActionDropItem(final String mineID, final Double chance, final ItemSt this.i = i; } - public void execute(final Player p, final Location l, final double randomChance) { + public void execute(final Player p, final Location l) { if (super.getMine() == null) { return; } - if (randomChance < super.getChance()) { - Objects.requireNonNull(l.getWorld()).dropItemNaturally(l, this.i.clone()); + Objects.requireNonNull(l.getWorld()).dropItemNaturally(l, this.i.clone()); - if (!super.getMine().getSettingBool(RMineSettings.DISCARD_BREAK_ACTION_MESSAGES)) - TranslatableLine.MINE_BREAK_ACTION_DROP_ITEM.send(p); - } + if (!super.getMine().getSettingBool(RMineSettings.DISCARD_BREAK_ACTION_MESSAGES)) + TranslatableLine.MINE_BREAK_ACTION_DROP_ITEM.send(p); } @Override diff --git a/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineActionDummy.java b/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineActionDummy.java index 9dd799e..0e3aee5 100644 --- a/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineActionDummy.java +++ b/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineActionDummy.java @@ -25,7 +25,7 @@ public MineActionDummy() { super(); } - public void execute(final Player p, final Location l, double randomChance) { + public void execute(final Player p, final Location l) { } @Override diff --git a/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineActionGiveItem.java b/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineActionGiveItem.java index 00d0c2c..86a7c7b 100644 --- a/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineActionGiveItem.java +++ b/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineActionGiveItem.java @@ -41,19 +41,20 @@ public MineActionGiveItem(final String mineID, final Double chance, final ItemSt this.i = i; } - public void execute(final Player p, final Location l, final double randomChance) { + public void execute(final Player p, final Location l) { if (super.getMine() == null) { return; } - if (randomChance < super.getChance()) { - if (hasSpace(p, i)) { - p.getInventory().addItem(i); - if (!super.getMine().getSettingBool(RMineSettings.DISCARD_BREAK_ACTION_MESSAGES)) - TranslatableLine.MINE_BREAK_ACTION_GIVE_ITEM.send(p); - } else { - p.getWorld().dropItemNaturally(p.getLocation(), i); - Text.send(p, "&cYour inventory is full. &fThe give item has dropped!"); - } + + if (!super.getMine().getSettingBool(RMineSettings.DISCARD_BREAK_ACTION_MESSAGES)) { + TranslatableLine.MINE_BREAK_ACTION_GIVE_ITEM.send(p); + } + + if (hasSpace(p, i)) { + p.getInventory().addItem(i); + } else { + p.getWorld().dropItemNaturally(p.getLocation(), i); + Text.send(p, "&cYour inventory is full. &fThe item was dropped!"); } } diff --git a/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineActionMoney.java b/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineActionMoney.java index 2c76f19..d8f5132 100644 --- a/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineActionMoney.java +++ b/realmines-api/src/main/java/joserodpt/realmines/api/mine/components/actions/MineActionMoney.java @@ -41,18 +41,17 @@ public MineActionMoney(final String mineID, final Double chance, final Double mo this.money = money; } - public void execute(final Player p, final Location l, double randomChance) { + public void execute(final Player p, final Location l) { if (super.getMine() == null) { return; } - if (randomChance < super.getChance()) { - if (RealMinesAPI.getInstance().getEconomy() != null) { - RealMinesAPI.getInstance().getEconomy().depositPlayer(p, money); - if (!super.getMine().getSettingBool(RMineSettings.DISCARD_BREAK_ACTION_MESSAGES)) - TranslatableLine.MINE_BREAK_ACTION_DROP_ITEM.send(p); - } else { - RealMinesAPI.getInstance().getLogger().warning("Economy not found or Vault not installed. Please install a compatible economy plugin. Skipping break action ID " + getID()); - } + + if (RealMinesAPI.getInstance().getEconomy() != null) { + RealMinesAPI.getInstance().getEconomy().depositPlayer(p, money); + if (!super.getMine().getSettingBool(RMineSettings.DISCARD_BREAK_ACTION_MESSAGES)) + TranslatableLine.MINE_BREAK_ACTION_DROP_ITEM.send(p); + } else { + RealMinesAPI.getInstance().getLogger().warning("Economy not found or Vault not installed. Please install a compatible economy plugin. Skipping break action ID " + getID()); } }