UltimateStacker/UltimateStacker-Plugin/src/main/java/com.craftaro.ultimatestacker/stackable/block/BlockStackImpl.java

154 lines
3.9 KiB
Java
Raw Normal View History

2023-05-25 19:20:03 +02:00
package com.craftaro.ultimatestacker.stackable.block;
2020-08-25 01:01:11 +02:00
import com.craftaro.core.database.Data;
import com.craftaro.core.database.SerializedLocation;
2024-01-07 12:16:27 +01:00
import com.craftaro.third_party.com.cryptomorin.xseries.XMaterial;
import com.craftaro.core.utils.TextUtils;
2023-05-25 19:20:03 +02:00
import com.craftaro.ultimatestacker.UltimateStacker;
import com.craftaro.ultimatestacker.api.stack.block.BlockStack;
2023-05-30 11:21:46 +02:00
import com.craftaro.ultimatestacker.settings.Settings;
2020-08-25 01:01:11 +02:00
import org.bukkit.Location;
import java.util.HashMap;
import java.util.Map;
2021-12-22 23:11:16 +01:00
import java.util.UUID;
2023-05-25 19:20:03 +02:00
public class BlockStackImpl implements BlockStack {
2020-08-25 01:01:11 +02:00
2021-12-22 23:11:16 +01:00
// This is the unique identifier for this stack.
// It is reset on every plugin load.
// Used for holograms.
private final UUID uniqueId = UUID.randomUUID();
2020-08-25 01:01:11 +02:00
// The id that identifies this stack in the database.
private int id;
2023-04-07 18:58:21 +02:00
private int amount = 1;
2023-06-29 11:18:18 +02:00
private XMaterial material;
private Location location;
2020-08-25 01:01:11 +02:00
/**
* Required for deserialization
*/
public BlockStackImpl() {
2020-08-25 01:01:11 +02:00
}
public BlockStackImpl(XMaterial material, Location location) {
2020-08-25 01:01:11 +02:00
this.material = material;
this.location = location;
this.id = UltimateStacker.getInstance().getDataManager().getNextId("blocks");
2020-08-25 01:01:11 +02:00
}
2023-05-25 19:20:03 +02:00
@Override
public int getId() {
return id;
}
@Override
public Map<String, Object> serialize() {
Map<String, Object> map = new HashMap<>();
map.put("id", id);
map.put("amount", amount);
map.put("material", material.name());
map.putAll(SerializedLocation.of(location));
return map;
}
@Override
public Data deserialize(Map<String, Object> map) {
id = (int) map.get("id");
amount = (int) map.get("amount");
2023-06-29 11:18:18 +02:00
material = XMaterial.valueOf((String) map.get("material"));
location = SerializedLocation.of(map);
return this;
}
@Override
public String getTableName() {
return "blocks";
}
2023-05-25 19:20:03 +02:00
public void setId(int id) {
this.id = id;
}
2020-08-25 01:01:11 +02:00
@Override
public int getAmount() {
return amount;
}
2023-05-25 19:20:03 +02:00
@Override
public void setAmount(int amount) {
if (amount < 1) {
destroy();
return;
}
this.amount = amount;
}
2020-08-26 15:40:51 +02:00
@Override
public boolean isValid() {
2023-06-29 11:18:18 +02:00
return XMaterial.matchXMaterial(location.getBlock().getType().name()).get() == material;
2020-08-26 15:40:51 +02:00
}
2023-05-25 19:20:03 +02:00
@Override
2020-08-25 01:01:11 +02:00
public void add(int amount) {
this.amount = this.amount + amount;
}
2023-05-25 19:20:03 +02:00
@Override
2020-08-25 01:01:11 +02:00
public void take(int amount) {
this.amount = this.amount - amount;
}
2023-05-25 19:20:03 +02:00
@Override
2020-08-25 01:01:11 +02:00
public void destroy() {
amount = 0;
UltimateStacker plugin = UltimateStacker.getInstance();
plugin.getBlockStackManager().removeBlock(location);
plugin.removeHologram(this);
plugin.getPluginDataManager().delete(this);
2020-08-25 01:01:11 +02:00
}
@Override
public Location getLocation() {
2023-07-08 11:19:06 +02:00
return this.location.clone();
2020-08-25 01:01:11 +02:00
}
2023-05-25 19:20:03 +02:00
@Override
2023-06-29 11:18:18 +02:00
public XMaterial getMaterial() {
2020-08-25 01:01:11 +02:00
return material;
}
@Override
public String getHologramName() {
String nameFormat = Settings.NAME_FORMAT_BLOCK.getString();
2021-03-05 15:09:00 +01:00
String displayName = TextUtils.formatText(material.name().toLowerCase().replace("_", " "), true);
2020-08-25 01:01:11 +02:00
nameFormat = nameFormat.replace("{TYPE}", displayName);
nameFormat = nameFormat.replace("{AMT}", Integer.toString(amount));
2021-03-05 15:09:00 +01:00
return TextUtils.formatText(nameFormat).trim();
2020-08-25 01:01:11 +02:00
}
@Override
public boolean areHologramsEnabled() {
return Settings.BLOCK_HOLOGRAMS.getBoolean();
}
2021-12-22 23:11:16 +01:00
@Override
public String getHologramId() {
return "UltimateStacker-" + uniqueId;
}
2020-08-25 01:01:11 +02:00
@Override
public String toString() {
2023-05-25 19:20:03 +02:00
return "BlockStackImpl{" +
2020-08-25 01:01:11 +02:00
"id=" + id +
", amount=" + amount +
", material=" + material +
", location=" + location +
'}';
}
}