Ejemplo n.º 1
0
  /** Checking item usage for right click on entity */
  public boolean checkEntityRightClick(ItemStack item, Resident res, Entity entity) {
    for (Iterator<SegmentEntity> it = segmentsEntities.iterator(); it.hasNext(); ) {
      SegmentEntity segment = it.next();
      if (segment.getType() == EntityType.PROTECT
          && segment.getCheckClass().isAssignableFrom(entity.getClass())) {
        int dim = entity.dimension;
        int x = (int) Math.floor(entity.posX);
        int y = (int) Math.floor(entity.posY);
        int z = (int) Math.floor(entity.posZ);

        if (!hasPermission(res, segment, dim, x, y, z)) {
          return true;
        }
      }
    }

    if (item == null) return false;

    for (Iterator<SegmentItem> it = segmentsItems.iterator(); it.hasNext(); ) {
      SegmentItem segment = it.next();
      if (segment.getType() == ItemType.RIGHT_CLICK_ENTITY
          && segment.getCheckClass().isAssignableFrom(item.getItem().getClass())) {
        try {
          if (segment.checkCondition(item)) {
            int range = segment.getRange(item);
            int dim = entity.dimension;
            int x = (int) Math.floor(entity.posX);
            int y = (int) Math.floor(entity.posY);
            int z = (int) Math.floor(entity.posZ);

            if (range == 0) {
              if (!hasPermission(res, segment, dim, x, y, z)) {
                return true;
              }
            } else {
              Volume rangeBox =
                  new Volume(x - range, y - range, z - range, x + range, y + range, z + range);
              if (!hasPermission(res, segment, dim, rangeBox)) {
                return true;
              }
            }
          }
        } catch (Exception ex) {
          MyTown.instance.LOG.error(
              "Failed to check item use on {} at the player {} ({}, {}, {} | Dim: {})",
              item.getDisplayName(),
              res.getPlayerName(),
              entity.posX,
              entity.posY,
              entity.posZ,
              entity.dimension);
          MyTown.instance.LOG.error(ExceptionUtils.getStackTrace(ex));
          if (ex instanceof GetterException || ex instanceof ConditionException) {
            this.disableSegment(it, segment, ex.getMessage());
          }
        }
      }
    }
    return false;
  }
Ejemplo n.º 2
0
 private void disableSegment(Iterator<? extends Segment> it, Segment segment, String message) {
   it.remove();
   MyTown.instance.LOG.error(message);
   MyTown.instance.LOG.error(
       "Disabling segment for {} in protection {}.",
       segment.getCheckClass().getName(),
       this.modid);
   MyTown.instance.LOG.info("Reload protections to enable it again.");
 }
Ejemplo n.º 3
0
 protected boolean shouldCheck(Object object) {
   try {
     if (condition != null && !condition.execute(object, getters)) {
       return false;
     }
   } catch (GetterException ex) {
     MyTown.instance.LOG.error(
         "Encountered error when checking condition for {}", checkClass.getSimpleName());
     MyTown.instance.LOG.error(ExceptionUtils.getStackTrace(ex));
     disable();
   } catch (ConditionException ex) {
     MyTown.instance.LOG.error(
         "Encountered error when checking condition for {}", checkClass.getSimpleName());
     MyTown.instance.LOG.error(ExceptionUtils.getStackTrace(ex));
     disable();
   }
   return true;
 }
Ejemplo n.º 4
0
 public boolean checkTileEntity(TileEntity te) {
   for (Iterator<SegmentTileEntity> it = segmentsTiles.iterator(); it.hasNext(); ) {
     SegmentTileEntity segment = it.next();
     if (segment.getCheckClass().isAssignableFrom(te.getClass())) {
       try {
         if (segment.checkCondition(te)) {
           Volume teBox =
               new Volume(
                   segment.getX1(te),
                   segment.getY1(te),
                   segment.getZ1(te),
                   segment.getX2(te),
                   segment.getY2(te),
                   segment.getZ2(te));
           int dim = te.getWorldObj().provider.dimensionId;
           Resident owner =
               segment.hasOwner() ? Protections.instance.getOwnerForTileEntity(te) : null;
           if (!hasPermission(owner, segment, dim, teBox)) {
             return true;
           }
         }
       } catch (Exception ex) {
         MyTown.instance.LOG.error(
             "Failed to check tile entity: {} ({}, {}, {}, Dim: {})",
             te.getClass().getSimpleName(),
             te.xCoord,
             te.yCoord,
             te.zCoord,
             te.getWorldObj().provider.dimensionId);
         MyTown.instance.LOG.error(ExceptionUtils.getStackTrace(ex));
         // Disabling protection if something errors.
         if (ex instanceof GetterException || ex instanceof ConditionException) {
           this.disableSegment(it, segment, ex.getMessage());
         } else {
           MyTown.instance.LOG.error("Skipping...");
         }
       }
       return false;
     }
   }
   return false;
 }
Ejemplo n.º 5
0
  @SuppressWarnings("unchecked")
  @Override
  public boolean validate(List<Flag> items) {
    boolean isValid = true;

    for (Iterator<Flag> it = items.iterator(); it.hasNext(); ) {
      Flag item = it.next();
      if (item.flagType == null) {
        MyTown.instance.LOG.error("An unrecognized flagType has been found. Removing...");
        it.remove();
        isValid = false;
        continue;
      }
      if (!item.flagType.isWildPerm) {
        MyTown.instance.LOG.error(
            "A non wild flagType has been found in WildPerms config file. Removing...");
        it.remove();
        isValid = false;
      }
    }

    for (FlagType type : FlagType.values()) {
      if (type.isWildPerm) {
        boolean ok = false;
        for (Flag f : items) {
          if (f.flagType == type) {
            ok = true;
          }
        }
        if (!ok) {
          MyTown.instance.LOG.error(
              "FlagType {} for Wild does not exist in the WildPerms file. Adding...", type.name);
          items.add(new Flag(type, type.defaultValue));
          isValid = false;
        }
      }
    }
    return isValid;
  }
Ejemplo n.º 6
0
 public static void check(TileEntity te) {
   for (SegmentTileEntity segment : segmentsTile.get(te.getClass())) {
     if (!segment.shouldExist(te)) {
       ItemStack itemStack = new ItemStack(te.getBlockType(), 1, te.getBlockMetadata());
       NBTTagCompound nbt = new NBTTagCompound();
       te.writeToNBT(nbt);
       itemStack.setTagCompound(nbt);
       WorldUtils.dropAsEntity(te.getWorldObj(), te.xCoord, te.yCoord, te.zCoord, itemStack);
       te.getWorldObj().setBlock(te.xCoord, te.yCoord, te.zCoord, Blocks.air);
       te.invalidate();
       MyTown.instance.LOG.info("TileEntity {} was ATOMICALLY DISINTEGRATED!", te.toString());
       return;
     }
   }
 }
Ejemplo n.º 7
0
  /** Gets the nearby tile entities of the specified tile entity and of the specified type */
  public static List<TileEntity> getNearbyTileEntity(
      TileEntity te, Class<? extends TileEntity> type) {
    List<TileEntity> result = new ArrayList<TileEntity>();
    int[] dx = {0, 1, 0, -1, 0, 0};
    int[] dy = {1, 0, -1, 0, 0, 0};
    int[] dz = {0, 0, 0, 0, 1, -1};

    for (int i = 0; i < 6; i++) {
      TileEntity found =
          te.getWorldObj().getTileEntity(te.xCoord + dx[i], te.yCoord + dy[i], te.zCoord + dz[i]);
      if (found != null && type.isAssignableFrom(found.getClass())) {
        MyTown.instance.LOG.info("Found tile entity {} for class {}", found, type.getName());
        result.add(found);
      }
    }
    return result;
  }
Ejemplo n.º 8
0
  /** Checking item usage for left or right click on block */
  public boolean checkItem(ItemStack item, ItemType type, Resident res, BlockPos bp, int face) {

    for (Iterator<SegmentItem> it = segmentsItems.iterator(); it.hasNext(); ) {
      SegmentItem segment = it.next();
      if (segment.getType() == type
          && segment.getCheckClass().isAssignableFrom(item.getItem().getClass())) {
        ForgeDirection direction = ForgeDirection.getOrientation(face);
        if (segment.isOnAdjacent()) {
          bp =
              new BlockPos(
                  bp.getX() + direction.offsetX,
                  bp.getY() + direction.offsetY,
                  bp.getZ() + direction.offsetZ,
                  bp.getDim());
        }
        if (!segment.isDirectionalClientUpdate()) {
          direction = null;
        }
        try {
          if (segment.checkCondition(item)) {
            int range = segment.getRange(item);
            int dim = bp.getDim();
            int x = bp.getX();
            int y = bp.getY();
            int z = bp.getZ();
            boolean isProtected;

            if (range == 0) {
              isProtected = !hasPermission(res, segment, dim, x, y, z);
            } else {
              Volume rangeBox =
                  new Volume(x - range, y - range, z - range, x + range, y + range, z + range);
              isProtected = !hasPermission(res, segment, dim, rangeBox);
            }

            if (isProtected) {
              if (segment.hasClientUpdate()) {
                sendClientUpdate(
                    segment.getClientUpdateCoords(),
                    bp,
                    (EntityPlayerMP) res.getPlayer(),
                    direction);
              }
              return true;
            }
          }
        } catch (Exception ex) {
          MyTown.instance.LOG.error(
              "Failed to check item use on {} at the player {} ({})",
              item.getDisplayName(),
              res.getPlayerName(),
              bp);
          MyTown.instance.LOG.error(ExceptionUtils.getStackTrace(ex));
          if (ex instanceof GetterException || ex instanceof ConditionException) {
            this.disableSegment(it, segment, ex.getMessage());
          }
        }
      }
    }
    return false;
  }
Ejemplo n.º 9
0
    @Override
    public Segment deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
      if (!json.getAsJsonObject().has("class")) {
        throw new ProtectionParseException("One of the segments is missing a class identifier");
      }

      JsonObject jsonObject = json.getAsJsonObject();
      String classString = jsonObject.get("class").getAsString();

      if (!json.getAsJsonObject().has("type")) {
        throw new ProtectionParseException("Segment for " + classString + " is missing a type");
      }
      String type = jsonObject.get("type").getAsString();
      jsonObject.remove("type");

      Segment segment = null;
      if ("specialBlock".equals(type)) {
        segment = deserializeSpecialBlock(jsonObject, context);
      } else if ("block".equals(type)) {
        segment = deserializeBlock(jsonObject, context);
      } else if ("entity".equals(type)) {
        segment = deserializeEntity(jsonObject, context);
      } else if ("item".equals(type)) {
        segment = deserializeItem(jsonObject, context);
      } else if ("tileEntity".equals(type)) {
        segment = deserializeTileEntity(jsonObject, context);
      }

      if (segment == null) {
        throw new ProtectionParseException("Identifier type is invalid");
      }

      try {
        segment.checkClass = Class.forName(classString);
      } catch (ClassNotFoundException ex) {
        // throw new ProtectionParseException("Invalid class identifier: " + classString);
        MyTown.instance.LOG.error(
            "Invalid class identifier {" + classString + "}: >>> Segment Rejected <<<");
        return null;
      }
      jsonObject.remove("class");

      if (!(segment instanceof SegmentSpecialBlock)) {
        if (!json.getAsJsonObject().has("flags")) {
          throw new ProtectionParseException("Segment for " + classString + " is missing flags");
        }
        segment.flags.addAll(
            deserializeAsArray(
                jsonObject.get("flags"),
                context,
                new TypeToken<FlagType<Boolean>>() {},
                new TypeToken<List<FlagType<Boolean>>>() {}.getType()));
        jsonObject.remove("flags");

        if (jsonObject.has("condition")) {
          segment.condition = new Condition(jsonObject.get("condition").getAsString());
          jsonObject.remove("condition");
        }

        if (jsonObject.has("priority")) {
          segment.priority = Priority.valueOf(jsonObject.get("priority").getAsString());
          jsonObject.remove("priority");
        }

        for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
          Getter getter = context.deserialize(entry.getValue(), Getter.class);
          getter.setName(entry.getKey());
          segment.getters.add(getter);
        }
      }

      return segment;
    }
Ejemplo n.º 10
0
 protected void disable() {
   MyTown.instance.LOG.error("Disabling segment for {}", checkClass.getName());
   MyTown.instance.LOG.info("Reload protections to enable it again.");
   this.isDisabled = true;
 }