use of org.spongepowered.api.item.ItemType in project HuskyCrates-Sponge by codeHusky.
the class CrateConfigParser method itemFromNode.
private static ItemStack itemFromNode(ConfigurationNode itemRoot) {
try {
if (itemRoot.getNode("id").isVirtual()) {
HuskyCrates.instance.logger.error("NO ITEM ID: " + itemRoot.getNode("name").getString("(no name)") + " (item #" + itemRoot.getKey() + ") || " + itemRoot.getParent().getParent().getKey());
return ItemStack.of(ItemTypes.NONE, 1);
}
ItemType type;
Integer dmg = null;
try {
type = itemRoot.getNode("id").getValue(TypeToken.of(ItemType.class));
} catch (Exception e) {
String id = itemRoot.getNode("id").getString();
String[] parts = id.split(":");
try {
if (parts.length == 3) {
id = parts[0] + ":" + parts[1];
dmg = Integer.parseInt(parts[2]);
} else if (parts.length == 2) {
id = parts[0];
dmg = Integer.parseInt(parts[1]);
}
} catch (Exception ee) {
HuskyCrates.instance.logger.error("INVALID ITEM ID: \"" + itemRoot.getNode("id").getString("NOT A STRING") + "\" || " + itemRoot.getNode("name").getString("(no name)") + " (item #" + itemRoot.getKey() + ") || " + itemRoot.getParent().getParent().getKey());
return ItemStack.of(ItemTypes.NONE, 1);
}
Optional<ItemType> optType = Sponge.getRegistry().getType(ItemType.class, id);
if (optType.isPresent()) {
type = optType.get();
} else {
HuskyCrates.instance.logger.error("INVALID ITEM ID: \"" + itemRoot.getNode("id").getString("NOT A STRING") + "\" || " + itemRoot.getNode("name").getString("(no name)") + " (item #" + itemRoot.getKey() + ") || " + itemRoot.getParent().getParent().getKey());
return ItemStack.of(ItemTypes.NONE, 1);
}
}
ItemStack item = ItemStack.builder().itemType(type).quantity(itemRoot.getNode("count").getInt(1)).build();
if (!itemRoot.getNode("name").isVirtual()) {
item.offer(Keys.DISPLAY_NAME, TextSerializers.FORMATTING_CODE.deserialize(itemRoot.getNode("name").getString()));
}
if (!itemRoot.getNode("lore").isVirtual()) {
ArrayList<Text> lore = new ArrayList<>();
for (String ll : itemRoot.getNode("lore").getList(TypeToken.of(String.class))) {
lore.add(TextSerializers.FORMATTING_CODE.deserialize(ll));
}
item.offer(Keys.ITEM_LORE, lore);
}
if (!itemRoot.getNode("name").isVirtual()) {
item.offer(Keys.DISPLAY_NAME, TextSerializers.FORMATTING_CODE.deserialize(itemRoot.getNode("name").getString()));
}
if (!itemRoot.getNode("enchants").isVirtual()) {
ArrayList<Enchantment> enchantments = new ArrayList<>();
for (Object key : itemRoot.getNode("enchants").getChildrenMap().keySet()) {
int level = itemRoot.getNode("enchants").getChildrenMap().get(key).getInt();
String enchantID = (String) key;
Optional<EnchantmentType> pEnchantType = Sponge.getRegistry().getType(EnchantmentType.class, enchantID);
if (!pEnchantType.isPresent()) {
HuskyCrates.instance.logger.error("INVALID ENCHANT ID: \"" + key + "\" || " + itemRoot.getNode("name").getString("(no name)") + " (item #" + itemRoot.getKey() + ") || " + itemRoot.getParent().getParent().getKey());
return ItemStack.of(ItemTypes.NONE, 1);
}
Enchantment pEnchant = Enchantment.of(pEnchantType.get(), level);
enchantments.add(pEnchant);
}
item.offer(Keys.ITEM_ENCHANTMENTS, enchantments);
}
if (!itemRoot.getNode("damage").isVirtual()) {
// HuskyCrates.instance.logger.info("damage override called");
item = ItemStack.builder().fromContainer(// OVERRIDE DAMAGE VAL! :)
item.toContainer().set(DataQuery.of("UnsafeDamage"), itemRoot.getNode("damage").getInt(0))).build();
} else if (dmg != null) {
item = ItemStack.builder().fromContainer(// OVERRIDE DAMAGE VAL! :)
item.toContainer().set(DataQuery.of("UnsafeDamage"), dmg)).build();
}
if (!itemRoot.getNode("nbt").isVirtual()) {
// nbt overrrides
LinkedHashMap items = (LinkedHashMap) itemRoot.getNode("nbt").getValue();
if (item.toContainer().get(DataQuery.of("UnsafeData")).isPresent()) {
BiMap real = ((BiMap) item.toContainer().getMap(DataQuery.of("UnsafeData")).get());
items.putAll(real);
}
// System.out.println(item.toContainer().get(DataQuery.of("UnsafeData")).get().getClass());
item = ItemStack.builder().fromContainer(item.toContainer().set(DataQuery.of("UnsafeData"), items)).build();
}
// item.offer(Keys.PICKUP_DELAY,itemRoot.getNode("pickupdelay").getInt())
return item;
} catch (ObjectMappingException e) {
e.printStackTrace();
}
return null;
}
use of org.spongepowered.api.item.ItemType in project Skree by Skelril.
the class CursedMineInstance method sweepFloor.
public void sweepFloor() {
for (Item item : getContained(Item.class)) {
if (!contains(item)) {
continue;
}
ItemType id = item.getItemType();
for (ItemType aItem : ITEMS) {
if (aItem == id) {
ItemStackSnapshot snapshot = item.get(Keys.REPRESENTED_ITEM).get();
int newAmt = (int) (snapshot.getQuantity() * .8);
if (newAmt < 1) {
item.remove();
} else {
ItemStack newStack = snapshot.createStack();
newStack.setQuantity(newAmt);
item.offer(Keys.REPRESENTED_ITEM, newStack.createSnapshot());
}
break;
}
}
}
}
use of org.spongepowered.api.item.ItemType in project Skree by Skelril.
the class FocusTeleporter method onRightClick.
@Listener
public void onRightClick(InteractBlockEvent.Secondary.MainHand event, @First Player player) {
Optional<org.spongepowered.api.item.inventory.ItemStack> optHeldItem = player.getItemInHand(HandTypes.MAIN_HAND);
if (!optHeldItem.isPresent()) {
return;
}
org.spongepowered.api.item.inventory.ItemStack held = optHeldItem.get();
if (held.getItem() != this) {
return;
}
Optional<Location<World>> optDestination = getDestination(held);
if (!optDestination.isPresent()) {
return;
}
Inventory result = player.getInventory().query((ItemType) Sponge.getRegistry().getType(ItemType.class, "skree:ender_focus").get());
if (result.size() > 0) {
Task.builder().execute(() -> {
result.poll(1);
player.setLocation(optDestination.get());
}).delayTicks(1).submit(SkreePlugin.inst());
event.setUseBlockResult(Tristate.FALSE);
}
}
Aggregations