Ejemplo n.º 1
0
 @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
 public void onBlockPhysics(BlockPhysicsEvent event) {
   final Block physics = event.getBlock();
   if (physics.getRelative(BlockFace.DOWN).getType() != Material.AIR) {
     return;
   }
   final Sprout sprout =
       plugin
           .getWorldRegistry()
           .remove(physics.getWorld().getName(), physics.getX(), physics.getY(), physics.getZ());
   final SaveThread thread = ((SaveThread) ThreadRegistry.get(physics.getWorld().getName()));
   if (thread != null) {
     thread.remove(physics.getLocation(), (SimpleSprout) sprout);
   }
   if (sprout == null) {
     return;
   }
   event.setCancelled(true);
   physics.setType(Material.AIR);
   ((SpoutBlock) physics).setCustomBlock(null);
   if (!sprout.getRequiredTools().isEmpty()) {
     disperseDrops(sprout, physics, false);
   }
 }
Ejemplo n.º 2
0
  @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
  public void onBlockFromTo(BlockFromToEvent event) {
    final Block to = event.getToBlock();
    if (to.getType() == Material.LONG_GRASS
        && !(((SpoutBlock) to).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(sprout, to);
    } else {
      final Sprout sprout =
          plugin
              .getWorldRegistry()
              .remove(to.getWorld().getName(), to.getX(), to.getY(), to.getZ());
      final SaveThread thread = ((SaveThread) ThreadRegistry.get(to.getWorld().getName()));
      if (thread != null) {
        thread.remove(to.getLocation(), (SimpleSprout) sprout);
      }
      if (sprout == null) {
        return;
      }
      event.setCancelled(true);

      to.setType(Material.AIR);
      ((SpoutBlock) to).setCustomBlock(null);
      if (!sprout.getRequiredTools().isEmpty()) {
        disperseDrops(sprout, to, false);
      }
    }
  }
Ejemplo n.º 3
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
      }
    }
  }