-
-
Save yannicklamprecht/954034de30a1f4427239f2dc5ccd49c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.craftstuebchen.minez3.weaponry.persistence; | |
import org.bukkit.NamespacedKey; | |
import org.bukkit.persistence.PersistentDataAdapterContext; | |
import org.bukkit.persistence.PersistentDataContainer; | |
import org.bukkit.persistence.PersistentDataType; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class WeaponDataTagType implements PersistentDataType<PersistentDataContainer, WeaponData> { | |
// thats the one from the JavaDoc example | |
private static final UUIDTagType UUID_TAG_TYPE = new UUIDTagType(); | |
private JavaPlugin javaPlugin; | |
public WeaponDataTagType(JavaPlugin javaPlugin) { | |
this.javaPlugin = javaPlugin; | |
} | |
@Override | |
public Class<PersistentDataContainer> getPrimitiveType() { | |
return PersistentDataContainer.class; | |
} | |
@Override | |
public Class<WeaponData> getComplexType() { | |
return WeaponData.class; | |
} | |
@Override | |
public PersistentDataContainer toPrimitive(WeaponData weaponData, PersistentDataAdapterContext persistentDataAdapterContext) { | |
PersistentDataContainer persistentDataContainer = persistentDataAdapterContext.newPersistentDataContainer(); | |
persistentDataContainer.set(key("weapon"), PersistentDataType.STRING, weaponData.getId()); | |
persistentDataContainer.set(key("customtrail"), PersistentDataType.STRING, weaponData.getCustomTrails() == null ? "undefined" : weaponData.getCustomTrails()); | |
persistentDataContainer.set(key("rounds"), PersistentDataType.INTEGER, weaponData.getRounds()); | |
persistentDataContainer.set(key("uuid"), UUID_TAG_TYPE, weaponData.getUuid()); | |
persistentDataContainer.set(key("boltpulled"), PersistentDataType.BYTE, weaponData.isBoltpulled() ? (byte) 1 : 0); | |
persistentDataContainer.set(key("firemode"), PersistentDataType.INTEGER, weaponData.getFireMode()); | |
writeAttachments(persistentDataContainer, weaponData.getAttachments()); | |
return persistentDataContainer; | |
} | |
@Override | |
public WeaponData fromPrimitive(PersistentDataContainer persistentDataContainer, PersistentDataAdapterContext persistentDataAdapterContext) { | |
WeaponData weaponData = new WeaponData(); | |
weaponData.setId(persistentDataContainer.get(key("weapon"), PersistentDataType.STRING)); | |
weaponData.setCustomTrails(persistentDataContainer.get(key("customtrail"), PersistentDataType.STRING)); | |
weaponData.setRounds(getOrDefault(persistentDataContainer.get(key("rounds"), PersistentDataType.INTEGER), 0)); | |
weaponData.setUuid(persistentDataContainer.get(key("uuid"), UUID_TAG_TYPE)); | |
weaponData.setBoltpulled(fromByte(persistentDataContainer.get(key("boltpulled"), PersistentDataType.BYTE), false)); | |
weaponData.setFireMode(getOrDefault(persistentDataContainer.get(key("firemode"), PersistentDataType.INTEGER), 0)); | |
weaponData.setAttachments(readAttachements(persistentDataContainer)); | |
return weaponData; | |
} | |
private void writeAttachments(PersistentDataContainer customItemTagContainer, Map<String, String> attachments) { | |
List<String> concatedAttachements = new ArrayList<>(); | |
for (Map.Entry<String, String> attachment : attachments.entrySet()) { | |
concatedAttachements.add(attachment.getKey() + "," + attachment.getValue()); | |
} | |
customItemTagContainer.set(key("weaponattachments"), PersistentDataType.STRING, String.join(";", concatedAttachements)); | |
} | |
private Map<String, String> readAttachements(PersistentDataContainer persistentDataContainer) { | |
Map<String, String> list = new HashMap<>(); | |
if (persistentDataContainer.has(key("weaponattachments"), PersistentDataType.STRING)) { | |
String attachments = persistentDataContainer.get(key("weaponattachments"), PersistentDataType.STRING); | |
String[] attachmentList = attachments.split(";"); | |
for (String singleAttachement : attachmentList) { | |
String[] slit = singleAttachement.split(","); | |
if (slit.length == 2) { | |
list.put(slit[0], slit[1]); | |
} | |
} | |
} | |
return list; | |
} | |
private NamespacedKey key(String key) { | |
return new NamespacedKey(javaPlugin, key); | |
} | |
private int getOrDefault(Integer integer, int defaultValue) { | |
return integer != null ? integer : defaultValue; | |
} | |
private boolean fromByte(Byte byteValue, boolean defaultValue) { | |
return byteValue != null ? byteValue == 1 : defaultValue; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DataPersistence { | |
private final NamespacedKey weaponKey; | |
private final WeaponDataTagType weaponDataTagType; | |
public DataPersistence(JavaPlugin javaPlugin){ | |
this.weaponKey = new NamespacedKey(plugin, "weapon"); | |
this.weaponDataTagType = new WeaponDataTagType(plugin); | |
} | |
public ItemStack set(ItemStack itemStack, WeaponData weaponData) { | |
ItemMeta itemMeta = itemStack.getItemMeta(); | |
PersistentDataContainer customItemTagContainer = itemMeta.getPersistentDataContainer(); | |
customItemTagContainer.set(weaponKey, weaponDataTagType, weaponData); | |
itemStack.setItemMeta(itemMeta); | |
return itemStack; | |
} | |
public WeaponData get(ItemStack itemStack) { | |
ItemMeta itemMeta = itemStack.getItemMeta(); | |
PersistentDataContainer customItemTagContainer = itemMeta.getPersistentDataContainer(); | |
return customItemTagContainer.get(weaponKey, weaponDataTagType); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment