dev/rework

This commit is contained in:
Len
2022-08-25 00:22:46 +02:00
parent 1ae73c5ba8
commit abf53fbbbe
28 changed files with 1204 additions and 297 deletions
@@ -11,6 +11,7 @@ import lombok.Setter;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
@@ -59,11 +60,11 @@ public class PlayerShop {
public PlayerShop(Location shopLocation, Location signLocation, UUID uuid, String playerName) {
this.shopID = UUID.randomUUID();
this.shopLocation = shopLocation;
this.signLocation = signLocation;
this.shopLocation = new Location(shopLocation.getWorld(), shopLocation.getBlockX(), shopLocation.getBlockY(), shopLocation.getBlockZ());
this.signLocation = new Location(signLocation.getWorld(), signLocation.getBlockX(), signLocation.getBlockY(), signLocation.getBlockZ());
this.ownerUUID = uuid;
this.ownerName = playerName;
updateSign();
this.server = Bukkit.getServerName();
}
public static PlayerShop load(UUID shopID, String ownerName, UUID ownerUUID, ShopType shopType, String server,
@@ -133,23 +134,29 @@ public class PlayerShop {
}
public void updateSign() {
if (!isInitialized()) {
setSignLines(type.getShopTypeConfig().inActiveSignLines);
} else {
setSignLines(type.getShopTypeConfig().activeSignLines);
}
}
public void removeSignLines() {
setSignLines(type.getShopTypeConfig().activeSignLines);
}
void setSignLines(List<String> signLines) {
new BukkitRunnable() {
public void run() {
Sign signBlock = (Sign) signLocation.getBlock().getState();
MiniMessage miniMessage = MiniMessage.miniMessage();
List<String> signLines;
TagResolver tagResolver = TagResolver.resolver(
Placeholder.unparsed("ownername", getOwnerName()),
Placeholder.unparsed("price", String.valueOf(getPrice())),
Placeholder.unparsed("amount", String.valueOf(getAmount()))
Placeholder.unparsed("amount", String.valueOf(getAmount())),
Placeholder.unparsed("item", ShopUtil.getItemName(getItemStack()))
);
if (!isInitialized()) {
signLines = type.getShopTypeConfig().inActiveSignLines;
} else {
signLines = type.getShopTypeConfig().activeSignLines;
}
for (int i = 0; i < 4; i++) {
signBlock.line(i, miniMessage.deserialize(signLines.get(i), tagResolver));
}
@@ -159,18 +166,18 @@ public class PlayerShop {
}
public double getPricePerItem() {
double pricePer = this.getPrice() / this.getAmount();
return this.getPrice() / this.getAmount();
}
public boolean removeBalance(double amount) {
ShopBalanceChangeEvent shopBalanceChangeEvent = new ShopBalanceChangeEvent(this, ShopBalanceChangeEvent.ChangeReason.WIDRAW);
ShopBalanceChangeEvent shopBalanceChangeEvent = new ShopBalanceChangeEvent(this, ShopBalanceChangeEvent.ChangeReason.WITHDRAW);
if (Util.callCancellableEvent(shopBalanceChangeEvent))
return false; // cancelled by another plugin, does this need logging?
setDirty(true);
update();
this.balance -= amount;
this.setLastTransaction(System.currentTimeMillis());
return true;
}
@@ -182,6 +189,7 @@ public class PlayerShop {
setDirty(true);
update();
this.balance += amount;
this.setLastTransaction(System.currentTimeMillis());
return true;
}
@@ -280,13 +288,14 @@ public class PlayerShop {
}
public void setItemStack(ItemStack itemStack) {
if (this.itemStack.equals(itemStack))
if (this.itemStack != null && this.itemStack.equals(itemStack))
return; // no changes have been made.
ShopItemChangeEvent shopItemChangeEvent = new ShopItemChangeEvent(this, itemStack);
if (Util.callCancellableEvent(shopItemChangeEvent))
return; // cancelled by another plugin, does this need logging?
this.itemStack = itemStack;
this.itemStack.setAmount(this.amount != 0 ? this.amount : 1);
setDirty(true);
update();
}
@@ -311,6 +320,8 @@ public class PlayerShop {
return; // cancelled by another plugin, does this need logging?
this.amount = amount;
if (this.itemStack != null)
this.itemStack.setAmount(this.amount);
setDirty(true);
update();
}
@@ -331,7 +342,11 @@ public class PlayerShop {
* Updates and saves the PlayerShop in the database
*/
private void update() {
PlayerShops.getInstance().getDatabaseHelper().updateShop(this, false);
if (!ShopUtil.isLoaded(signLocation))
return;
updateSign();
}
}
@@ -18,6 +18,18 @@ public enum ShopType {
return shopTypeConfig;
}
public static ShopType fromString(String name) {
if (name == null)
return ShopType.NONE;
for (ShopType shopType : ShopType.values()) {
if (name.equalsIgnoreCase(shopType.toString()))
return shopType;
}
return ShopType.NONE;
}
@Override
public String toString() {
return name().toLowerCase();