Exemple #1
0
  @EventHandler
  public void onVehicleImpact(CartBlockImpactEvent event) {

    // validate
    if (!event.getBlocks().matches(getMaterial())) return;
    if (!event.getBlocks().hasSign()) return;
    if (event.isMinor()) return;
    if (!(event.getBlocks().matches("cartlift up") || event.getBlocks().matches("cartlift down")))
      return;

    Minecart cart = (Minecart) event.getVehicle();

    // go
    boolean up = event.getBlocks().matches("cartlift up");
    Block destination = event.getBlocks().sign;

    BlockFace face;
    if (up) face = BlockFace.UP;
    else face = BlockFace.DOWN;

    while (true) {

      if (destination.getLocation().getBlockY() <= 0 && !up) return;
      if (destination.getLocation().getBlockY() >= destination.getWorld().getMaxHeight() - 1 && up)
        return;

      destination = destination.getRelative(face);

      if (SignUtil.isSign(destination)
          && event.getBlocks().base.getTypeId()
              == destination.getRelative(BlockFace.UP, 1).getTypeId()) {

        ChangedSign state = BukkitUtil.toChangedSign(destination);
        String testLine = state.getLine(1);

        if (testLine.equalsIgnoreCase("[CartLift Up]")
            || testLine.equalsIgnoreCase("[CartLift Down]")
            || testLine.equalsIgnoreCase("[CartLift]")) {
          destination = destination.getRelative(BlockFace.UP, 2);
          break;
        }
      }
    }

    CartUtils.teleport(
        cart,
        new Location(
            destination.getWorld(),
            destination.getX(),
            destination.getY(),
            destination.getZ(),
            cart.getLocation().getYaw(),
            cart.getLocation().getPitch()));
  }
  @Override
  public void execute(Minecart minecart, Object parameter) {

    if (Type.DOUBLE.isInstance(parameter)) {
      minecart.getWorld().setTime(((Double) parameter).longValue());
    } else {
      if (String.valueOf(parameter).equalsIgnoreCase("d")
          || String.valueOf(parameter).equalsIgnoreCase("day")) {
        minecart.getWorld().setTime(0);
      } else if (String.valueOf(parameter).equalsIgnoreCase("n")
          || String.valueOf(parameter).equalsIgnoreCase("night")) {
        minecart.getWorld().setTime(12500);
      }
    }
  }
  @Override
  public void impact(Minecart cart, CartMechanismBlocks blocks, boolean minor) {
    // validate
    if (cart == null) return;

    // care?
    if (minor) return;

    // enabled?
    if (Power.OFF == isActive(blocks.rail, blocks.base, blocks.sign)) return;

    // speed up or down
    Vector newVelocity;
    if (multiplier > 1) {
      newVelocity = cart.getVelocity().normalize().multiply(multiplier);
    } else if (multiplier < 1) {
      newVelocity = cart.getVelocity().multiply(multiplier);
    } else return;
    // go
    cart.setVelocity(newVelocity);
  }
  @Override
  public boolean run(
      CommandSender sender,
      Player sender_p,
      Command cmd,
      String commandLabel,
      String[] args,
      boolean senderIsConsole) {
    Player targetPlayer = sender_p;

    if (args.length == 1) {

      targetPlayer = getPlayer(args[0]);

      if (targetPlayer == null) {
        sender.sendMessage(TotalFreedomMod.PLAYER_NOT_FOUND);
        return true;
      }
    }

    if (senderIsConsole) {
      if (targetPlayer == null) {
        sender.sendMessage(
            "When used from the console, you must define a target player: /cartsit <player>");
        return true;
      }
    } else if (targetPlayer != sender_p && !TFM_AdminList.isSuperAdmin(sender)) {
      sender.sendMessage("Only superadmins can select another player as a /cartsit target.");
      return true;
    }

    if (targetPlayer.isInsideVehicle()) {
      targetPlayer.getVehicle().eject();
    } else {
      Minecart nearest_cart = null;
      for (Minecart cart : targetPlayer.getWorld().getEntitiesByClass(Minecart.class)) {
        if (cart.isEmpty()) {
          if (nearest_cart == null) {
            nearest_cart = cart;
          } else {
            if (cart.getLocation().distanceSquared(targetPlayer.getLocation())
                < nearest_cart.getLocation().distanceSquared(targetPlayer.getLocation())) {
              nearest_cart = cart;
            }
          }
        }
      }

      if (nearest_cart != null) {
        nearest_cart.setPassenger(targetPlayer);
      } else {
        sender.sendMessage("There are no empty minecarts in the target world.");
      }
    }

    return true;
  }
Exemple #5
0
  /**
   * Returns true if the entity was damaged.
   *
   * @return
   */
  protected boolean hurt() {

    int radius = 10; // Default Radius
    int damage = 2;
    Location location = getSign().getLocation();
    Type type = Type.MOB_HOSTILE;
    try {
      radius = Integer.parseInt(getSign().getLine(2).split("=")[0]);
      if (getSign().getLine(2).contains("=")) {
        int x = Integer.parseInt(getSign().getLine(2).split("=")[1].split(":")[0]);
        int y = Integer.parseInt(getSign().getLine(2).split("=")[1].split(":")[1]);
        int z = Integer.parseInt(getSign().getLine(2).split("=")[1].split(":")[2]);
        location.add(x, y, z);

        damage = Integer.parseInt(getSign().getLine(2).split("=")[2]);
      }
    } catch (Exception e) {
    }

    if (getSign().getLine(3).length() != 0) type = Type.fromString(getSign().getLine(3));

    try {
      for (Entity e : LocationUtil.getNearbyEntities(location, radius)) {
        if (e.isDead() || !e.isValid()) continue;
        if (!type.is(e)) continue;
        if (e instanceof LivingEntity) ((LivingEntity) e).damage(damage);
        else if (e instanceof Minecart)
          ((Minecart) e).setDamage(((Minecart) e).getDamage() + damage);
        else e.remove();
        return true;
      }
    } catch (Exception e) {
    }

    return false;
  }
Exemple #6
0
  public static boolean isSortApplicable(String line, Minecart minecart) {
    if (line.equalsIgnoreCase("All")) {
      return true;
    }
    Entity test = minecart.getPassenger();
    Player player = null;
    if (test instanceof Player) player = (Player) test;

    if ((line.equalsIgnoreCase("Unoccupied") || line.equalsIgnoreCase("Empty"))
        && minecart.getPassenger() == null) {
      return true;
    }

    if (line.equalsIgnoreCase("Storage") && minecart instanceof StorageMinecart) {
      return true;
    } else if (line.equalsIgnoreCase("Powered") && minecart instanceof PoweredMinecart) {
      return true;
    } else if (line.equalsIgnoreCase("Minecart") && minecart instanceof Minecart) {
      return true;
    }

    if ((line.equalsIgnoreCase("Occupied") || line.equalsIgnoreCase("Full"))
        && minecart.getPassenger() != null) {
      return true;
    }

    if (line.equalsIgnoreCase("Animal") && test instanceof Animals) {
      return true;
    }

    if (line.equalsIgnoreCase("Mob") && test instanceof Monster) {
      return true;
    }

    if ((line.equalsIgnoreCase("Player") || line.equalsIgnoreCase("Ply")) && player != null) {
      return true;
    }

    String[] parts = line.split(":");

    if (parts.length >= 2) {
      if (player != null && parts[0].equalsIgnoreCase("Held")) {
        try {
          int item = Integer.parseInt(parts[1]);
          if (player.getItemInHand().getTypeId() == item) {
            return true;
          }
        } catch (NumberFormatException e) {
        }
      } else if (player != null && parts[0].equalsIgnoreCase("Ply")) {
        if (parts[1].equalsIgnoreCase(player.getName())) {
          return true;
        }
      } else if (parts[0].equalsIgnoreCase("Mob")) {
        String testMob = parts[1];
        test.toString().toLowerCase().equalsIgnoreCase(testMob);
      } else if (minecart instanceof StorageMinecart && parts[0].equalsIgnoreCase("Ctns")) {
        StorageMinecart storageCart = (StorageMinecart) minecart;
        Inventory storageInventory = storageCart.getInventory();

        if (parts.length == 3) {
          try {
            int item = Integer.parseInt(parts[1]);
            short durability = Short.parseShort(parts[2]);
            if (storageInventory.contains(new ItemStack(item, 1, durability))) {
              return true;
            }
          } catch (NumberFormatException e) {
          }
        } else {
          try {
            int item = Integer.parseInt(parts[1]);
            if (storageInventory.contains(item)) {
              return true;
            }
          } catch (NumberFormatException e) {
          }
        }
      }
    }

    return false;
  }
  @SuppressWarnings("deprecation")
  public void assignMobProps(Entity baseEntity, ISpawnableEntity data) {

    // This needs to be before everything else!
    if (data.getRider() != null) {
      addRider(baseEntity, data.getRider());
    }

    Vector v1 = data.getVelocity(baseEntity);
    Vector v2 = data.getVelocity2(baseEntity);
    if (v2.getX() == 0 && v2.getY() == 0 && v2.getZ() == 0) {
      baseEntity.setVelocity(data.getVelocity(baseEntity).clone());
    } else {
      Vector v3 = randomVector(v1, v2);
      baseEntity.setVelocity(v3.clone());
    }
    baseEntity.setFireTicks(data.getFireTicks(baseEntity));

    if (baseEntity instanceof LivingEntity) {
      LivingEntity entity = (LivingEntity) baseEntity;
      setBasicProps(entity, data);

      if (data.showCustomName()) {
        setCustomName(entity, data);
      }

      if (entity instanceof Ageable) {
        Ageable a = (Ageable) entity;
        setAgeProps(a, data);
      }

      if (entity instanceof Animals) {
        Animals animal = (Animals) entity;

        // Setting animal specific properties
        if (animal instanceof Pig) {
          Pig p = (Pig) animal;
          p.setSaddle(data.isSaddled());
        } else if (animal instanceof Sheep) {
          Sheep s = (Sheep) animal;
          DyeColor color = DyeColor.valueOf(data.getColor());
          s.setColor(color);
        } else if (animal instanceof Wolf) {
          Wolf w = (Wolf) animal;
          w.setAngry(data.isAngry());
          w.setTamed(data.isTamed());
          if (data.isTamed()) {

            ArrayList<Player> nearPlayers = getNearbyPlayers(w.getLocation(), 16);
            int index = (int) Math.round(Math.rint(nearPlayers.size() - 1));
            if (nearPlayers != null) {
              w.setOwner(nearPlayers.get(index));
            }

            w.setSitting(data.isSitting());
          }
        } else if (animal instanceof Ocelot) {
          Ocelot o = (Ocelot) animal;
          o.setTamed(data.isTamed());
          if (data.isTamed()) {
            Ocelot.Type catType = Ocelot.Type.valueOf(data.getCatType());
            o.setCatType(catType);

            ArrayList<Player> nearPlayers = getNearbyPlayers(o.getLocation(), 16);
            int index = (int) Math.round(Math.rint(nearPlayers.size() - 1));
            if (nearPlayers != null) {
              o.setOwner(nearPlayers.get(index));
            }

            o.setSitting(data.isSitting());
          }
        }
      } else if (entity instanceof Villager) {
        Villager v = (Villager) entity;
        v.setAge(data.getAge(baseEntity));
        v.setProfession(data.getProfession());
      } else if (entity instanceof Monster) {
        Monster monster = (Monster) entity;

        // Setting monster specific properties.
        if (monster instanceof Enderman) {
          Enderman e = (Enderman) monster;
          e.setCarriedMaterial(data.getEndermanBlock());
        } else if (monster instanceof Creeper) {
          Creeper c = (Creeper) monster;
          c.setPowered(data.isCharged());
        } else if (monster instanceof PigZombie) {
          PigZombie p = (PigZombie) monster;
          if (data.isAngry()) {
            p.setAngry(true);
          }
          p.setBaby((data.getAge(baseEntity) < -1) ? true : false);
        } else if (monster instanceof Spider) {
          Spider s = (Spider) monster;
          if (data.isJockey()) {
            makeJockey(s, data);
          }
        } else if (monster instanceof Zombie) {
          Zombie z = (Zombie) monster;
          boolean isVillager = false;
          if (data.hasProp("zombie")) {
            isVillager = (Boolean) (data.getProp("zombie"));
          }
          z.setBaby((data.getAge(baseEntity) < -1) ? true : false);
          z.setVillager(isVillager);
        } else if (monster instanceof Skeleton) {
          Skeleton sk = (Skeleton) monster;
          SkeletonType skType = SkeletonType.NORMAL;

          if (data.hasProp("wither")) {
            skType =
                ((Boolean) (data.getProp("wither")) == true)
                    ? SkeletonType.WITHER
                    : SkeletonType.NORMAL;
          }

          sk.setSkeletonType(skType);
        }
      } else if (entity instanceof Golem) {
        Golem golem = (Golem) entity;

        if (golem instanceof IronGolem) {
          IronGolem i = (IronGolem) golem;
          if (data.isAngry()) {
            ArrayList<Player> nearPlayers = getNearbyPlayers(i.getLocation(), 16);
            int index = (int) Math.round(Math.rint(nearPlayers.size() - 1));
            if (nearPlayers != null) {
              i.setPlayerCreated(false);
              i.damage(0, nearPlayers.get(index));
              i.setTarget(nearPlayers.get(index));
            }
          }
        }
        // Some are not classified as animals or monsters
      } else if (entity instanceof Slime) {
        Slime s = (Slime) entity;
        s.setSize(data.getSlimeSize());
      } else if (entity instanceof MagmaCube) {
        MagmaCube m = (MagmaCube) entity;
        m.setSize(data.getSlimeSize());
      }

    } else if (baseEntity instanceof Projectile) {
      Projectile pro = (Projectile) baseEntity;

      // Eventually add explosive arrows and such :D

      if (pro instanceof Fireball) {
        Fireball f = (Fireball) pro;
        setExplosiveProps(f, data);
        f.setVelocity(new Vector(0, 0, 0));
        f.setDirection(data.getVelocity(baseEntity));
      } else if (pro instanceof SmallFireball) {
        SmallFireball f = (SmallFireball) pro;
        setExplosiveProps(f, data);
        f.setVelocity(new Vector(0, 0, 0));
        f.setDirection(data.getVelocity(baseEntity));
      }

    } else if (baseEntity instanceof Explosive) {

      Explosive ex = (Explosive) baseEntity;

      if (ex instanceof TNTPrimed) {
        TNTPrimed tnt = (TNTPrimed) ex;
        setExplosiveProps(tnt, data);
        tnt.setFuseTicks(data.getFuseTicks(baseEntity));
      }

    } else if (baseEntity instanceof Firework) {

      Firework f = (Firework) baseEntity;
      ItemMeta meta = data.getItemType().getItemMeta();
      if (meta != null) {
        if (meta instanceof FireworkMeta) {
          FireworkMeta fMeta = (FireworkMeta) meta;
          if (fMeta != null) {
            f.setFireworkMeta(fMeta);
          }
        }
      }

    } else if (baseEntity instanceof Minecart) {

      Minecart m = (Minecart) baseEntity;
      if (data.hasProp("minecartSpeed")) {
        m.setMaxSpeed((Double) data.getProp("minecartSpeed"));
      }

    } else if (baseEntity instanceof ExperienceOrb) {
      ExperienceOrb o = (ExperienceOrb) baseEntity;
      o.setExperience(data.getDroppedExp(baseEntity));
    }

    setNBT(baseEntity, data);
  }