Esempio n. 1
0
  @Override
  public void onBlockInteract(LWCBlockInteractEvent event) {
    if (event.getResult() != Result.DEFAULT) {
      return;
    }

    if (!event.hasAction("create")) {
      return;
    }

    LWC lwc = event.getLWC();
    Block block = event.getBlock();
    LWCPlayer player = lwc.wrapPlayer(event.getPlayer());

    if (!lwc.isProtectable(block)) {
      return;
    }

    PhysDB physDb = lwc.getPhysicalDatabase();

    Action action = player.getAction("create");
    String actionData = action.getData();
    String[] split = actionData.split(" ");
    String protectionType = split[0].toLowerCase();
    String protectionData = StringUtil.join(split, 1);

    // check permissions again (DID THE LITTLE SHIT MOVE WORLDS??!?!?!?!?!?)
    if (!lwc.hasPermission(
        event.getPlayer(), "lwc.create." + protectionType, "lwc.create", "lwc.protect")) {
      lwc.sendLocale(player, "protection.accessdenied");
      lwc.removeModes(player);
      event.setResult(Result.CANCEL);
      return;
    }

    // misc data we'll use later
    String playerName = player.getName();
    String worldName = block.getWorld().getName();
    int blockX = block.getX();
    int blockY = block.getY();
    int blockZ = block.getZ();

    lwc.removeModes(player);
    LWCProtectionRegisterEvent evt =
        new LWCProtectionRegisterEvent(player.getBukkitPlayer(), block);
    lwc.getModuleLoader().dispatchEvent(evt);

    // another plugin cancelled the registration
    if (evt.isCancelled()) {
      return;
    }

    // The created protection
    Protection protection = null;

    if (protectionType.equals("public")) {
      protection =
          physDb.registerProtection(
              block.getTypeId(),
              Protection.Type.PUBLIC,
              worldName,
              player.getUniqueId().toString(),
              "",
              blockX,
              blockY,
              blockZ);
      lwc.sendLocale(player, "protection.interact.create.finalize");
    } else if (protectionType.equals("password")) {
      String password = lwc.encrypt(protectionData);

      protection =
          physDb.registerProtection(
              block.getTypeId(),
              Protection.Type.PASSWORD,
              worldName,
              player.getUniqueId().toString(),
              password,
              blockX,
              blockY,
              blockZ);
      player.addAccessibleProtection(protection);

      lwc.sendLocale(player, "protection.interact.create.finalize");
      lwc.sendLocale(player, "protection.interact.create.password");
    } else if (protectionType.equals("private") || protectionType.equals("donation")) {
      String[] rights = protectionData.split(" ");

      protection =
          physDb.registerProtection(
              block.getTypeId(),
              Protection.Type.matchType(protectionType),
              worldName,
              player.getUniqueId().toString(),
              "",
              blockX,
              blockY,
              blockZ);

      lwc.sendLocale(player, "protection.interact.create.finalize");
      lwc.processRightsModifications(player, protection, rights);
    }

    // tell the modules that a protection was registered
    if (protection != null) {
      // Fix the blocks that match it
      protection.removeCache();
      LWC.getInstance().getProtectionCache().addProtection(protection);

      lwc.getModuleLoader().dispatchEvent(new LWCProtectionRegistrationPostEvent(protection));
    }

    event.setResult(Result.CANCEL);
  }
Esempio n. 2
0
  @Override
  public void onCommand(LWCCommandEvent event) {
    if (event.isCancelled()) {
      return;
    }

    if (!event.hasFlag("c", "create")) {
      return;
    }

    LWC lwc = event.getLWC();
    CommandSender sender = event.getSender();
    String[] args = event.getArgs();

    if (!(sender instanceof Player)) {
      return;
    }

    if (args.length == 0) {
      lwc.sendLocale(sender, "help.creation");
      return;
    }

    LWCPlayer player = lwc.wrapPlayer(sender);

    String full = StringUtil.join(args, 0).trim();
    String type = args[0].toLowerCase();
    String data = StringUtil.join(args, 1);
    event.setCancelled(true);

    /**
     * Allow individual enforcements with e.g lwc.create.private, or just the umbrella lwc.create
     * for all
     */
    if (!lwc.hasPermission(sender, "lwc.create." + type, "lwc.create", "lwc.protect")) {
      lwc.sendLocale(sender, "protection.accessdenied");
      return;
    }

    try {
      switch (Protection.Type.matchType(type)) {
        case PASSWORD:
          if (args.length < 2) {
            lwc.sendSimpleUsage(player, "/lwc -c password <Password>");
            return;
          }

          String hiddenPass = StringUtil.transform(data, '*');
          lwc.sendLocale(player, "protection.create.password", "password", hiddenPass);
          break;
      }
    } catch (IllegalArgumentException e) {
      // Invalid protection type!
      lwc.sendLocale(player, "help.creation");
      return;
    }

    Action action = new Action();
    action.setName("create");
    action.setPlayer(player);
    action.setData(full);

    player.removeAllActions();
    player.addAction(action);

    lwc.sendLocale(
        player,
        "protection.create.finalize",
        "type",
        lwc.getPlugin().getMessageParser().parseMessage(type));
  }