예제 #1
0
  public boolean collect() {

    boolean collected = false;

    for (Entity entity : LocationUtil.getNearbyEntities(centre, radius)) {
      if (entity.isValid() && entity instanceof Item) {
        if (LocationUtil.isWithinRadius(centre, entity.getLocation(), radius)) {

          stackCheck:
          {
            ItemStack stack = ((Item) entity).getItemStack();

            if (!ItemUtil.isStackValid(stack)) return false;

            for (ItemStack filter : filters) {

              if (!ItemUtil.isStackValid(filter)) continue;

              if (include && !ItemUtil.areItemsIdentical(filter, stack)) break stackCheck;
              else if (!include && ItemUtil.areItemsIdentical(filter, stack)) break stackCheck;
            }

            BlockFace back = SignUtil.getBack(BukkitUtil.toSign(getSign()).getBlock());
            Block pipe = getBackBlock().getRelative(back);

            Pipes pipes = Pipes.Factory.setupPipes(pipe, getBackBlock(), stack);

            if (pipes != null && pipes.getItems().isEmpty()) return true;

            // Add the items to a container, and destroy them.
            List<ItemStack> leftovers =
                InventoryUtil.addItemsToInventory((InventoryHolder) chest.getState(), stack);
            if (leftovers.isEmpty()) {
              entity.remove();
              return true;
            } else {
              if (ItemUtil.areItemsIdentical(leftovers.get(0), stack)
                  && leftovers.get(0).getAmount() != stack.getAmount()) {
                ((Item) entity).setItemStack(leftovers.get(0));
                return true;
              }
            }
          }
        }
      }
    }

    return collected;
  }
예제 #2
0
  @Override
  public void load() {

    if (getLine(3).isEmpty()) offset = getBackBlock().getRelative(0, 1, 0).getLocation();
    else offset = ICUtil.parseBlockLocation(getSign(), 3).getLocation();
    item = ItemUtil.getItem(getLine(2));
  }
  @EventHandler(priority = EventPriority.HIGH)
  public void handleCustomBlockDrops(BlockBreakEvent event) {

    if (!EventUtil.passesFilter(event)) return;

    if (CraftBookPlugin.inst().getConfiguration().customDropPermissions
        && !CraftBookPlugin.inst()
            .wrapPlayer(event.getPlayer())
            .hasPermission("craftbook.mech.drops")) return;

    if (event.getPlayer().getGameMode() == GameMode.CREATIVE) // Don't drop in creative.
    return;

    int id = event.getBlock().getTypeId();
    byte data = event.getBlock().getData();

    CustomDropManager.CustomItemDrop drop = customDrops.getBlockDrops(id);

    if (drop != null) {
      CustomDropManager.DropDefinition[] drops = drop.getDrop(data);
      if (drops != null) {
        Location l = event.getBlock().getLocation();
        World w = event.getBlock().getWorld();
        // Add the custom drops
        for (CustomDropManager.DropDefinition dropDefinition : drops) {
          ItemStack stack = dropDefinition.getItemStack();
          if (ItemUtil.isStackValid(stack)) w.dropItemNaturally(l, stack);
        }

        if (!drops[0].append) {
          event.getBlock().setType(Material.AIR);
          event.setCancelled(true);
          ((ExperienceOrb) event.getBlock().getWorld().spawnEntity(l, EntityType.EXPERIENCE_ORB))
              .setExperience(event.getExpToDrop());
        }
      }
    }
  }
예제 #4
0
  @Override
  public void load() {

    radius = ICUtil.parseRadius(getSign());
    String radiusString = radius.getBlockX() + "," + radius.getBlockY() + "," + radius.getBlockZ();
    if (radius.getBlockX() == radius.getBlockY() && radius.getBlockY() == radius.getBlockZ())
      radiusString = String.valueOf(radius.getBlockX());
    if (getLine(2).contains("=")) {
      getSign().setLine(2, radiusString + "=" + RegexUtil.EQUALS_PATTERN.split(getLine(2))[1]);
      centre = ICUtil.parseBlockLocation(getSign(), 2).getLocation();
    } else {
      getSign().setLine(2, radiusString);
      centre = getBackBlock().getLocation();
    }

    include = !getLine(3).startsWith("-");

    for (String bit : getLine(3).replace("-", "").split(",")) {

      filters.add(ItemUtil.getItem(bit));
    }

    chest = getBackBlock().getRelative(0, 1, 0);
  }
  @EventHandler(priority = EventPriority.HIGH)
  public void handleCustomMobDrops(EntityDeathEvent event) {

    if (!EventUtil.passesFilter(event)) return;
    EntityType entityType = event.getEntityType();
    if (entityType == null || !entityType.isAlive() || entityType.equals(EntityType.PLAYER)) return;
    CustomDropManager.DropDefinition[] drops = customDrops.getMobDrop(event.getEntity());
    if (drops != null) {
      if (!drops[0].append) {
        event.getDrops().clear();
        ((ExperienceOrb)
                event
                    .getEntity()
                    .getWorld()
                    .spawnEntity(event.getEntity().getLocation(), EntityType.EXPERIENCE_ORB))
            .setExperience(event.getDroppedExp());
      }
      // Add the custom drops
      for (CustomDropManager.DropDefinition dropDefinition : drops) {
        ItemStack stack = dropDefinition.getItemStack();
        if (ItemUtil.isStackValid(stack)) event.getDrops().add(stack);
      }
    }
  }
  @Override
  public void trigger(ChipState chip) {

    if (!location.getChunk().isLoaded()) return;

    if (!chip.getInput(0)) return;
    Block left = SignUtil.getLeftBlock(BukkitUtil.toSign(getSign()).getBlock());
    ChangedSign effectSign = null;
    if (left.getTypeId() == BlockID.WALL_SIGN) {
      effectSign = BukkitUtil.toChangedSign(left);
    }

    Block right = SignUtil.getRightBlock(BukkitUtil.toSign(getSign()).getBlock());
    ChangedSign armourSign = null;
    if (right.getTypeId() == BlockID.WALL_SIGN) {
      armourSign = BukkitUtil.toChangedSign(right);
    }

    for (int i = 0; i < amount; i++) {
      Entity ent = BukkitUtil.toSign(getSign()).getWorld().spawn(location, type.getEntityClass());

      if (armourSign != null) { // Apply armor
        if (ent instanceof LivingEntity) {

          for (int s = 0; s < 4; s++) {
            String bit = armourSign.getLine(s);

            ItemStack slot = ItemUtil.makeItemValid(ItemUtil.getItem(bit));

            if (s == 0) ((LivingEntity) ent).getEquipment().setHelmet(slot);
            if (s == 1) ((LivingEntity) ent).getEquipment().setChestplate(slot);
            if (s == 2) ((LivingEntity) ent).getEquipment().setLeggings(slot);
            if (s == 3) ((LivingEntity) ent).getEquipment().setBoots(slot);
          }
        }
      }

      Boolean upwards = null;

      while (effectSign != null) { // Apply effects
        for (int s = 0; s < 4; s++) {
          String bit = effectSign.getLine(s);
          if (bit == null || bit.trim().isEmpty()) continue;

          String[] data = RegexUtil.COLON_PATTERN.split(bit);

          if (data[0].equalsIgnoreCase("e")) CreatureSpawner.setEntityData(ent, bit.substring(2));
          else if (data[0].equalsIgnoreCase("r")) {
            EntityType rider = EntityType.fromName(data[1].trim());
            Entity rid = BukkitUtil.toSign(getSign()).getWorld().spawnEntity(location, rider);
            ent.setPassenger(rid);
          } else if (data[0].equalsIgnoreCase("p") && ent instanceof LivingEntity) {
            for (int a = 1; a < data.length; a++) {
              try {
                String[] potionBits = RegexUtil.SEMICOLON_PATTERN.split(data[a]);
                PotionEffect effect =
                    new PotionEffect(
                        PotionEffectType.getById(Integer.parseInt(potionBits[0])),
                        Integer.parseInt(potionBits[1]),
                        Integer.parseInt(potionBits[2]));
                ((LivingEntity) ent).addPotionEffect(effect, true);
              } catch (Exception e) {
              }
            }
          } else if (data[0].equalsIgnoreCase("v")) {
            try {
              double x, y, z;
              String[] coords = RegexUtil.COMMA_PATTERN.split(data[1]);
              x = Double.parseDouble(coords[0]);
              y = Double.parseDouble(coords[1]);
              z = Double.parseDouble(coords[2]);
              ent.setVelocity(new org.bukkit.util.Vector(x, y, z));
            } catch (Exception ignored) {
            }
          } else if (data[0].equalsIgnoreCase("s")) {
            if (!(ent instanceof LivingEntity)) continue;

            ItemStack slot = ItemUtil.makeItemValid(ItemUtil.getItem(bit.replace("s:", "")));
            ((LivingEntity) ent).getEquipment().setItemInHand(slot);
          }
        }
        if (upwards == null) {
          if (BukkitUtil.toSign(effectSign).getBlock().getRelative(0, 1, 0).getTypeId()
              == BlockID.WALL_SIGN) {
            effectSign =
                BukkitUtil.toChangedSign(
                    BukkitUtil.toSign(effectSign).getBlock().getRelative(0, 1, 0));
            upwards = true;
          } else if (BukkitUtil.toSign(effectSign).getBlock().getRelative(0, -1, 0).getTypeId()
              == BlockID.WALL_SIGN) {
            effectSign =
                BukkitUtil.toChangedSign(
                    BukkitUtil.toSign(effectSign).getBlock().getRelative(0, -1, 0));
            upwards = false;
          } else break;
        } else {
          if (BukkitUtil.toSign(effectSign)
                  .getBlock()
                  .getRelative(0, upwards ? 1 : -1, 0)
                  .getTypeId()
              == BlockID.WALL_SIGN)
            effectSign =
                BukkitUtil.toChangedSign(
                    BukkitUtil.toSign(effectSign).getBlock().getRelative(0, upwards ? 1 : -1, 0));
          else break;
        }
      }
    }
  }
예제 #7
0
  protected boolean collect() {

    Block b = SignUtil.getBackBlock(BukkitUtil.toSign(getSign()).getBlock());

    int x = b.getX();
    int y = b.getY() + 1;
    int z = b.getZ();
    Block bl = BukkitUtil.toSign(getSign()).getBlock().getWorld().getBlockAt(x, y, z);
    for (Entity en : BukkitUtil.toSign(getSign()).getChunk().getEntities()) {
      if (!(en instanceof Item)) {
        continue;
      }
      Item item = (Item) en;
      if (!ItemUtil.isStackValid(item.getItemStack()) || item.isDead() || !item.isValid()) {
        continue;
      }
      int ix = item.getLocation().getBlockX();
      int iy = item.getLocation().getBlockY();
      int iz = item.getLocation().getBlockZ();
      if (ix == getSign().getX() && iy == getSign().getY() && iz == getSign().getZ()) {

        // Create two test stacks to check against
        ItemStack[] testStacks = new ItemStack[] {null, null};

        // Create test stack #1
        try {
          if (getSign().getLine(2).contains(":")) {
            int id = Integer.parseInt(getSign().getLine(2).split(":")[0]);
            int data = Integer.parseInt(getSign().getLine(2).split(":")[1]);
            testStacks[0] = new ItemStack(id, 0, (short) data);
          } else {
            int id = Integer.parseInt(getSign().getLine(2));
            testStacks[1] = new ItemStack(id, 1, (short) 0, (byte) 0);
          }
        } catch (Exception ignored) {
        }

        // Create test stack #2
        try {
          if (getSign().getLine(3).contains(":")) {
            int id = Integer.parseInt(getSign().getLine(3).split(":")[0]);
            int data = Integer.parseInt(getSign().getLine(3).split(":")[1]);
            testStacks[1] = new ItemStack(id, 0, (short) data);
          } else {
            int id = Integer.parseInt(getSign().getLine(2));
            testStacks[1] = new ItemStack(id, 1, (short) 0, (byte) 0);
          }
        } catch (Exception ignored) {
        }

        // Check to see if it matches either test stack, if not stop
        if (testStacks[0] != null)
          if (ItemUtil.areItemsIdentical(testStacks[0], item.getItemStack())) {
            continue;
          }
        if (testStacks[1] != null)
          if (!ItemUtil.areItemsIdentical(testStacks[1], item.getItemStack())) {
            continue;
          }

        // Add the items to a container, and destroy them.
        if (bl.getTypeId() == BlockID.CHEST)
          if (((Chest) bl.getState()).getInventory().firstEmpty() != -1) {

            ((Chest) bl.getState()).getInventory().addItem(item.getItemStack());
            item.remove();
            return true;
          }

        if (bl.getTypeId() == BlockID.DISPENSER)
          if (((Dispenser) bl.getState()).getInventory().firstEmpty() != -1) {

            ((Dispenser) bl.getState()).getInventory().addItem(item.getItemStack());
            item.remove();
            return true;
          }

        if (bl.getTypeId() == BlockID.BREWING_STAND) {

          if (!ItemUtil.isAPotionIngredient(item.getItemStack())) return false;
          if (((BrewingStand) bl.getState()).getInventory().getIngredient() == null
              || ItemUtil.areItemsIdentical(
                  ((BrewingStand) bl.getState()).getInventory().getIngredient(),
                  item.getItemStack())) {

            if (((BrewingStand) bl.getState()).getInventory().getIngredient() == null) {
              ((BrewingStand) bl.getState()).getInventory().setIngredient(item.getItemStack());
            } else {
              ItemUtil.addToStack(
                  ((BrewingStand) bl.getState()).getInventory().getIngredient(),
                  item.getItemStack());
            }
            item.remove();
            return true;
          }
        }

        if (bl.getTypeId() == BlockID.FURNACE || bl.getTypeId() == BlockID.BURNING_FURNACE) {

          Furnace fur = (Furnace) bl.getState();

          if (ItemUtil.isFurnacable(item.getItemStack())
              && (fur.getInventory().getSmelting() == null
                  || ItemUtil.areItemsIdentical(
                      item.getItemStack(), fur.getInventory().getSmelting()))) {
            if (fur.getInventory().getSmelting() == null) {
              fur.getInventory().setSmelting(item.getItemStack());
            } else {
              ItemUtil.addToStack(
                  ((Furnace) bl.getState()).getInventory().getSmelting(), item.getItemStack());
            }
            item.remove();
            return true;
          }

          if (ItemUtil.isAFuel(item.getItemStack())
              && (fur.getInventory().getFuel() == null
                  || ItemUtil.areItemsIdentical(
                      item.getItemStack(), fur.getInventory().getFuel()))) {
            if (fur.getInventory().getFuel() == null) {
              fur.getInventory().setFuel(item.getItemStack());
            } else {
              ItemUtil.addToStack(
                  ((Furnace) bl.getState()).getInventory().getFuel(), item.getItemStack());
            }
            item.remove();
            return true;
          }
        }
      }
    }
    return false;
  }