mirror of
https://github.com/TotalFreedomMC/TF-EssentialsX.git
synced 2025-02-11 20:00:46 +00:00
Add support for spawners with entities on 1.13+
This commit is contained in:
parent
7e1d258dd3
commit
4adb669cdd
1 changed files with 55 additions and 19 deletions
|
@ -8,11 +8,11 @@ import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
import net.ess3.api.IEssentials;
|
import net.ess3.api.IEssentials;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.entity.EntityType;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.Damageable;
|
import org.bukkit.inventory.meta.Damageable;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
import org.bukkit.inventory.meta.PotionMeta;
|
import org.bukkit.inventory.meta.PotionMeta;
|
||||||
import org.bukkit.inventory.meta.Repairable;
|
|
||||||
import org.bukkit.potion.PotionData;
|
import org.bukkit.potion.PotionData;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
@ -24,10 +24,8 @@ import static com.earth2me.essentials.I18n.tl;
|
||||||
|
|
||||||
public class FlatItemDb extends AbstractItemDb {
|
public class FlatItemDb extends AbstractItemDb {
|
||||||
protected static final Logger LOGGER = Logger.getLogger("Essentials");
|
protected static final Logger LOGGER = Logger.getLogger("Essentials");
|
||||||
private final transient IEssentials ess;
|
|
||||||
|
|
||||||
private static Gson gson = new Gson();
|
private static Gson gson = new Gson();
|
||||||
|
private final transient IEssentials ess;
|
||||||
// Maps primary name to ItemData
|
// Maps primary name to ItemData
|
||||||
private final transient Map<String, ItemData> items = new HashMap<>();
|
private final transient Map<String, ItemData> items = new HashMap<>();
|
||||||
|
|
||||||
|
@ -57,8 +55,8 @@ public class FlatItemDb extends AbstractItemDb {
|
||||||
this.reset();
|
this.reset();
|
||||||
|
|
||||||
String json = file.getLines().stream()
|
String json = file.getLines().stream()
|
||||||
.filter(line -> !line.startsWith("#"))
|
.filter(line -> !line.startsWith("#"))
|
||||||
.collect(Collectors.joining());
|
.collect(Collectors.joining());
|
||||||
|
|
||||||
this.loadJSON(String.join("\n", json));
|
this.loadJSON(String.join("\n", json));
|
||||||
}
|
}
|
||||||
|
@ -108,12 +106,12 @@ public class FlatItemDb extends AbstractItemDb {
|
||||||
throw new Exception(tl("unknownItemName", id));
|
throw new Exception(tl("unknownItemName", id));
|
||||||
}
|
}
|
||||||
|
|
||||||
PotionData potionData = data.getPotionData();
|
|
||||||
Material material = data.getMaterial();
|
Material material = data.getMaterial();
|
||||||
|
|
||||||
ItemStack stack = new ItemStack(material);
|
ItemStack stack = new ItemStack(material);
|
||||||
stack.setAmount(material.getMaxStackSize());
|
stack.setAmount(material.getMaxStackSize());
|
||||||
|
|
||||||
|
PotionData potionData = data.getPotionData();
|
||||||
ItemMeta meta = stack.getItemMeta();
|
ItemMeta meta = stack.getItemMeta();
|
||||||
|
|
||||||
if (potionData != null && meta instanceof PotionMeta) {
|
if (potionData != null && meta instanceof PotionMeta) {
|
||||||
|
@ -130,6 +128,13 @@ public class FlatItemDb extends AbstractItemDb {
|
||||||
|
|
||||||
stack.setItemMeta(meta);
|
stack.setItemMeta(meta);
|
||||||
|
|
||||||
|
// The spawner provider will update the meta again, so we need to call it after
|
||||||
|
// setItemMeta to prevent a race condition
|
||||||
|
EntityType entity = data.getEntity();
|
||||||
|
if (entity != null && material.toString().contains("SPAWNER")) {
|
||||||
|
ess.getSpawnerProvider().setEntityType(stack, entity);
|
||||||
|
}
|
||||||
|
|
||||||
return stack;
|
return stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,14 +166,7 @@ public class FlatItemDb extends AbstractItemDb {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String name(ItemStack item) {
|
public String name(ItemStack item) {
|
||||||
Material type = item.getType();
|
ItemData data = lookup(item);
|
||||||
PotionData potion = null;
|
|
||||||
|
|
||||||
if (MaterialUtil.isPotion(type) && item.getItemMeta() instanceof PotionMeta) {
|
|
||||||
potion = ((PotionMeta) item.getItemMeta()).getBasePotionData();
|
|
||||||
}
|
|
||||||
|
|
||||||
ItemData data = new ItemData(type, potion);
|
|
||||||
|
|
||||||
for (Map.Entry<String, ItemData> entry : items.entrySet()) {
|
for (Map.Entry<String, ItemData> entry : items.entrySet()) {
|
||||||
if (entry.getValue().equals(data)) {
|
if (entry.getValue().equals(data)) {
|
||||||
|
@ -185,20 +183,44 @@ public class FlatItemDb extends AbstractItemDb {
|
||||||
throw new UnsupportedOperationException("Legacy IDs aren't supported on this version.");
|
throw new UnsupportedOperationException("Legacy IDs aren't supported on this version.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ItemData lookup(ItemStack item) {
|
||||||
|
Material type = item.getType();
|
||||||
|
|
||||||
|
if (MaterialUtil.isPotion(type) && item.getItemMeta() instanceof PotionMeta) {
|
||||||
|
PotionData potion = ((PotionMeta) item.getItemMeta()).getBasePotionData();
|
||||||
|
return new ItemData(type, potion);
|
||||||
|
} else if (type.toString().contains("SPAWNER")) {
|
||||||
|
EntityType entity = ess.getSpawnerProvider().getEntityType(item);
|
||||||
|
return new ItemData(type, entity);
|
||||||
|
} else {
|
||||||
|
return new ItemData(type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<String> listNames() {
|
public Collection<String> listNames() {
|
||||||
return Collections.unmodifiableSet(allAliases);
|
return Collections.unmodifiableSet(allAliases);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class ItemData {
|
public static class ItemData {
|
||||||
private Material material;
|
private final Material material;
|
||||||
private PotionData potionData;
|
private PotionData potionData = null;
|
||||||
|
private EntityType entity = null;
|
||||||
|
|
||||||
|
public ItemData(Material material) {
|
||||||
|
this.material = material;
|
||||||
|
}
|
||||||
|
|
||||||
public ItemData(Material material, PotionData potionData) {
|
public ItemData(Material material, PotionData potionData) {
|
||||||
this.material = material;
|
this.material = material;
|
||||||
this.potionData = potionData;
|
this.potionData = potionData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ItemData(Material material, EntityType entity) {
|
||||||
|
this.material = material;
|
||||||
|
this.entity = entity;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return (31 * material.hashCode()) ^ potionData.hashCode();
|
return (31 * material.hashCode()) ^ potionData.hashCode();
|
||||||
|
@ -213,7 +235,7 @@ public class FlatItemDb extends AbstractItemDb {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
ItemData that = (ItemData) o;
|
ItemData that = (ItemData) o;
|
||||||
return this.material == that.getMaterial() && potionDataEquals(that);
|
return this.material == that.getMaterial() && potionDataEquals(that) && entityEquals(that);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Material getMaterial() {
|
public Material getMaterial() {
|
||||||
|
@ -224,6 +246,10 @@ public class FlatItemDb extends AbstractItemDb {
|
||||||
return this.potionData;
|
return this.potionData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public EntityType getEntity() {
|
||||||
|
return this.entity;
|
||||||
|
}
|
||||||
|
|
||||||
private boolean potionDataEquals(ItemData o) {
|
private boolean potionDataEquals(ItemData o) {
|
||||||
if (this.potionData == null && o.getPotionData() == null) {
|
if (this.potionData == null && o.getPotionData() == null) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -233,5 +259,15 @@ public class FlatItemDb extends AbstractItemDb {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean entityEquals(ItemData o) {
|
||||||
|
if (this.entity == null && o.getEntity() == null) { // neither have an entity
|
||||||
|
return true;
|
||||||
|
} else if (this.entity != null && o.getEntity() != null) { // both have an entity; check if it's the same one
|
||||||
|
return this.entity.equals(o.getEntity());
|
||||||
|
} else { // one has an entity but the other doesn't, so they can't be equal
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue