예제 #1
0
  @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
  public void onBlockBreak(BlockBreakEvent event) {
    final Block block = event.getBlock();
    // Handle random seed drops. To preserve a possible LongGrass base block, make sure it isn't a
    // custom block
    // TODO Configurable.
    if (event.getBlock().getType() == Material.LONG_GRASS
        && !(((SpoutBlock) block).getBlockType() instanceof CustomBlock)
        && RANDOM.nextInt(10 - 1) + 1 == 7) { // 10% chance for a drop.
      final Sprout sprout =
          plugin.getSproutRegistry().get(RANDOM.nextInt(plugin.getSproutRegistry().size()));
      if (sprout == null) {
        return;
      }
      if (!sprout.getVariables().dropItemSourceOnGrassBreak()) {
        return;
      }
      disperseSeeds(event.getPlayer(), sprout, block);
    } else {
      // Handle breaking of Sprouts
      final Sprout sprout =
          plugin
              .getWorldRegistry()
              .get(block.getWorld().getName(), block.getX(), block.getY(), block.getZ());
      final SaveThread thread = ((SaveThread) ThreadRegistry.get(block.getWorld().getName()));
      if (sprout == null) {
        return;
      }
      event.setCancelled(true); // Cancels Bukkit Event, SpoutPlugin events proceed this.

      // Handle Custom Tools, Required tools first.
      SpoutItemStack handStack = null;
      if (event.getPlayer().getItemInHand() != null) {
        handStack = new SpoutItemStack(event.getPlayer().getItemInHand());
      }
      boolean foundSproutTool = false;
      boolean foundStageTool = false;
      // Sprout based Tools
      if (handStack != null && handStack.isCustomItem() && !sprout.getRequiredTools().isEmpty()) {
        for (Tool requiredTool : sprout.getRequiredTools()) {
          if (requiredTool
              .getName()
              .equalsIgnoreCase(((CustomItem) handStack.getMaterial()).getFullName())) {
            // Found exact tool.
            foundSproutTool = true;
            break;
          }
        }
      }

      // Stage based Tools
      if (handStack != null
          && handStack.isCustomItem()
          && !sprout.getCurrentStage().getRequiredTools().isEmpty()) {
        for (Tool requiredStageTool : sprout.getCurrentStage().getRequiredTools()) {
          if (requiredStageTool
              .getName()
              .equalsIgnoreCase(((CustomItem) handStack.getMaterial()).getFullName())) {
            // Found exact tool.
            foundStageTool = true;
            break;
          }
        }
      }

      // Sprout Tool Lookup
      if (!foundSproutTool
          && !sprout.getRequiredTools().isEmpty()
          && !event.getPlayer().getGameMode().equals(GameMode.CREATIVE)) {
        // Invalid tool or item in hand null
        event
            .getPlayer()
            .sendMessage(
                ChatColor.DARK_AQUA
                    + "[Sprout]"
                    + ChatColor.WHITE
                    + " Requires a tool to harvest.");
        return;
      }

      // Stage Tool Lookup
      if (!foundStageTool
          && !sprout.getCurrentStage().getRequiredTools().isEmpty()
          && !event.getPlayer().getGameMode().equals(GameMode.CREATIVE)) {
        // Invalid tool or item in hand null
        event
            .getPlayer()
            .sendMessage(
                ChatColor.DARK_AQUA
                    + "[Sprout]"
                    + ChatColor.WHITE
                    + " Requires a tool to harvest.");
        return;
      }

      // Handle Jobs Integration Calls
      if (SproutConfiguration.jobsEnabled) {
        if (!Jobs.hasRequiredJobAndLevel(
            event.getPlayer(), "Farmer", sprout.getMinimumLevelRequired())) {
          event
              .getPlayer()
              .sendMessage(
                  ChatColor.DARK_AQUA
                      + "[Sprout]"
                      + ChatColor.WHITE
                      + " Harvesting this crop requires the job "
                      + ChatColor.GOLD
                      + "[Farmer]"
                      + ChatColor.WHITE
                      + ", at level "
                      + ChatColor.GOLD
                      + sprout.getMinimumLevelRequired()
                      + ChatColor.WHITE
                      + " or higher.");
          return;
        }
        JobsWorker.jobsBreak(event.getPlayer(), block);
      }

      // Clear current location
      block.setType(Material.AIR);
      ((SpoutBlock) block).setCustomBlock(null);

      // Lets roll a dice for a bonus!
      if (!event.getPlayer().getGameMode().equals(GameMode.CREATIVE) && (sprout.isFullyGrown())) {
        if (!sprout.getBonus().isEmpty()) {
          int bonusModifier = 0;
          if (handStack != null && !sprout.getCurrentStage().getBonusTools().isEmpty()) {
            for (Tool bonusTool : sprout.getCurrentStage().getBonusTools()) {
              if (bonusTool
                  .getName()
                  .equalsIgnoreCase(((CustomItem) handStack.getMaterial()).getFullName())) {
                bonusModifier = bonusTool.getBonusAmount();
                break;
              }
            }
          } else if (handStack != null && !sprout.getBonusTools().isEmpty()) {
            for (Tool bonusTool : sprout.getBonusTools()) {
              if (bonusTool
                  .getName()
                  .equalsIgnoreCase(((CustomItem) handStack.getMaterial()).getFullName())) {
                bonusModifier = bonusTool.getBonusAmount();
                break;
              }
            }
          }

          if (sprout.getBonusChance() - bonusModifier < 0) { // Automatic bonus drop
            disperseDrops(event.getPlayer(), sprout, block, true);
          } else if (RANDOM.nextInt((sprout.getBonusChance() - bonusModifier) + 1)
              == (sprout.getBonusChance() - bonusModifier)) {
            if (SproutConfiguration.bonusMessage) {
              event
                  .getPlayer()
                  .sendMessage(
                      ChatColor.DARK_AQUA
                          + "[Sprout]"
                          + ChatColor.WHITE
                          + " You get a bonus drop!");
            }
            disperseDrops(event.getPlayer(), sprout, block, true);
          }
        }
        disperseDrops(event.getPlayer(), sprout, block, false);
      }

      // Remove Sprout from Db.
      if (thread != null) {
        thread.remove(block.getLocation(), (SimpleSprout) sprout); // Remove from DB.
        plugin
            .getWorldRegistry()
            .remove(
                block.getWorld().getName(),
                block.getX(),
                block.getY(),
                block.getZ()); // Remove from Registry
      }
    }
  }
예제 #2
0
  @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
  public void onPlayerInteract(PlayerInteractEvent event) {
    // Prevent trampling
    switch (event.getAction()) {
      case PHYSICAL:
        final Block top = event.getClickedBlock().getRelative((BlockFace.UP));
        if (plugin
            .getWorldRegistry()
            .contains(top.getWorld().getName(), top.getX(), top.getY(), top.getZ())) {
          event.setCancelled(true);
          break;
        }
      case RIGHT_CLICK_BLOCK:
        // Exit this method if player clicking on chest, door, button, etc.
        switch (event.getClickedBlock().getType()) {
          case CHEST:
          case WOOD_BUTTON:
          case STONE_BUTTON:
          case WOOD_DOOR:
          case IRON_DOOR:
          case IRON_DOOR_BLOCK:
          case FENCE_GATE:
          case BREWING_STAND:
          case FURNACE:
          case BURNING_FURNACE:
            return;
        }
        final Player interacter = event.getPlayer();
        final ItemStack held = event.getItem();
        if (held == null) {
          return;
        }
        final Block interacted = event.getClickedBlock();
        final SpoutItemStack stack = new SpoutItemStack(held);
        final String name;

        // Grab the material's name
        if (stack.isCustomItem()) {
          name = stack.getMaterial().getNotchianName();
        } else {
          name = held.getType().name();
        }

        // Fertilizer logic
        final Sprout dispersed =
            plugin
                .getWorldRegistry()
                .get(
                    interacted.getWorld().getName(),
                    interacted.getX(),
                    interacted.getY(),
                    interacted.getZ());
        if (dispersed != null
            && !((SimpleSprout) dispersed).isOnLastStage()
            && dispersed.getVariables().allowFertilization()) {
          final Stage current = dispersed.getCurrentStage();
          final Fertilizer fertilizer;
          if (current == null) {
            fertilizer = dispersed.getFertilizerSource();
          } else {
            fertilizer = current.getFertilizer();
          }

          boolean toContinue = false;

          // Bonemeal
          if ((fertilizer.getName().equals("bonemeal") && name.equals("INK_SACK"))) {
            toContinue = true;
            // Custom Block
          } else if (fertilizer.getName().endsWith(name)) {
            toContinue = true;
            // Material
          } else if (fertilizer.getName().equals(name.toLowerCase())) {
            toContinue = true;
          }
          if (!toContinue) {
            return;
          }
          event.setCancelled(true);

          Stage stage;
          org.getspout.spoutapi.material.Material customMaterial;
          Material material;

          // Stage 0
          if (current == null) {
            stage = dispersed.getStage(1);
            customMaterial = MaterialData.getCustomBlock(stage.getName());
            material = Material.getMaterial(stage.getName().toUpperCase());

            if (customMaterial == null) {
              if (material == null) {
                return;
              }
            }

            // Grow to stage 1
            ((SimpleSprout) dispersed).grow(stage);

            if (customMaterial != null) {
              ((SpoutBlock) interacted).setCustomBlock((CustomBlock) customMaterial);
            } else {
              ((SpoutBlock) interacted).setCustomBlock(null);
              interacted.setType(material);
            }
          } else {
            stage = ((SimpleSprout) dispersed).getNextStage();
            if (stage == null) {
              return;
            }
            customMaterial = MaterialData.getCustomBlock(stage.getName());
            material = Material.getMaterial(stage.getName().toUpperCase());

            if (customMaterial == null) {
              if (material == null) {
                return;
              }
            }

            ((SimpleSprout) dispersed).incrementFertilizerCount(stage);
            if (((SimpleSprout) dispersed).getFertilizerCount(stage) >= fertilizer.getAmount()) {
              if (customMaterial != null) {
                ((SpoutBlock) interacted).setCustomBlock((CustomBlock) customMaterial);
              } else {
                ((SpoutBlock) interacted).setCustomBlock(null);
                interacted.setType(material);
              }
              ((SimpleSprout) dispersed).grow(stage);
            }
            if (((SimpleSprout) dispersed).isOnLastStage()) {
              ((SimpleSprout) dispersed).setFullyGrown(true);
              final SaveThread thread =
                  ((SaveThread) ThreadRegistry.get(interacted.getWorld().getName()));
              if (thread != null) {
                thread.add(interacted.getLocation(), (SimpleSprout) dispersed);
              }
            }
          }
          decrementInventory(interacter, interacter.getItemInHand());
        } else {
          // Non-fertilizer logic
          final Sprout sprout = plugin.getSproutRegistry().find(name);
          if (sprout == null) {
            return;
          }

          event.setCancelled(true);

          // Block face logic. TODO Customizable?
          if (event.getBlockFace() != BlockFace.UP) {
            return;
          }

          // Soil logic
          org.getspout.spoutapi.material.Material customMaterial =
              MaterialData.getCustomItem(sprout.getPlacementSource());
          Material material = Material.getMaterial(sprout.getPlacementSource().toUpperCase());
          if (customMaterial == null
              || !(((SpoutBlock) interacted).getBlockType() instanceof CustomBlock)
              || !((CustomBlock) ((SpoutBlock) interacted).getBlockType())
                  .getFullName()
                  .equalsIgnoreCase(customMaterial.getName())) {
            if (material == null || !interacted.getType().equals(material)) {
              return;
            }
          }

          final Block where = interacted.getRelative(BlockFace.UP);

          // Make sure where we are setting the block won't be already obstructed.
          if (where.getType() != Material.AIR) {
            return;
          }

          // Handle Jobs Integration Calls
          if (SproutConfiguration.jobsEnabled) {
            if (!Jobs.hasRequiredJobAndLevel(
                event.getPlayer(), "Farmer", sprout.getMinimumLevelRequired())) {
              event
                  .getPlayer()
                  .sendMessage(
                      ChatColor.DARK_AQUA
                          + "[Sprout]"
                          + ChatColor.WHITE
                          + " Planting this seed requires the job "
                          + ChatColor.GOLD
                          + "[Farmer]"
                          + ChatColor.WHITE
                          + ", at level "
                          + ChatColor.GOLD
                          + sprout.getMinimumLevelRequired()
                          + ChatColor.WHITE
                          + " or higher.");
              return;
            }
            // Planting experience within jobs is handled by block place of the vanilla block of
            // "CROP"
          }

          final Sprout toInject = cloner.deepClone(sprout);
          plugin
              .getWorldRegistry()
              .add(where.getWorld().getName(), where.getX(), where.getY(), where.getZ(), toInject);
          final SaveThread thread = ((SaveThread) ThreadRegistry.get(where.getWorld().getName()));
          if (thread != null) {
            thread.add(where.getLocation(), (SimpleSprout) toInject);
          }

          // Set material
          if (stack.isCustomItem()) {
            final CustomBlock block = MaterialData.getCustomBlock(sprout.getBlockSource());
            ((SpoutBlock) where).setCustomBlock(block);
            interacter.playSound(interacter.getLocation(), Sound.DIG_GRASS, 1.0F, 0.7936508F);
            decrementInventory(interacter, held);
          }
          // Handle Jobs Integration Calls

          if (SproutConfiguration.jobsEnabled) {
            JobsWorker.jobsPlace(event.getPlayer(), where);
          }
        }
    }
  }