コード例 #1
0
ファイル: Entity.java プロジェクト: ItsLucas/Nukkit
  public static boolean registerEntity(String name, Class<? extends Entity> clazz, boolean force) {
    if (clazz == null) {
      return false;
    }
    try {
      int networkId = clazz.getField("NETWORK_ID").getInt(null);
      knownEntities.put(String.valueOf(networkId), clazz);
    } catch (Exception e) {
      if (!force) {
        return false;
      }
    }

    knownEntities.put(name, clazz);
    shortNames.put(clazz.getSimpleName(), name);
    return true;
  }
コード例 #2
0
ファイル: Entity.java プロジェクト: ItsLucas/Nukkit
  public static Entity createEntity(String name, FullChunk chunk, CompoundTag nbt, Object... args) {
    Entity entity = null;

    if (knownEntities.containsKey(name)) {
      Class<? extends Entity> clazz = knownEntities.get(name);

      if (clazz == null) {
        return null;
      }

      for (Constructor constructor : clazz.getConstructors()) {
        if (entity != null) {
          break;
        }

        if (constructor.getParameterCount() != (args == null ? 2 : args.length + 2)) {
          continue;
        }

        try {
          if (args == null || args.length == 0) {
            entity = (Entity) constructor.newInstance(chunk, nbt);
          } else {
            Object[] objects = new Object[args.length + 2];

            objects[0] = chunk;
            objects[1] = nbt;
            System.arraycopy(args, 0, objects, 2, args.length);
            entity = (Entity) constructor.newInstance(objects);
          }
        } catch (Exception e) {
          // ignore
        }
      }
    }

    return entity;
  }