Ejemplo n.º 1
0
  @SuppressWarnings("unchecked")
  public static void register() throws IOException {
    Stopwatch watch = Stopwatch.createStarted();

    InputStream in = PoreEventWrapper.class.getResourceAsStream("events.txt");
    if (in == null) {
      Pore.getLogger().warn("No registered events found, Bukkit events will not be called.");
      return;
    }

    try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
      String line;
      while ((line = reader.readLine()) != null) {
        line = line.trim();
        if (line.isEmpty() || line.charAt(0) == '#') {
          continue;
        }

        try {
          register((Class<? extends Event>) Class.forName(line));
        } catch (ClassNotFoundException e) {
          Pore.getLogger().warn("Failed to register class {} as an event", line, e);
        }
      }
    }

    Pore.getLogger().debug("Registered events in {}", watch.stop());
  }
Ejemplo n.º 2
0
 @Override
 public void unregister(EventPriority priority) {
   Object listener = listeners.get(priority);
   if (listener != null) {
     Pore.getGame().getEventManager().unregisterListeners(listener);
   }
 }
Ejemplo n.º 3
0
 @SuppressWarnings({
   "rawtypes",
   "unchecked"
 }) // I can't parameterize this either; it scares the compiler
 private static <T extends VariantData<U, T, ?>, U extends CatalogType> T getItemData(
     ItemStack item, Class<T> type, BiMap<U, Integer> map) {
   int damage = item.getDurability();
   if (!map.containsValue(damage)) {
     throw new UnsupportedOperationException();
   }
   // no idea why a typecast is necessary here but excluding it makes javac angry
   @SuppressWarnings("RedundantCast")
   T data =
       (T)
           Pore.getGame()
               .getRegistry()
               .createItemBuilder()
               .itemType(MaterialConverter.asItem(item.getType()))
               .quantity(1)
               .build()
               .getOrCreate(type)
               .get();
   data.type().set(map.inverse().get(damage));
   return data;
 }
Ejemplo n.º 4
0
    @Override
    public void unregister() {
      EventManager manager = Pore.getGame().getEventManager();

      for (Object listener : listeners.values()) {
        manager.unregisterListeners(listener);
      }
    }
Ejemplo n.º 5
0
    @Override
    public void register(EventPriority priority) {
      PoreEventHandler<S> listener = listeners.get(priority);
      if (listener == null) {
        if (constructor == null) {
          this.constructor = PoreConstructors.create(pore, sponge);
        }

        listener = new PoreEventHandler<>(priority, constructor);
        listeners.put(priority, listener);
      }

      Pore.getGame()
          .getEventManager()
          .registerListener(
              Pore.getPlugin(), sponge, EventPriorityConverter.of(priority), listener);
    }
Ejemplo n.º 6
0
 final boolean register(Class<S> event) {
   if (this.registered) {
     return false;
   } else {
     this.registered = true;
     Sponge.getEventManager()
         .registerListener(
             Pore.getPlugin(), event, EventPriorityConverter.of(this.priority), this);
     return true;
   }
 }
Ejemplo n.º 7
0
  public static void call(Event event, EventPriority priority) {
    for (RegisteredListener registration : event.getHandlers().getRegisteredListeners(priority)) {
      if (!registration.getPlugin().isEnabled()) {
        continue;
      }

      try {
        registration.callEvent(event);
      } catch (AuthorNagException ex) {
        Plugin plugin = registration.getPlugin();

        if (plugin.isNaggable()) {
          plugin.setNaggable(false);

          Pore.getServer()
              .getLogger()
              .log(
                  Level.SEVERE,
                  String.format(
                      "Nag author(s): '%s' of '%s' about the following: %s",
                      plugin.getDescription().getAuthors(),
                      plugin.getDescription().getFullName(),
                      ex.getMessage()));
        }
      } catch (EventException ex) {
        Pore.getServer()
            .getLogger()
            .log(
                Level.SEVERE,
                "Could not pass event "
                    + event.getEventName()
                    + " to "
                    + registration.getPlugin().getDescription().getFullName(),
                ex);
      }
    }
  }
Ejemplo n.º 8
0
 /**
  * Obtains {@link DataManipulator} from an ItemStack.
  *
  * @param item The ItemStack to retrieve {@link DataManipulator} from
  * @return The obtained {@link DataManipulator}, or {@code null} if none can be discerned
  */
 @SuppressWarnings(
     "rawtypes") // I tried parameterizing the return value but Java absolutely spazzed out about
                 // it
 public static DataManipulator getItemData(ItemStack item) {
   switch (item.getType()) {
     case COAL:
       return getItemData(item, COAL_ITEM_DATA, COAL_MAP);
     case COOKED_FISH:
       return getItemData(item, COOKED_FISH_ITEM_DATA, COOKED_FISH_MAP);
     case WOOL:
       break;
     case INK_SACK:
       break;
     case STAINED_CLAY:
       break;
     case STAINED_GLASS:
       break;
     case STAINED_GLASS_PANE:
       return getItemData(item, DYEABLE_DATA, DYE_MAP);
     case RAW_FISH:
       return getItemData(item, FISH_DATA, FISH_MAP);
     case GOLDEN_APPLE:
       return getItemData(item, GOLDEN_APPLE_ITEM_DATA, GOLDEN_APPLE_MAP);
     default:
       Optional<DurabilityData> data =
           Pore.getGame()
               .getRegistry()
               .createItemBuilder()
               .itemType(MaterialConverter.asItem(item.getType()))
               .quantity(1)
               .build()
               .get(DURABILITY_DATA);
       if (data.isPresent()) {
         return data.get();
       } else {
         throw new UnsupportedOperationException();
       }
   }
   return null;
 }