Adds proper support for dual-side paid signs
This commit is contained in:
parent
35a5e40eeb
commit
396bcfc68b
2
pom.xml
2
pom.xml
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<groupId>net.knarcraft</groupId>
|
<groupId>net.knarcraft</groupId>
|
||||||
<artifactId>paidsigns</artifactId>
|
<artifactId>paidsigns</artifactId>
|
||||||
<version>1.0.2</version>
|
<version>1.0.3-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>Paid Signs</name>
|
<name>Paid Signs</name>
|
||||||
|
@ -1,13 +1,45 @@
|
|||||||
package net.knarcraft.paidsigns.container;
|
package net.knarcraft.paidsigns.container;
|
||||||
|
|
||||||
|
import org.bukkit.block.sign.Side;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A representation of a sign placed by a player that matched a paid sign
|
* A representation of a sign placed by a player that matched a paid sign
|
||||||
*
|
*
|
||||||
* @param playerId <p>The unique id of the player that created the sign</p>
|
* @param playerId <p>The unique id of the player that created the sign</p>
|
||||||
* @param cost <p>The cost the player paid for creating the sign</p>
|
* @param frontCost <p>The cost the player paid for creating the front of the sign</p>
|
||||||
|
* @param backCost <p>The cost the player paid for creating the back of the sign</p>
|
||||||
*/
|
*/
|
||||||
public record TrackedSign(UUID playerId, double cost) {
|
public record TrackedSign(UUID playerId, double frontCost, double backCost) {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an instance of a tracked sign
|
||||||
|
*
|
||||||
|
* @param playerId <p>The unique id of the player that created the sign</p>
|
||||||
|
* @param side <p>The side the player paid for</p>
|
||||||
|
* @param cost <p>The cost the player paid for creating the side of the sign</p>
|
||||||
|
* @return <p>A tracked sign instance</p>
|
||||||
|
*/
|
||||||
|
public static TrackedSign getInstance(UUID playerId, Side side, double cost) {
|
||||||
|
if (side == Side.FRONT) {
|
||||||
|
return new TrackedSign(playerId, cost, 0);
|
||||||
|
} else {
|
||||||
|
return new TrackedSign(playerId, 0, cost);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the cost paid for the given side
|
||||||
|
*
|
||||||
|
* @param side <p>The side to check the cost for</p>
|
||||||
|
* @return <p>The cost the player paid</p>
|
||||||
|
*/
|
||||||
|
public double getCost(Side side) {
|
||||||
|
return switch (side) {
|
||||||
|
case FRONT -> frontCost;
|
||||||
|
case BACK -> backCost;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,8 @@ public class SignListener implements Listener {
|
|||||||
// As signs can be edited now, this event might be triggered on an already registered sign, so unregister
|
// As signs can be edited now, this event might be triggered on an already registered sign, so unregister
|
||||||
if (SignHelper.isSign(event.getBlock())) {
|
if (SignHelper.isSign(event.getBlock())) {
|
||||||
try {
|
try {
|
||||||
TrackedSignManager.removeTrackedSign(event.getBlock().getLocation(), true, false);
|
TrackedSignManager.removeTrackedSign(event.getBlock().getLocation(), true, false,
|
||||||
|
event.getSide());
|
||||||
} catch (IOException ignored) {
|
} catch (IOException ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -164,7 +165,7 @@ public class SignListener implements Listener {
|
|||||||
EconomyManager.withdraw(player, cost);
|
EconomyManager.withdraw(player, cost);
|
||||||
PaidSigns.getStringFormatter().displaySuccessMessage(player, replaceCost(cost, PaidSignsTranslatableMessage.SUCCESS_PAID_FOR_SIGN));
|
PaidSigns.getStringFormatter().displaySuccessMessage(player, replaceCost(cost, PaidSignsTranslatableMessage.SUCCESS_PAID_FOR_SIGN));
|
||||||
try {
|
try {
|
||||||
TrackedSignManager.addTrackedSign(event.getBlock().getLocation(), player.getUniqueId(), cost);
|
TrackedSignManager.addTrackedSign(event.getBlock().getLocation(), player.getUniqueId(), cost, event.getSide());
|
||||||
} catch (IOException ignored) {
|
} catch (IOException ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ import net.knarcraft.paidsigns.utility.SignHelper;
|
|||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.OfflinePlayer;
|
import org.bukkit.OfflinePlayer;
|
||||||
|
import org.bukkit.block.sign.Side;
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
@ -36,13 +37,72 @@ public final class TrackedSignManager {
|
|||||||
*
|
*
|
||||||
* @param signLocation <p>The location the sign was created at</p>
|
* @param signLocation <p>The location the sign was created at</p>
|
||||||
* @param playerId <p>The unique id of the player that created the sign</p>
|
* @param playerId <p>The unique id of the player that created the sign</p>
|
||||||
|
* @param cost <p>The cost the player paid</p>
|
||||||
|
* @param side <p>The side of the sign the player paid for</p>
|
||||||
* @throws IOException <p>If unable to save the tracked signs</p>
|
* @throws IOException <p>If unable to save the tracked signs</p>
|
||||||
*/
|
*/
|
||||||
public static void addTrackedSign(Location signLocation, UUID playerId, double cost) throws IOException {
|
public static void addTrackedSign(Location signLocation, UUID playerId, double cost, Side side) throws IOException {
|
||||||
trackedSigns.put(signLocation, new TrackedSign(playerId, cost));
|
if (trackedSigns.containsKey(signLocation)) {
|
||||||
|
// If there is already a tracked sign, overwrite it, while updating only the correct cost
|
||||||
|
TrackedSign oldSign = trackedSigns.get(signLocation);
|
||||||
|
if (side == Side.FRONT) {
|
||||||
|
trackedSigns.put(signLocation, new TrackedSign(playerId, cost, oldSign.backCost()));
|
||||||
|
} else {
|
||||||
|
trackedSigns.put(signLocation, new TrackedSign(playerId, oldSign.frontCost(), cost));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
trackedSigns.put(signLocation, TrackedSign.getInstance(playerId, side, cost));
|
||||||
|
}
|
||||||
saveTrackedSigns();
|
saveTrackedSigns();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a tracked sign from the manager
|
||||||
|
*
|
||||||
|
* @param signLocation <p>The location the sign was removed from</p>
|
||||||
|
* @param refund <p>Whether to perform a refund after un-tracking the sign</p>
|
||||||
|
* @param forceRefund <p>Whether to force a refund, even if refunding is disabled</p>
|
||||||
|
* @param side <p>The side of the sign to un-track</p>
|
||||||
|
* @throws IOException <p>If unable to save the tracked signs</p>
|
||||||
|
*/
|
||||||
|
public static void removeTrackedSign(Location signLocation, boolean refund, boolean forceRefund,
|
||||||
|
Side side) throws IOException {
|
||||||
|
if (!trackedSigns.containsKey(signLocation)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
TrackedSign trackedSign = trackedSigns.get(signLocation);
|
||||||
|
|
||||||
|
Side oppositeSide = getOppositeSide(side);
|
||||||
|
double oppositeCost = trackedSign.getCost(oppositeSide);
|
||||||
|
boolean isInUse = oppositeCost > 0;
|
||||||
|
|
||||||
|
if (!isInUse) {
|
||||||
|
trackedSigns.remove(signLocation);
|
||||||
|
} else {
|
||||||
|
// If the opposite side has a cost, create a new sign with only that cost
|
||||||
|
trackedSigns.put(signLocation, TrackedSign.getInstance(trackedSign.playerId(), oppositeSide, oppositeCost));
|
||||||
|
}
|
||||||
|
|
||||||
|
saveTrackedSigns();
|
||||||
|
if (trackedSign.getCost(side) > 0) {
|
||||||
|
refund(trackedSign, refund, forceRefund, side);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the opposite sign side of the given side
|
||||||
|
*
|
||||||
|
* @param side <p>The side to get the opposite of</p>
|
||||||
|
* @return <p>The opposite sign side</p>
|
||||||
|
*/
|
||||||
|
private static Side getOppositeSide(Side side) {
|
||||||
|
if (side == Side.FRONT) {
|
||||||
|
return Side.BACK;
|
||||||
|
} else {
|
||||||
|
return Side.FRONT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes a tracked sign from the manager
|
* Removes a tracked sign from the manager
|
||||||
*
|
*
|
||||||
@ -58,7 +118,7 @@ public final class TrackedSignManager {
|
|||||||
TrackedSign trackedSign = trackedSigns.get(signLocation);
|
TrackedSign trackedSign = trackedSigns.get(signLocation);
|
||||||
trackedSigns.remove(signLocation);
|
trackedSigns.remove(signLocation);
|
||||||
saveTrackedSigns();
|
saveTrackedSigns();
|
||||||
refund(trackedSign, refund, forceRefund);
|
refundBothSides(trackedSign, refund, forceRefund);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -105,16 +165,27 @@ public final class TrackedSignManager {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
double cost = signSection.getDouble(key + ".cost");
|
double frontCost;
|
||||||
|
double backCost = 0;
|
||||||
|
if (signSection.contains(key + ".cost")) {
|
||||||
|
// Migrate from single side cost to two side cost
|
||||||
|
frontCost = signSection.getDouble(key + ".cost");
|
||||||
|
signSection.set(key + ".cost", null);
|
||||||
|
} else {
|
||||||
|
frontCost = signSection.getDouble(key + ".frontCost");
|
||||||
|
}
|
||||||
|
if (signSection.contains(key + ".backCost")) {
|
||||||
|
backCost = signSection.getDouble(key + ".backCost");
|
||||||
|
}
|
||||||
UUID playerId = UUID.fromString(Objects.requireNonNull(signSection.getString(key + ".playerId")));
|
UUID playerId = UUID.fromString(Objects.requireNonNull(signSection.getString(key + ".playerId")));
|
||||||
TrackedSign trackedSign = new TrackedSign(playerId, cost);
|
TrackedSign trackedSign = new TrackedSign(playerId, frontCost, backCost);
|
||||||
|
|
||||||
//Prevent destroyed signs from being tracked indefinitely
|
//Prevent destroyed signs from being tracked indefinitely
|
||||||
if (!SignHelper.isSign(signLocation.getBlock())) {
|
if (!SignHelper.isSign(signLocation.getBlock())) {
|
||||||
Bukkit.getScheduler().scheduleSyncDelayedTask(PaidSigns.getInstance(), () -> {
|
Bukkit.getScheduler().scheduleSyncDelayedTask(PaidSigns.getInstance(), () -> {
|
||||||
PaidSigns.getInstance().getLogger().log(Level.WARNING, "The sign at " + signLocation +
|
PaidSigns.getInstance().getLogger().log(Level.WARNING, "The sign at " + signLocation +
|
||||||
" no longer exists. Removing from sign tracker. Refunding the player.");
|
" no longer exists. Removing from sign tracker. Refunding the player.");
|
||||||
refund(trackedSign, true, false);
|
refundBothSides(trackedSign, true, false);
|
||||||
}, 100);
|
}, 100);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -135,7 +206,8 @@ public final class TrackedSignManager {
|
|||||||
TrackedSign sign = trackedSigns.get(signLocation);
|
TrackedSign sign = trackedSigns.get(signLocation);
|
||||||
String locationString = Objects.requireNonNull(signLocation.getWorld()).getUID() + "," +
|
String locationString = Objects.requireNonNull(signLocation.getWorld()).getUID() + "," +
|
||||||
signLocation.getBlockX() + "," + signLocation.getBlockY() + "," + signLocation.getBlockZ();
|
signLocation.getBlockX() + "," + signLocation.getBlockY() + "," + signLocation.getBlockZ();
|
||||||
signSection.set(locationString + ".cost", sign.cost());
|
signSection.set(locationString + ".frontCost", sign.frontCost());
|
||||||
|
signSection.set(locationString + ".backCost", sign.backCost());
|
||||||
signSection.set(locationString + ".playerId", sign.playerId().toString());
|
signSection.set(locationString + ".playerId", sign.playerId().toString());
|
||||||
}
|
}
|
||||||
configuration.save(signsFile);
|
configuration.save(signsFile);
|
||||||
@ -148,17 +220,18 @@ public final class TrackedSignManager {
|
|||||||
* @param refund <p>Whether to actually refund</p>
|
* @param refund <p>Whether to actually refund</p>
|
||||||
* @param forceRefund <p>Whether to force a refund, even if refunding is disabled</p>
|
* @param forceRefund <p>Whether to force a refund, even if refunding is disabled</p>
|
||||||
*/
|
*/
|
||||||
private static void refund(TrackedSign trackedSign, boolean refund, boolean forceRefund) {
|
private static void refund(TrackedSign trackedSign, boolean refund, boolean forceRefund, Side signSide) {
|
||||||
if ((!PaidSigns.getInstance().areRefundsEnabled() || !refund) && !forceRefund) {
|
if ((!PaidSigns.getInstance().areRefundsEnabled() || !refund) && !forceRefund) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
double cost = trackedSign.getCost(signSide);
|
||||||
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(trackedSign.playerId());
|
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(trackedSign.playerId());
|
||||||
double refundSum;
|
double refundSum;
|
||||||
if (forceRefund) {
|
if (forceRefund) {
|
||||||
//In the case where a refund is forced, the normal refund rate should not apply
|
//In the case where a refund is forced, the normal refund rate should not apply
|
||||||
refundSum = trackedSign.cost();
|
refundSum = cost;
|
||||||
} else {
|
} else {
|
||||||
refundSum = trackedSign.cost() / 100 * PaidSigns.getInstance().getRefundPercentage();
|
refundSum = cost / 100 * PaidSigns.getInstance().getRefundPercentage();
|
||||||
}
|
}
|
||||||
EconomyManager.deposit(offlinePlayer, refundSum);
|
EconomyManager.deposit(offlinePlayer, refundSum);
|
||||||
if (offlinePlayer instanceof Player player) {
|
if (offlinePlayer instanceof Player player) {
|
||||||
@ -168,4 +241,20 @@ public final class TrackedSignManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refunds the costs for both sides of a paid sign
|
||||||
|
*
|
||||||
|
* @param trackedSign <p>The tracked sign to refund for</p>
|
||||||
|
* @param refund <p>Whether to perform a refund after un-tracking the sign</p>
|
||||||
|
* @param forceRefund <p>Whether to force a refund, even if refunding is disabled</p>
|
||||||
|
*/
|
||||||
|
private static void refundBothSides(TrackedSign trackedSign, boolean refund, boolean forceRefund) {
|
||||||
|
if (trackedSign.frontCost() > 0) {
|
||||||
|
refund(trackedSign, refund, forceRefund, Side.FRONT);
|
||||||
|
}
|
||||||
|
if (trackedSign.backCost() > 0) {
|
||||||
|
refund(trackedSign, refund, forceRefund, Side.BACK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user