use of org.dragonet.inventory.ItemList in project Dragonet-Legacy by DragonetMC.
the class Translator_v0_11 method processCrafting.
public void processCrafting(WindowSetSlotPacket packet) {
if (!(this.getSession().getPlayer() instanceof Player)) {
return;
}
int realSlot = 0;
if (packet.slot < 27) {
realSlot = packet.slot + 9;
} else if (packet.slot >= 27) {
realSlot = packet.slot - 27;
}
ItemStack item = this.getSession().getPlayer().getInventory().getItem(realSlot);
if (item == null) {
item = new ItemStack(Material.AIR);
} else if (item.getAmount() <= 0) {
this.getSession().getPlayer().getInventory().setItem(realSlot, null);
return;
}
System.out.println("FROM " + item.toString() + "to (ITEM=" + packet.item.id + ",CNT=" + packet.item.count + ")");
if (packet.item.count < 0) {
this.getSession().sendInventory();
return;
}
if (item.getTypeId() == 0 && packet.item.id == 0) {
this.getSession().sendInventory();
//No changes
return;
}
if (item.getTypeId() == packet.item.id && item.getAmount() == packet.item.count && item.getDurability() == packet.item.meta) {
this.getSession().sendInventory();
//No changes
return;
}
if ((item.getTypeId() != 0 && packet.item.id == 0) || (item.getTypeId() != 0 && (item.getTypeId() != packet.item.id)) || (item.getAmount() > (packet.item.count & 0xFF))) {
this.getSession().sendInventory();
//Decreasing item, ignore
return;
}
int amount = packet.item.count - (item.getTypeId() == 0 ? 0 : item.getAmount());
ItemStack result = new ItemStack(packet.item.id, amount, packet.item.meta);
List<Recipe> recipes = this.getSession().getServer().getCraftingManager().getRecipesFor(result);
if (recipes.size() <= 0) {
return;
}
//System.out.println("CRAFTING FOR: " + result.toString() + ", recipes count: " + recipes.size());
if (packet.windowID == PEWindowConstantID.PLAYER_INVENTORY && recipes.size() > 4) {
//Can not craft more than 4 recipes in a player inventory
this.getSession().sendInventory();
return;
}
ItemList items = new ItemList(this.getSession().getPlayer().getInventory());
//List all ways to craft
for (Recipe recipe : recipes) {
if (recipe instanceof ShapedRecipe) {
ShapedRecipe shaped = (ShapedRecipe) recipe;
boolean faild = false;
for (String itemChar : shaped.getShape()) {
ItemStack ingredient = shaped.getIngredientMap().get(new Character(itemChar.charAt(0)));
if (ingredient == null) {
continue;
}
if (!items.tryToRemove(ingredient)) {
faild = true;
break;
}
}
if (!faild) {
//Apply changes
for (String itemChar : shaped.getShape()) {
ItemStack ingredient = shaped.getIngredientMap().get(new Character(itemChar.charAt(0)));
if (ingredient == null) {
continue;
}
this.getSession().getPlayer().getInventory().remove(ingredient);
}
//System.out.println("CRAFT SUCCESS! ");
} else {
continue;
}
this.getSession().getPlayer().getInventory().addItem(result);
this.getSession().sendInventory();
return;
}
}
//System.out.println("FAILD TO CRAFT! ");
this.getSession().sendInventory();
}
use of org.dragonet.inventory.ItemList in project Dragonet-Legacy by DragonetMC.
the class WindowItemsPacketTranslator method removeItems.
/**
* Remove enough items from the given item list to form the given recipe.
* @param items The items to remove the ingredients from.
* @param recipe A recipe known to match the items.
*/
public void removeItems(ItemStack[] items, Recipe recipe) {
ItemList lst = new ItemList(items);
if (recipe instanceof ShapedRecipe) {
ShapedRecipe shaped = (ShapedRecipe) recipe;
for (String itemChar : shaped.getShape()) {
ItemStack ingredient = shaped.getIngredientMap().get(new Character(itemChar.charAt(0)));
if (ingredient == null) {
continue;
}
lst.tryToRemove(ingredient);
}
}
for (int i = 0; i < items.length; i++) {
items[i] = lst.getItems().get(i);
}
}
Aggregations