Skip to content

Commit

Permalink
Fixes issue #65. If a block has multiple break actions, only the one …
Browse files Browse the repository at this point in the history
…with the lowest chance will execute.
  • Loading branch information
joserodpt committed Jan 17, 2025
1 parent 9febc8e commit 87c9dd8
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 51 deletions.
41 changes: 24 additions & 17 deletions realmines-api/src/main/java/joserodpt/realmines/api/mine/RMine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -1235,7 +1242,7 @@ public Map<MineCuboid.CuboidDirection, Material> getFaces() {
return this.faces;
}

public abstract RMine.Type getType();
public abstract Type getType();

public abstract void clearContents();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down

0 comments on commit 87c9dd8

Please sign in to comment.