use of org.spongepowered.mod.interfaces.IMixinEventBus in project SpongeForge by SpongePowered.
the class SpongeForgeEventFactory method callWorldLoadEvent.
private static LoadWorldEvent callWorldLoadEvent(Event event) {
LoadWorldEvent spongeEvent = (LoadWorldEvent) event;
// Since Forge only uses a single save handler, we need to make sure to pass the overworld's handler.
// This makes sure that mods dont attempt to save/read their data from the wrong location.
final net.minecraft.world.World minecraftWorld = (net.minecraft.world.World) spongeEvent.getTargetWorld();
((IMixinWorld) spongeEvent.getTargetWorld()).setCallingWorldEvent(true);
((IMixinChunkProviderServer) minecraftWorld.getChunkProvider()).setForceChunkRequests(true);
((IMixinEventBus) MinecraftForge.EVENT_BUS).post(new WorldEvent.Load(minecraftWorld), true);
((IMixinChunkProviderServer) minecraftWorld.getChunkProvider()).setForceChunkRequests(false);
((IMixinWorld) minecraftWorld).setCallingWorldEvent(false);
return spongeEvent;
}
use of org.spongepowered.mod.interfaces.IMixinEventBus in project SpongeForge by SpongePowered.
the class SpongeForgeEventFactory method handleCustomStack.
// Copied from ForgeInternalHandler.onEntityJoinWorld, but with modifications
private static void handleCustomStack(SpawnEntityEvent event) {
// Sponge start - iterate over entities
ListIterator<org.spongepowered.api.entity.Entity> it = event.getEntities().listIterator();
while (it.hasNext()) {
// Sponge - use entity from event
Entity entity = (Entity) it.next();
if (entity.getClass().equals(EntityItem.class)) {
ItemStack stack = ((EntityItem) entity).getItem();
if (stack.isEmpty()) {
// Sponge - continue instead of return
continue;
}
Item item = stack.getItem();
if (item == null) {
// Sponge - technically, this check is in the wrong place, as it normally runs in Forge's listener (i.e. after 'beforeModifications' listener)
// However, it's really only a sanity check for something that should never happen, so it's fine
FMLLog.warning("Attempted to add a EntityItem to the world with a invalid item at " + "(%2.2f, %2.2f, %2.2f), this is most likely a config issue between you and the server. Please double check your configs", entity.posX, entity.posY, entity.posZ);
entity.setDead();
// Sponge - use our setCancelled method
event.setCancelled(true);
// Sponge - continue instead of return
continue;
}
if (item.hasCustomEntity(stack)) {
// Sponge - use world from entity
Entity newEntity = item.createEntity(entity.getEntityWorld(), entity, stack);
if (newEntity != null) {
entity.setDead();
// event.setCanceled(true); Sponge - don't cancel the event
// Sponge start - fire cancelled event, and return new event.
// We don't need to set 'StaticMixinForgeHelper.preventInternalForgeEntityListener' to 'true',
// since Forge's listener only handled uncancelled events
EntityJoinWorldEvent cancelledEvent = new EntityJoinWorldEvent(entity, entity.getEntityWorld());
cancelledEvent.setCanceled(true);
((IMixinEventBus) MinecraftForge.EVENT_BUS).post(cancelledEvent, true);
if (!cancelledEvent.isCanceled()) {
SpongeImpl.getLogger().error("A mod has un-cancelled the EntityJoinWorld event for the original EntityItem (from before Item#createEntity is called). This is almost certainly a terrible idea!");
}
it.set((org.spongepowered.api.entity.Entity) newEntity);
continue;
// Sponge end
}
}
}
}
}
use of org.spongepowered.mod.interfaces.IMixinEventBus in project SpongeForge by SpongePowered.
the class SpongeForgeEventFactory method callItemTossEvent.
public static DropItemEvent.Dispense callItemTossEvent(Event event) {
DropItemEvent.Dispense spongeEvent = (DropItemEvent.Dispense) event;
if (spongeEvent.getEntities().size() <= 0) {
return spongeEvent;
}
final Cause cause = spongeEvent.getCause();
Object source = cause.root();
Optional<DamageSource> damageSource = cause.first(DamageSource.class);
final Optional<Entity> spawnEntity = cause.first(Entity.class);
final Optional<SpawnType> spawnType = cause.getContext().get(EventContextKeys.SPAWN_TYPE);
if (!spawnType.isPresent() || !damageSource.isPresent() || !spawnEntity.isPresent()) {
// Mods expect EntityJoinWorldEvent to trigger
callEntityJoinWorldEvent(spongeEvent);
return spongeEvent;
}
Entity entity = spawnEntity.get();
EntityItem item = (EntityItem) spongeEvent.getEntities().get(0);
if (entity == null || item == null || item.getItem() == null || !(entity instanceof Player)) {
return spongeEvent;
}
ItemTossEvent forgeEvent = new ItemTossEvent(item, (EntityPlayerMP) entity);
((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
if (forgeEvent.isCanceled()) {
if (item.isDead) {
// Mods such as Flux-Networks kills the entity item to spawn a custom one
return spongeEvent;
}
spongeEvent.setCancelled(true);
} else {
// Forge treats EntityJoinWorldEvent separately from Toss so we need to call it here
callEntityJoinWorldEvent(spongeEvent);
}
return spongeEvent;
}
use of org.spongepowered.mod.interfaces.IMixinEventBus in project SpongeForge by SpongePowered.
the class SpongeForgeEventFactory method callEntityJoinWorldEvent.
public static SpawnEntityEvent callEntityJoinWorldEvent(Event event) {
SpawnEntityEvent spongeEvent = (SpawnEntityEvent) event;
ListIterator<org.spongepowered.api.entity.Entity> iterator = spongeEvent.getEntities().listIterator();
if (spongeEvent.getEntities().size() == 0) {
return spongeEvent;
}
// used to avoid player item restores when set to dead
boolean canCancelEvent = true;
while (iterator.hasNext()) {
org.spongepowered.api.entity.Entity entity = iterator.next();
EntityJoinWorldEvent forgeEvent = new EntityJoinWorldEvent((Entity) entity, (net.minecraft.world.World) entity.getLocation().getExtent());
boolean prev = StaticMixinForgeHelper.preventInternalForgeEntityListener;
StaticMixinForgeHelper.preventInternalForgeEntityListener = true;
((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
StaticMixinForgeHelper.preventInternalForgeEntityListener = prev;
Entity mcEntity = (Entity) entity;
if (mcEntity.isDead) {
// Don't restore packet item if a mod wants it dead
// Mods such as Flux-Networks kills the entity item to spawn a custom one
canCancelEvent = false;
}
if (forgeEvent.isCanceled()) {
iterator.remove();
}
}
if (spongeEvent.getEntities().size() == 0 && canCancelEvent) {
spongeEvent.setCancelled(true);
}
return spongeEvent;
}
use of org.spongepowered.mod.interfaces.IMixinEventBus in project SpongeForge by SpongePowered.
the class SpongeForgeEventFactory method callExplosionEventDetonate.
@SuppressWarnings("unchecked")
private static ExplosionEvent.Detonate callExplosionEventDetonate(Event event) {
ExplosionEvent.Detonate spongeEvent = (ExplosionEvent.Detonate) event;
Explosion explosion = (Explosion) spongeEvent.getExplosion();
net.minecraftforge.event.world.ExplosionEvent.Detonate forgeEvent = new net.minecraftforge.event.world.ExplosionEvent.Detonate((net.minecraft.world.World) spongeEvent.getTargetWorld(), explosion, (List<Entity>) (List<?>) spongeEvent.getEntities());
explosion.getAffectedBlockPositions().clear();
for (Location<World> x : spongeEvent.getAffectedLocations()) {
explosion.getAffectedBlockPositions().add(VecHelper.toBlockPos(x.getPosition()));
}
((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
if (forgeEvent.isCanceled()) {
spongeEvent.setCancelled(true);
}
((ExplosionEvent.Detonate) event).getAffectedLocations().clear();
for (BlockPos pos : forgeEvent.getAffectedBlocks()) {
((ExplosionEvent.Detonate) event).getAffectedLocations().add(new Location<>(spongeEvent.getTargetWorld(), VecHelper.toVector3i(pos)));
}
return spongeEvent;
}
Aggregations