@Override public void readData(SpoutInputStream input) throws IOException { super.readData(input); stack.setTypeId(input.readInt()); stack.setAmount((int) input.readShort()); stack.setDurability(input.readShort()); depth = input.readInt(); renderAmount = input.readBoolean(); }
public static void transferId(ItemStack stack, JsonObject json, boolean stack2json) { if (stack2json) { int id = stack.getTypeId(); if (id == DEFAULT_ID) return; json.addProperty(ID, id); } else { JsonElement element = json.get(ID); if (element == null) return; stack.setTypeId(element.getAsInt()); } }
public void performMimic(Player p, int radius, boolean sphere) { int freqArray[][] = new int[200][16]; // holder for counts of each blocktype, and 15 durability levels for wool // get player location Location location = p.getLocation(); int xPos = location.getBlockX(); int yPos = -1; if (location.getBlockY() - (int) radius / 5 > 0) { // this is a hemisphere the top half of a sphere. This makes it positioned so you // still get some blocks a little below you. yPos = location.getBlockY() - (int) radius / 5; } else { yPos = location.getBlockY(); // in case player is just barely above bedrock } int zPos = location.getBlockZ(); Inventory inv = p.getInventory(); int data = 0; // may want to then use getContents which gives a whole array if stacks, then setContents. // get block type frequency within a hemisphere for (int x = radius; x >= 0; x--) { double xPow = Math.pow(x, 2); for (int y = radius; y >= 0; y--) { double yPow = Math.pow(y, 2); for (int z = radius; z >= 0; z--) { if (xPow + yPow + Math.pow(z, 2) <= Math.pow(radius + 0.5, 2)) { data = (w.getBlockAt(xPos + x, yPos + y, zPos + z).getData()); if (data > 15) { data = 15; } freqArray[w.getBlockTypeIdAt(xPos + x, yPos + y, zPos + z)][data]++; data = (w.getBlockAt(xPos + x, yPos + y, zPos - z).getData()); if (data > 15) { data = 15; } freqArray[w.getBlockTypeIdAt(xPos + x, yPos + y, zPos - z)][data]++; if (sphere) { data = (w.getBlockAt(xPos + x, yPos - y, zPos + z).getData()); if (data > 15) { data = 15; } freqArray[w.getBlockTypeIdAt(xPos + x, yPos - y, zPos + z)][ data]++; // uncomment for full sphere version. data = (w.getBlockAt(xPos + x, yPos - y, zPos - z).getData()); if (data > 15) { data = 15; } freqArray[w.getBlockTypeIdAt(xPos + x, yPos - y, zPos - z)][data]++; } data = (w.getBlockAt(xPos - x, yPos + y, zPos + z).getData()); if (data > 15) { data = 15; } freqArray[w.getBlockTypeIdAt(xPos - x, yPos + y, zPos + z)][data]++; data = (w.getBlockAt(xPos - x, yPos + y, zPos - z).getData()); if (data > 15) { data = 15; } freqArray[w.getBlockTypeIdAt(xPos - x, yPos + y, zPos - z)][data]++; if (sphere) { data = (w.getBlockAt(xPos - x, yPos - y, zPos + z).getData()); if (data > 15) { data = 15; } freqArray[w.getBlockTypeIdAt(xPos - x, yPos - y, zPos + z)][data]++; data = (w.getBlockAt(xPos - x, yPos - y, zPos - z).getData()); if (data > 15) { data = 15; } freqArray[w.getBlockTypeIdAt(xPos - x, yPos - y, zPos - z)][data]++; } } } } } for (int k = 0; k <= 199; k++) { // ignore anything specified on config file. By default, some common natural stuff, // and explosives and things. if (unmimicable.contains(k)) { for (int l = 0; l <= 15; l++) { freqArray[k][l] = 0; } } } int sum = 0; for (int k = 0; k <= 199; k++) { // only one data value type for anything whose data value is irrelevant to inventory // (like torches) if (k == 66 || k == 18 || k == 84 || k == 61 || k == 62 || k == 55 || k == 92 || k == 8 || k == 10 || k == 23 || k == 25 || k == 26 || k == 46 || k == 50 || k == 51 || k == 52 || k == 53 || k == 54 || k == 63 || k == 64 || k == 65 || k == 67 || k == 68 || k == 69 || k == 71 || k == 75 || k == 76 || k == 77 || k == 85 || k == 86 || k == 91 || k == 93 || k == 94 || k == 90 || k == 70 || k == 72 || k == 96) { sum = 0; for (int l = 0; l <= 15; l++) { sum = sum + freqArray[k][l]; freqArray[k][l] = 0; } freqArray[k][0] = sum; } } int max = 0; int winnerA = 0; int winnerB = 0; boolean keepLooking = true; int replaced = 0; int existId; int existDur; boolean[][] skipThis = new boolean[200] [36]; // don't give item types you already have in your inventory. first dim = item id // type, second dim = data value for (int r = 1; r <= 36; r++) { ItemStack stack = inv.getItem(r); existId = (stack == null) ? 0 : stack.getTypeId(); existDur = (stack == null) ? 0 : stack.getDurability(); switch (existId) { // if you have a door item in inventory, don't mimic door BLOCKS, etc. case 331: skipThis[55][0] = true; break; case 323: skipThis[63][0] = true; skipThis[68][0] = true; break; case 324: skipThis[64][0] = true; break; case 330: skipThis[71][0] = true; break; case 338: skipThis[83][0] = true; break; } if (existId < 200 && existId > 0) { if (existDur < 16 && existDur > -1) { skipThis[existId][existDur] = true; } } } for (int index = 0; index <= 17; index++) { // will only replace first two lines of your backpack. max = 0; winnerA = 0; winnerB = 0; if (keepLooking) { for (int j = 0; j <= 199; j++) { // find current maximum / most popular block type in array for (int m = 0; m <= 15; m++) { if (freqArray[j][m] > max && !skipThis[j][m]) { max = freqArray[j][m]; winnerA = j; winnerB = m; freqArray[j][m] = 0; // this should fix the looping problem of last build } else if (skipThis[j][m]) { freqArray[j][m] = 0; } } } } if (winnerA != 0) { // if winnerA == 0, then that means we are out of new block types that were found // during the scan, so stop messing with the inventory. ItemStack stack = inv.getItem(index + 9); // get the stack from inventory we are at right now. if (stack != null) { stack.setTypeId(winnerA); } else { stack = new ItemStack(winnerA); } // makes it a stack of the popular type. if (winnerA == 55) { stack.setTypeId(331); } // changing various things that should be items instead of blocks. if (winnerA == 63 || winnerA == 68) { stack.setTypeId(323); } if (winnerA == 64) { stack.setTypeId(324); } if (winnerA == 71) { stack.setTypeId(330); } if (winnerA == 83) { stack.setTypeId(338); } stack.setAmount(64); // make it a full stack. // MaterialData matData = new MaterialData(winnerB); //Okay, this is supposed to create a // new materialdata type of a certain data value. winnerB will be 5, for instance, for // light green wool. stack.setDurability( (short) winnerB); // then it should set the stack to that data value. Unfortunately, there // is only a method here for a full materialdata object, not just an // integer GRR inv.setItem(index + 9, stack); // put new stack back into current slot. freqArray[winnerA][winnerB] = 0; // clear out that winner so we will get the next most popular next time. if (index == 17) { replaced = 18; // handles end case; } } else { if (keepLooking) { replaced = index; } keepLooking = false; } } p.sendMessage( ChatColor.LIGHT_PURPLE + "Environment mimicked in inventory. " + replaced + " stack(s) given (max: 18)."); }
/** * Sets the type of this item * * <p>Note that in doing so you will reset the MaterialData for this stack * * @param type New type to set the items in this stack to */ @Utility public void setType(Material type) { Validate.notNull(type, "Material cannot be null"); setTypeId(type.getId()); }
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { // Console ausschließen if (!(sender instanceof Player)) { sender.sendMessage(ChatColor.RED + "You have to be a player!"); return false; } // beenden, wenn Argumente gegeben wurden if (args.length != 0) { return false; } Player player = (Player) sender; if (cmd.getName().equalsIgnoreCase("care-pack")) { if (player.hasPermission("CarePack.ever")) { PlayerInventory inventory = player.getInventory(); // Steinschwert ItemStack item = new ItemStack(272, 1); inventory.addItem(item); // 10 Holz item.setTypeId(17); item.setData(new MaterialData(0)); item.setAmount(10); inventory.addItem(item); // 4 Fackeln item.setTypeId(50); item.setAmount(4); inventory.addItem(item); // 1 Bett item.setTypeId(355); item.setAmount(1); inventory.addItem(item); // 2 Brot item.setTypeId(297); item.setAmount(2); inventory.addItem(item); // 1 Beef item.setTypeId(364); item.setAmount(1); inventory.addItem(item); return true; } else if ((player.getWorld().getTime() < 7000 && player.getWorld().getTime() > 22000) || player.getFoodLevel() < 6 || player.getSaturation() < 6) { PlayerInventory inventory = player.getInventory(); // Steinschwert ItemStack item = new ItemStack(272, 1); inventory.addItem(item); // 10 Holz item.setTypeId(17); item.setData(new MaterialData(0)); item.setAmount(10); inventory.addItem(item); // 4 Fackeln item.setTypeId(50); item.setAmount(4); inventory.addItem(item); // 1 Bett item.setTypeId(355); item.setAmount(1); inventory.addItem(item); // 2 Brot item.setTypeId(297); item.setAmount(2); inventory.addItem(item); // 1 Beef item.setTypeId(364); item.setAmount(1); inventory.addItem(item); return true; } else { player.sendMessage(ChatColor.RED + "You can't use this command now!"); return true; } } return false; }