|
| 1 | +package com.laytonsmith.abstraction.bukkit; |
| 2 | + |
| 3 | +import com.laytonsmith.abstraction.MCTagContainer; |
| 4 | +import com.laytonsmith.abstraction.enums.MCTagType; |
| 5 | +import com.laytonsmith.commandhelper.CommandHelperPlugin; |
| 6 | +import org.bukkit.NamespacedKey; |
| 7 | +import org.bukkit.persistence.PersistentDataContainer; |
| 8 | +import org.bukkit.persistence.PersistentDataType; |
| 9 | + |
| 10 | +import java.util.Collection; |
| 11 | + |
| 12 | +public class BukkitMCTagContainer implements MCTagContainer { |
| 13 | + |
| 14 | + PersistentDataContainer pdc; |
| 15 | + |
| 16 | + public BukkitMCTagContainer(PersistentDataContainer pdc) { |
| 17 | + this.pdc = pdc; |
| 18 | + } |
| 19 | + |
| 20 | + @Override |
| 21 | + public MCTagContainer newContainer() { |
| 22 | + return new BukkitMCTagContainer(pdc.getAdapterContext().newPersistentDataContainer()); |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public boolean isEmpty() { |
| 27 | + return this.pdc.isEmpty(); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public Collection getKeys() { |
| 32 | + return pdc.getKeys(); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public MCTagType getType(Object key) { |
| 37 | + NamespacedKey namespacedKey = NamespacedKey(key); |
| 38 | + // Check tag types in order of most frequently used |
| 39 | + if(pdc.has(namespacedKey, PersistentDataType.STRING)) { |
| 40 | + return MCTagType.STRING; |
| 41 | + } else if(pdc.has(namespacedKey, PersistentDataType.INTEGER)) { |
| 42 | + return MCTagType.INTEGER; |
| 43 | + } else if(pdc.has(namespacedKey, PersistentDataType.BYTE)) { |
| 44 | + return MCTagType.BYTE; |
| 45 | + } else if(pdc.has(namespacedKey, PersistentDataType.DOUBLE)) { |
| 46 | + return MCTagType.DOUBLE; |
| 47 | + } else if(pdc.has(namespacedKey, PersistentDataType.LONG)) { |
| 48 | + return MCTagType.LONG; |
| 49 | + } else if(pdc.has(namespacedKey, PersistentDataType.FLOAT)) { |
| 50 | + return MCTagType.FLOAT; |
| 51 | + } else if(pdc.has(namespacedKey, PersistentDataType.TAG_CONTAINER)) { |
| 52 | + return MCTagType.TAG_CONTAINER; |
| 53 | + } else if(pdc.has(namespacedKey, PersistentDataType.BYTE_ARRAY)) { |
| 54 | + return MCTagType.BYTE_ARRAY; |
| 55 | + } else if(pdc.has(namespacedKey, PersistentDataType.SHORT)) { |
| 56 | + return MCTagType.SHORT; |
| 57 | + } else if(pdc.has(namespacedKey, PersistentDataType.INTEGER_ARRAY)) { |
| 58 | + return MCTagType.INTEGER_ARRAY; |
| 59 | + } else if(pdc.has(namespacedKey, PersistentDataType.LONG_ARRAY)) { |
| 60 | + return MCTagType.LONG_ARRAY; |
| 61 | + } else if(pdc.has(namespacedKey, PersistentDataType.TAG_CONTAINER_ARRAY)) { |
| 62 | + return MCTagType.TAG_CONTAINER_ARRAY; |
| 63 | + } |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Object get(Object key, MCTagType type) { |
| 69 | + PersistentDataType bukkitType = GetPersistentDataType(type); |
| 70 | + Object value = pdc.get(NamespacedKey(key), bukkitType); |
| 71 | + if(value instanceof PersistentDataContainer) { |
| 72 | + return new BukkitMCTagContainer((PersistentDataContainer) value); |
| 73 | + } else if(value instanceof PersistentDataContainer[] concreteContainers) { |
| 74 | + MCTagContainer[] abstractContainers = new MCTagContainer[concreteContainers.length]; |
| 75 | + for(int i = 0; i < concreteContainers.length; i++) { |
| 76 | + abstractContainers[i] = new BukkitMCTagContainer(concreteContainers[i]); |
| 77 | + } |
| 78 | + return abstractContainers; |
| 79 | + } |
| 80 | + return value; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void set(Object key, MCTagType type, Object value) { |
| 85 | + PersistentDataType bukkitType = GetPersistentDataType(type); |
| 86 | + if(value instanceof MCTagContainer) { |
| 87 | + value = ((MCTagContainer) value).getHandle(); |
| 88 | + } else if(value instanceof MCTagContainer[] abstractContainers) { |
| 89 | + PersistentDataContainer[] concreteContainers = new PersistentDataContainer[abstractContainers.length]; |
| 90 | + for(int i = 0; i < abstractContainers.length; i++) { |
| 91 | + concreteContainers[i] = (PersistentDataContainer) abstractContainers[i].getHandle(); |
| 92 | + } |
| 93 | + value = concreteContainers; |
| 94 | + } |
| 95 | + pdc.set(NamespacedKey(key), bukkitType, value); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void remove(Object key) { |
| 100 | + pdc.remove(NamespacedKey(key)); |
| 101 | + } |
| 102 | + |
| 103 | + private static NamespacedKey NamespacedKey(Object key) { |
| 104 | + if(key instanceof NamespacedKey) { |
| 105 | + return (NamespacedKey) key; |
| 106 | + } else if(key instanceof String) { |
| 107 | + NamespacedKey namespacedKey = NamespacedKey.fromString((String) key, CommandHelperPlugin.self); |
| 108 | + if(namespacedKey != null) { |
| 109 | + return namespacedKey; |
| 110 | + } |
| 111 | + } |
| 112 | + throw new IllegalArgumentException("Invalid namespaced key."); |
| 113 | + } |
| 114 | + |
| 115 | + private static PersistentDataType GetPersistentDataType(MCTagType type) { |
| 116 | + switch(type) { |
| 117 | + case BYTE: |
| 118 | + return PersistentDataType.BYTE; |
| 119 | + case BYTE_ARRAY: |
| 120 | + return PersistentDataType.BYTE_ARRAY; |
| 121 | + case DOUBLE: |
| 122 | + return PersistentDataType.DOUBLE; |
| 123 | + case FLOAT: |
| 124 | + return PersistentDataType.FLOAT; |
| 125 | + case INTEGER: |
| 126 | + return PersistentDataType.INTEGER; |
| 127 | + case INTEGER_ARRAY: |
| 128 | + return PersistentDataType.INTEGER_ARRAY; |
| 129 | + case LONG: |
| 130 | + return PersistentDataType.LONG; |
| 131 | + case LONG_ARRAY: |
| 132 | + return PersistentDataType.LONG_ARRAY; |
| 133 | + case SHORT: |
| 134 | + return PersistentDataType.SHORT; |
| 135 | + case STRING: |
| 136 | + return PersistentDataType.STRING; |
| 137 | + case TAG_CONTAINER: |
| 138 | + return PersistentDataType.TAG_CONTAINER; |
| 139 | + case TAG_CONTAINER_ARRAY: |
| 140 | + return PersistentDataType.TAG_CONTAINER_ARRAY; |
| 141 | + } |
| 142 | + throw new IllegalArgumentException("Invalid persistent data type: " + type.name()); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public Object getHandle() { |
| 147 | + return pdc; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public boolean equals(Object o) { |
| 152 | + return o instanceof MCTagContainer && this.pdc.equals(((MCTagContainer) o).getHandle()); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public int hashCode() { |
| 157 | + return pdc.hashCode(); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public String toString() { |
| 162 | + return pdc.toString(); |
| 163 | + } |
| 164 | +} |
0 commit comments