/** * @author gabizou - February 7th, 2016 * <p>This will short circuit all other patches such that we control the entities being loaded * by chunkloading and can throw our bulk entity event. This will bypass Forge's hook for * individual entity events, but the SpongeModEventManager will still successfully throw the * appropriate event and cancel the entities otherwise contained. * @param entities The entities being loaded * @param callbackInfo The callback info */ @Final @Inject(method = "loadEntities", at = @At("HEAD"), cancellable = true) private void spongeLoadEntities( Collection<net.minecraft.entity.Entity> entities, CallbackInfo callbackInfo) { if (entities.isEmpty()) { // just return, no entities to load! callbackInfo.cancel(); return; } List<Entity> entityList = new ArrayList<>(); ImmutableList.Builder<EntitySnapshot> snapshotBuilder = ImmutableList.builder(); for (net.minecraft.entity.Entity entity : entities) { entityList.add((Entity) entity); snapshotBuilder.add(((Entity) entity).createSnapshot()); } SpawnCause cause = SpawnCause.builder().type(InternalSpawnTypes.CHUNK_LOAD).build(); List<NamedCause> causes = new ArrayList<>(); causes.add(NamedCause.source(cause)); causes.add(NamedCause.of("World", this)); SpawnEntityEvent.ChunkLoad chunkLoad = SpongeEventFactory.createSpawnEntityEventChunkLoad( Cause.of(causes), entityList, snapshotBuilder.build(), this); SpongeImpl.postEvent(chunkLoad); if (!chunkLoad.isCancelled()) { for (Entity successful : chunkLoad.getEntities()) { this.loadedEntityList.add((net.minecraft.entity.Entity) successful); this.onEntityAdded((net.minecraft.entity.Entity) successful); } } callbackInfo.cancel(); }
@Listener public void onSpawnEntityEvent(SpawnEntityEvent event, @Root SpawnCause cause) { WorldSettings settings = WorldSettings.get(event.getTargetWorld()); if (!settings.isVanillaSpawnerAllowed() && cause.getType().equals(SpawnTypes.MOB_SPAWNER)) { return; } else if (!settings.isModSpawnerAllowed() && cause.getType().equals(SpawnTypes.CUSTOM)) { return; } else if (!settings.isPluginSpawnerAllowed() && cause.getType().equals(SpawnTypes.PLUGIN)) { return; } else if (!settings.isEggSpawnerAllowed() && cause.getType().equals(SpawnTypes.SPAWN_EGG)) { return; } for (Entity entity : event.getEntities()) { entity.offer(Sponge.getDataManager().getManipulatorBuilder(MoneyData.class).get().create()); } }