Implement ItemDbProviders, pt 7

Implement getStack and resolve for FlatItemDbProvider
This commit is contained in:
md678685 2018-08-19 23:12:27 +01:00
parent 610774b0b9
commit fec655a29c

View File

@ -4,6 +4,8 @@ import com.google.gson.*;
import net.ess3.nms.ItemDbProvider;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionType;
import java.util.*;
@ -16,7 +18,7 @@ public class FlatItemDbProvider extends ItemDbProvider {
@Override
public Material resolve(String name) {
return null;
return Objects.requireNonNull(getByName(name)).getMaterial();
}
@Override
@ -59,11 +61,42 @@ public class FlatItemDbProvider extends ItemDbProvider {
return null;
}
@Override
public ItemStack getStack(String name) throws Exception {
ItemData data = Objects.requireNonNull(getByName(name));
PotionData potionData = data.getPotionData();
Material material = data.getMaterial();
ItemStack stack = new ItemStack(material);
if (potionData != null) {
PotionMeta meta = (PotionMeta) stack.getItemMeta();
meta.setBasePotionData(potionData);
stack.setItemMeta(meta);
}
return stack;
}
@Override
public Collection<String> listNames() {
return Collections.unmodifiableSet(primaryNames.keySet());
}
private ItemData getByName(String name) {
if (primaryNames.containsKey(name.toLowerCase())) {
return primaryNames.get(name);
} else {
for (Map.Entry<String, List<String>> entry : names.entrySet()) {
if (entry.getValue().contains(name.toLowerCase())) {
return primaryNames.get(entry.getKey());
}
}
}
return null;
}
@Override
public void rebuild(List<String> lines) {
String json = String.join("\n", lines);