2019-12-16 16:47:51 +01:00
|
|
|
package world.bentobox.limits.objects;
|
2019-02-07 08:20:45 +01:00
|
|
|
|
2019-11-08 21:35:47 +01:00
|
|
|
import java.util.EnumMap;
|
2020-04-11 13:53:28 +02:00
|
|
|
import java.util.HashMap;
|
2019-02-07 08:20:45 +01:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import org.bukkit.Material;
|
2020-01-09 23:42:37 +01:00
|
|
|
import org.bukkit.entity.EntityType;
|
2019-02-07 08:20:45 +01:00
|
|
|
|
|
|
|
import com.google.gson.annotations.Expose;
|
|
|
|
|
|
|
|
import world.bentobox.bentobox.database.objects.DataObject;
|
2020-06-11 05:58:07 +02:00
|
|
|
import world.bentobox.bentobox.database.objects.Table;
|
2019-02-07 08:20:45 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author tastybento
|
|
|
|
*
|
|
|
|
*/
|
2020-06-11 05:58:07 +02:00
|
|
|
@Table(name = "IslandBlockCount")
|
2019-02-07 08:20:45 +01:00
|
|
|
public class IslandBlockCount implements DataObject {
|
|
|
|
|
|
|
|
@Expose
|
|
|
|
private String uniqueId = "";
|
|
|
|
|
2019-02-09 07:35:29 +01:00
|
|
|
@Expose
|
|
|
|
private String gameMode = "";
|
|
|
|
|
2019-02-07 08:20:45 +01:00
|
|
|
@Expose
|
2021-07-29 10:38:45 +02:00
|
|
|
private Map<Material, Integer> blockCounts = new EnumMap<>(Material.class);
|
2019-02-07 08:20:45 +01:00
|
|
|
|
2021-01-22 20:56:42 +01:00
|
|
|
private boolean changed;
|
|
|
|
|
2019-02-09 20:13:20 +01:00
|
|
|
/**
|
|
|
|
* Permission based limits
|
|
|
|
*/
|
2019-02-09 07:35:29 +01:00
|
|
|
@Expose
|
2019-11-08 21:35:47 +01:00
|
|
|
private Map<Material, Integer> blockLimits = new EnumMap<>(Material.class);
|
2020-01-09 23:42:37 +01:00
|
|
|
@Expose
|
|
|
|
private Map<EntityType, Integer> entityLimits = new EnumMap<>(EntityType.class);
|
2020-04-11 13:53:28 +02:00
|
|
|
@Expose
|
|
|
|
private Map<String, Integer> entityGroupLimits = new HashMap<>();
|
2019-02-09 07:35:29 +01:00
|
|
|
|
2019-02-08 04:39:27 +01:00
|
|
|
// Required for YAML database
|
|
|
|
public IslandBlockCount() {}
|
|
|
|
|
2021-03-07 18:20:28 +01:00
|
|
|
/**
|
|
|
|
* Create an island block count object
|
|
|
|
* @param islandId - unique Island ID string
|
|
|
|
* @param gameMode - Game mode name from gm.getDescription().getName()
|
|
|
|
*/
|
|
|
|
public IslandBlockCount(String islandId, String gameMode) {
|
|
|
|
this.uniqueId = islandId;
|
|
|
|
this.gameMode = gameMode;
|
2021-01-22 20:56:42 +01:00
|
|
|
setChanged();
|
2019-02-07 08:20:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see world.bentobox.bentobox.database.objects.DataObject#getUniqueId()
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String getUniqueId() {
|
|
|
|
return uniqueId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see world.bentobox.bentobox.database.objects.DataObject#setUniqueId(java.lang.String)
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void setUniqueId(String uniqueId) {
|
|
|
|
this.uniqueId = uniqueId;
|
2021-01-22 20:56:42 +01:00
|
|
|
setChanged();
|
2019-02-07 08:20:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the blockCount
|
|
|
|
*/
|
2021-07-29 10:38:45 +02:00
|
|
|
public Map<Material, Integer> getBlockCounts() {
|
|
|
|
return blockCounts;
|
2019-02-07 08:20:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-29 10:38:45 +02:00
|
|
|
* @param blockCounts the blockCount to set
|
2019-02-07 08:20:45 +01:00
|
|
|
*/
|
2021-07-29 10:38:45 +02:00
|
|
|
public void setBlockCounts(Map<Material, Integer> blockCounts) {
|
|
|
|
this.blockCounts = blockCounts;
|
2021-01-22 20:56:42 +01:00
|
|
|
setChanged();
|
2019-02-07 08:20:45 +01:00
|
|
|
}
|
|
|
|
|
2021-07-29 10:38:45 +02:00
|
|
|
/**
|
|
|
|
* Get the block count for this material for this island
|
|
|
|
* @param m - material
|
2021-07-30 11:16:28 +02:00
|
|
|
* @return count
|
2021-07-29 10:38:45 +02:00
|
|
|
*/
|
|
|
|
public Integer getBlockCount(Material m) {
|
2021-07-30 11:16:28 +02:00
|
|
|
return blockCounts.getOrDefault(m, 0);
|
2021-07-29 10:38:45 +02:00
|
|
|
}
|
|
|
|
|
2019-02-07 08:20:45 +01:00
|
|
|
/**
|
|
|
|
* Add a material to the count
|
|
|
|
* @param material - material
|
|
|
|
*/
|
|
|
|
public void add(Material material) {
|
2021-07-29 10:38:45 +02:00
|
|
|
blockCounts.merge(material, 1, Integer::sum);
|
2021-01-22 20:56:42 +01:00
|
|
|
setChanged();
|
2019-02-07 08:20:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a material from the count
|
|
|
|
* @param material - material
|
|
|
|
*/
|
|
|
|
public void remove(Material material) {
|
2021-07-29 10:38:45 +02:00
|
|
|
blockCounts.put(material, blockCounts.getOrDefault(material, 0) - 1);
|
|
|
|
blockCounts.values().removeIf(v -> v <= 0);
|
2021-01-22 20:56:42 +01:00
|
|
|
setChanged();
|
2019-02-07 08:20:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this material is at or over a limit
|
|
|
|
* @param material - block material
|
|
|
|
* @param limit - limit to check
|
|
|
|
* @return true if count is >= limit
|
|
|
|
*/
|
|
|
|
public boolean isAtLimit(Material material, int limit) {
|
2021-07-29 10:38:45 +02:00
|
|
|
return blockCounts.getOrDefault(material, 0) >= limit;
|
2019-02-07 08:20:45 +01:00
|
|
|
}
|
2019-02-09 07:35:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if no more of this material can be added to this island
|
|
|
|
* @param m - material
|
|
|
|
* @return true if no more material can be added
|
|
|
|
*/
|
|
|
|
public boolean isAtLimit(Material m) {
|
|
|
|
// Check island limits first
|
2021-07-29 10:38:45 +02:00
|
|
|
return blockLimits.containsKey(m) && blockCounts.getOrDefault(m, 0) >= blockLimits.get(m);
|
2019-02-09 07:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isBlockLimited(Material m) {
|
|
|
|
return blockLimits.containsKey(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the blockLimits
|
|
|
|
*/
|
|
|
|
public Map<Material, Integer> getBlockLimits() {
|
|
|
|
return blockLimits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param blockLimits the blockLimits to set
|
|
|
|
*/
|
|
|
|
public void setBlockLimits(Map<Material, Integer> blockLimits) {
|
|
|
|
this.blockLimits = blockLimits;
|
2021-01-22 20:56:42 +01:00
|
|
|
setChanged();
|
2019-02-09 07:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the block limit for this material for this island
|
|
|
|
* @param m - material
|
|
|
|
* @return limit or -1 for unlimited
|
|
|
|
*/
|
|
|
|
public Integer getBlockLimit(Material m) {
|
|
|
|
return blockLimits.getOrDefault(m, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the block limit for this material for this island
|
|
|
|
* @param m - material
|
|
|
|
* @param limit - maximum number allowed
|
|
|
|
*/
|
|
|
|
public void setBlockLimit(Material m, int limit) {
|
|
|
|
blockLimits.put(m, limit);
|
2021-01-22 20:56:42 +01:00
|
|
|
setChanged();
|
2019-02-09 07:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the gameMode
|
|
|
|
*/
|
|
|
|
public String getGameMode() {
|
|
|
|
return gameMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isGameMode(String gameMode) {
|
|
|
|
return this.gameMode.equals(gameMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param gameMode the gameMode to set
|
|
|
|
*/
|
|
|
|
public void setGameMode(String gameMode) {
|
|
|
|
this.gameMode = gameMode;
|
2021-01-22 20:56:42 +01:00
|
|
|
setChanged();
|
2019-02-09 07:35:29 +01:00
|
|
|
}
|
2020-01-09 23:42:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the entityLimits
|
|
|
|
*/
|
|
|
|
public Map<EntityType, Integer> getEntityLimits() {
|
|
|
|
return entityLimits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param entityLimits the entityLimits to set
|
|
|
|
*/
|
|
|
|
public void setEntityLimits(Map<EntityType, Integer> entityLimits) {
|
|
|
|
this.entityLimits = entityLimits;
|
2021-01-22 20:56:42 +01:00
|
|
|
setChanged();
|
2020-01-09 23:42:37 +01:00
|
|
|
}
|
2020-06-11 05:58:07 +02:00
|
|
|
|
2020-01-09 23:42:37 +01:00
|
|
|
/**
|
|
|
|
* Set an island-specific entity type limit
|
|
|
|
* @param t - entity type
|
|
|
|
* @param limit - limit
|
|
|
|
*/
|
|
|
|
public void setEntityLimit(EntityType t, int limit) {
|
|
|
|
entityLimits.put(t, limit);
|
2021-01-22 20:56:42 +01:00
|
|
|
setChanged();
|
2020-01-09 23:42:37 +01:00
|
|
|
}
|
2020-06-11 05:58:07 +02:00
|
|
|
|
2020-01-09 23:42:37 +01:00
|
|
|
/**
|
|
|
|
* Get the limit for an entity type
|
|
|
|
* @param t - entity type
|
|
|
|
* @return limit or -1 for unlimited
|
|
|
|
*/
|
|
|
|
public int getEntityLimit(EntityType t) {
|
|
|
|
return entityLimits.getOrDefault(t, -1);
|
|
|
|
}
|
2020-06-11 05:58:07 +02:00
|
|
|
|
2020-01-09 23:42:37 +01:00
|
|
|
/**
|
|
|
|
* Clear all island-specific entity type limits
|
|
|
|
*/
|
|
|
|
public void clearEntityLimits() {
|
|
|
|
entityLimits.clear();
|
2021-01-22 20:56:42 +01:00
|
|
|
setChanged();
|
2020-01-09 23:42:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-11 13:53:28 +02:00
|
|
|
/**
|
|
|
|
* @return the entityGroupLimits
|
|
|
|
*/
|
|
|
|
public Map<String, Integer> getEntityGroupLimits() {
|
|
|
|
return entityGroupLimits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param entityGroupLimits the entityGroupLimits to set
|
|
|
|
*/
|
|
|
|
public void setEntityGroupLimits(Map<String, Integer> entityGroupLimits) {
|
|
|
|
this.entityGroupLimits = entityGroupLimits;
|
2021-01-22 20:56:42 +01:00
|
|
|
setChanged();
|
2020-04-11 13:53:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set an island-specific entity group limit
|
|
|
|
* @param name - entity group
|
|
|
|
* @param limit - limit
|
|
|
|
*/
|
|
|
|
public void setEntityGroupLimit(String name, int limit) {
|
|
|
|
entityGroupLimits.put(name, limit);
|
2021-01-22 20:56:42 +01:00
|
|
|
setChanged();
|
2020-04-11 13:53:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the limit for an entity group
|
|
|
|
* @param name - entity group
|
|
|
|
* @return limit or -1 for unlimited
|
|
|
|
*/
|
|
|
|
public int getEntityGroupLimit(String name) {
|
|
|
|
return entityGroupLimits.getOrDefault(name, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear all island-specific entity group limits
|
|
|
|
*/
|
|
|
|
public void clearEntityGroupLimits() {
|
|
|
|
entityGroupLimits.clear();
|
2021-01-22 20:56:42 +01:00
|
|
|
setChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the changed
|
|
|
|
*/
|
|
|
|
public boolean isChanged() {
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param changed the changed to set
|
|
|
|
*/
|
|
|
|
public void setChanged(boolean changed) {
|
|
|
|
this.changed = changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark changed
|
|
|
|
*/
|
|
|
|
public void setChanged() {
|
|
|
|
this.changed = true;
|
2020-04-11 13:53:28 +02:00
|
|
|
}
|
2019-02-07 08:20:45 +01:00
|
|
|
}
|