Search in sources :

Example 1 with IMixinWorld

use of org.spongepowered.common.interfaces.world.IMixinWorld in project SpongeCommon by SpongePowered.

the class MixinSlotCrafting method afterTake.

/**
 * Create CraftItemEvent.Post result is also handled by
 * {@link MixinContainer#redirectTransferStackInSlot} or
 * {@link MixinContainer#redirectOnTakeThrow}
 */
@Inject(method = "onTake", cancellable = true, at = @At("RETURN"))
private void afterTake(EntityPlayer thePlayer, ItemStack stack, CallbackInfoReturnable<ItemStack> cir) {
    if (((IMixinWorld) thePlayer.world).isFake()) {
        return;
    }
    ((IMixinContainer) thePlayer.openContainer).detectAndSendChanges(true);
    ((IMixinContainer) thePlayer.openContainer).setCaptureInventory(false);
    Container container = thePlayer.openContainer;
    Inventory craftInv = ((Inventory) container).query(QueryOperationTypes.INVENTORY_TYPE.of(CraftingInventory.class));
    if (!(craftInv instanceof CraftingInventory)) {
        SpongeImpl.getLogger().warn("Detected crafting without a InventoryCrafting!? Crafting Event will not fire.");
        return;
    }
    // retain only last slot-transactions on output slot
    SlotTransaction first = null;
    List<SlotTransaction> capturedTransactions = ((IMixinContainer) container).getCapturedTransactions();
    for (Iterator<SlotTransaction> iterator = capturedTransactions.iterator(); iterator.hasNext(); ) {
        SlotTransaction trans = iterator.next();
        Optional<SlotIndex> slotIndex = trans.getSlot().getInventoryProperty(SlotIndex.class);
        if (slotIndex.isPresent() && slotIndex.get().getValue() == 0) {
            iterator.remove();
            if (first == null) {
                first = trans;
            }
        }
    }
    ItemStackSnapshot craftedItem;
    // if we got a transaction on the crafting-slot use this
    if (first != null) {
        capturedTransactions.add(first);
        craftedItem = first.getOriginal().copy();
    } else {
        if (stack.isEmpty()) {
            // if stack is empty this was probably shift-crafting so we use the captured itemstack instead
            craftedItem = ItemStackUtil.snapshotOf(this.craftedStack);
        } else {
            craftedItem = ItemStackUtil.snapshotOf(stack);
        }
    }
    CraftingInventory craftingInventory = (CraftingInventory) craftInv;
    CraftItemEvent.Craft event = SpongeCommonEventFactory.callCraftEventPost(thePlayer, craftingInventory, craftedItem, this.lastRecipe, container, capturedTransactions);
    ((IMixinContainer) container).setLastCraft(event);
    ((IMixinContainer) container).setFirePreview(true);
    this.craftedStack = null;
    SlotTransaction last = new SlotTransaction(craftingInventory.getResult(), ItemStackSnapshot.NONE, ItemStackUtil.snapshotOf(this.getStack()));
    List<SlotTransaction> previewTransactions = ((IMixinContainer) container).getPreviewTransactions();
    CraftingRecipe newRecipe = Sponge.getRegistry().getCraftingRecipeRegistry().findMatchingRecipe(craftingInventory.getCraftingGrid(), ((World) player.world)).orElse(null);
    SpongeCommonEventFactory.callCraftEventPre(thePlayer, craftingInventory, last, newRecipe, container, previewTransactions);
    previewTransactions.clear();
}
Also used : CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory) CraftingRecipe(org.spongepowered.api.item.recipe.crafting.CraftingRecipe) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) Container(net.minecraft.inventory.Container) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) CraftItemEvent(org.spongepowered.api.event.item.inventory.CraftItemEvent) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Inventory(org.spongepowered.api.item.inventory.Inventory) CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory) IInventory(net.minecraft.inventory.IInventory) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 2 with IMixinWorld

use of org.spongepowered.common.interfaces.world.IMixinWorld in project SpongeCommon by SpongePowered.

the class EntityActivationRange method activateEntities.

/**
 * Find what entities are in range of the players in the world and set
 * active if in range.
 *
 * @param world The world to perform activation checks in
 */
public static void activateEntities(World world) {
    if (((IMixinWorld) world).isFake()) {
        return;
    }
    for (EntityPlayer player : world.playerEntities) {
        int maxRange = 0;
        for (Integer range : maxActivationRanges.values()) {
            if (range > maxRange) {
                maxRange = range;
            }
        }
        maxRange = Math.min((((org.spongepowered.api.world.World) world).getViewDistance() << 4) - 8, maxRange);
        ((IModData_Activation) player).setActivatedTick(SpongeImpl.getServer().getTickCounter());
        growBb(maxBB, player.getEntityBoundingBox(), maxRange, 256, maxRange);
        int i = MathHelper.floor(maxBB.minX / 16.0D);
        int j = MathHelper.floor(maxBB.maxX / 16.0D);
        int k = MathHelper.floor(maxBB.minZ / 16.0D);
        int l = MathHelper.floor(maxBB.maxZ / 16.0D);
        for (int i1 = i; i1 <= j; ++i1) {
            for (int j1 = k; j1 <= l; ++j1) {
                WorldServer worldserver = (WorldServer) world;
                Chunk chunk = ((IMixinChunkProviderServer) worldserver.getChunkProvider()).getLoadedChunkWithoutMarkingActive(i1, j1);
                if (chunk != null) {
                    activateChunkEntities(player, chunk);
                }
            }
        }
    }
}
Also used : IModData_Activation(org.spongepowered.common.mixin.plugin.entityactivation.interfaces.IModData_Activation) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) EntityPlayer(net.minecraft.entity.player.EntityPlayer) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) WorldServer(net.minecraft.world.WorldServer) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) Chunk(net.minecraft.world.chunk.Chunk) IMixinChunkProviderServer(org.spongepowered.common.interfaces.world.gen.IMixinChunkProviderServer)

Example 3 with IMixinWorld

use of org.spongepowered.common.interfaces.world.IMixinWorld 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;
}
Also used : IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus) LoadWorldEvent(org.spongepowered.api.event.world.LoadWorldEvent) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) LoadWorldEvent(org.spongepowered.api.event.world.LoadWorldEvent) EntityJoinWorldEvent(net.minecraftforge.event.entity.EntityJoinWorldEvent) UnloadWorldEvent(org.spongepowered.api.event.world.UnloadWorldEvent) WorldEvent(net.minecraftforge.event.world.WorldEvent) SaveWorldEvent(org.spongepowered.api.event.world.SaveWorldEvent) TargetWorldEvent(org.spongepowered.api.event.world.TargetWorldEvent) World(org.spongepowered.api.world.World) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) IMixinChunkProviderServer(org.spongepowered.common.interfaces.world.gen.IMixinChunkProviderServer)

Example 4 with IMixinWorld

use of org.spongepowered.common.interfaces.world.IMixinWorld in project SpongeForge by SpongePowered.

the class MixinEntityPlayer method wakeUpPlayer.

/**
 * @author JBYoshi - November 23rd, 2015
 * @reason implement SpongeAPI events.
 *
 * @param immediately Whether to be woken up immediately
 * @param updateWorldFlag Whether to update the world
 * @param setSpawn Whether the player has successfully set up spawn
 */
@Overwrite
public void wakeUpPlayer(boolean immediately, boolean updateWorldFlag, boolean setSpawn) {
    IBlockState iblockstate = this.world.getBlockState(this.bedLocation);
    Transform<World> newLocation = null;
    if (this.bedLocation != null && iblockstate.getBlock().isBed(iblockstate, this.world, this.bedLocation, (Entity) (Object) this)) {
        iblockstate.getBlock().setBedOccupied(this.world, this.bedLocation, ((EntityPlayer) (Object) this), false);
        BlockPos blockpos = iblockstate.getBlock().getBedSpawnPosition(iblockstate, this.world, this.bedLocation, ((EntityPlayer) (Object) this));
        if (blockpos == null) {
            blockpos = ((EntityPlayer) (Object) this).bedLocation.up();
        }
        newLocation = this.getTransform().setPosition(new Vector3d(blockpos.getX() + 0.5F, blockpos.getY() + 0.1F, blockpos.getZ() + 0.5F));
    }
    SleepingEvent.Post post = null;
    if (!((IMixinWorld) this.world).isFake()) {
        try (final CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
            Sponge.getCauseStackManager().pushCause(this);
            post = SpongeEventFactory.createSleepingEventPost(Sponge.getCauseStackManager().getCurrentCause(), this.getWorld().createSnapshot(VecHelper.toVector3i(this.bedLocation)), Optional.ofNullable(newLocation), this, setSpawn);
            Sponge.getEventManager().post(post);
            if (post.isCancelled()) {
                return;
            }
            ForgeEventFactory.onPlayerWakeup(((EntityPlayer) (Object) this), immediately, updateWorldFlag, setSpawn);
            this.setSize(0.6F, 1.8F);
            if (post.getSpawnTransform().isPresent()) {
                this.setLocationAndAngles(post.getSpawnTransform().get());
            }
        }
    } else {
        ForgeEventFactory.onPlayerWakeup(((EntityPlayer) (Object) this), immediately, updateWorldFlag, setSpawn);
        this.setSize(0.6F, 1.8F);
    }
    this.sleeping = false;
    if (!this.world.isRemote && updateWorldFlag) {
        this.world.updateAllPlayersSleepingFlag();
    }
    this.sleepTimer = immediately ? 0 : 100;
    if (setSpawn) {
        this.setSpawnPoint(this.bedLocation, false);
    }
    if (post != null) {
        Sponge.getGame().getEventManager().post(SpongeEventFactory.createSleepingEventFinish(post.getCause(), this.getWorld().createSnapshot(VecHelper.toVector3i(this.bedLocation)), this));
    }
}
Also used : Entity(net.minecraft.entity.Entity) IBlockState(net.minecraft.block.state.IBlockState) Vector3d(com.flowpowered.math.vector.Vector3d) CauseStackManager(org.spongepowered.api.event.CauseStackManager) IMixinEntityPlayer(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayer) EntityPlayer(net.minecraft.entity.player.EntityPlayer) BlockPos(net.minecraft.util.math.BlockPos) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) World(org.spongepowered.api.world.World) SleepingEvent(org.spongepowered.api.event.action.SleepingEvent) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 5 with IMixinWorld

use of org.spongepowered.common.interfaces.world.IMixinWorld in project SpongeCommon by SpongePowered.

the class MixinEntityFallingBlock method onWorldSetBlockToAir.

@Inject(method = "onUpdate", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;setBlockToAir(Lnet/minecraft/util/math/BlockPos;)Z", shift = At.Shift.AFTER), cancellable = true)
private void onWorldSetBlockToAir(CallbackInfo ci) {
    final BlockPos pos = new BlockPos((EntityFallingBlock) (Object) this);
    if (((IMixinWorld) this.world).isFake()) {
        this.world.setBlockToAir(pos);
        return;
    }
    // Ideally, at this point we should still be in the EntityTickState and only this block should
    // be changing. What we need to do here is throw the block event specifically for setting air
    // and THEN if this one cancels, we should kill this entity off, unless we want some duplication
    // of falling blocks
    final PhaseData currentPhaseData = PhaseTracker.getInstance().getCurrentPhaseData();
    this.world.setBlockToAir(pos);
    // By this point, we should have one captured block at least.
    if (!TrackingUtil.processBlockCaptures(currentPhaseData.context.getCapturedBlocks(), currentPhaseData.state, currentPhaseData.context)) {
        // So, it's been cancelled, we want to absolutely remove this entity.
        // And we want to stop the entity update at this point.
        this.setDead();
        ci.cancel();
    }
}
Also used : PhaseData(org.spongepowered.common.event.tracking.PhaseData) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) BlockPos(net.minecraft.util.math.BlockPos) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

IMixinWorld (org.spongepowered.common.interfaces.world.IMixinWorld)7 EntityPlayer (net.minecraft.entity.player.EntityPlayer)3 World (org.spongepowered.api.world.World)3 Entity (net.minecraft.entity.Entity)2 BlockPos (net.minecraft.util.math.BlockPos)2 EntityJoinWorldEvent (net.minecraftforge.event.entity.EntityJoinWorldEvent)2 WorldEvent (net.minecraftforge.event.world.WorldEvent)2 LoadWorldEvent (org.spongepowered.api.event.world.LoadWorldEvent)2 SaveWorldEvent (org.spongepowered.api.event.world.SaveWorldEvent)2 TargetWorldEvent (org.spongepowered.api.event.world.TargetWorldEvent)2 UnloadWorldEvent (org.spongepowered.api.event.world.UnloadWorldEvent)2 Inject (org.spongepowered.asm.mixin.injection.Inject)2 IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)2 IMixinChunkProviderServer (org.spongepowered.common.interfaces.world.gen.IMixinChunkProviderServer)2 IModData_Activation (org.spongepowered.common.mixin.plugin.entityactivation.interfaces.IModData_Activation)2 IMixinEventBus (org.spongepowered.mod.interfaces.IMixinEventBus)2 Vector3d (com.flowpowered.math.vector.Vector3d)1 IBlockState (net.minecraft.block.state.IBlockState)1 MultiPartEntityPart (net.minecraft.entity.MultiPartEntityPart)1 EntityDragon (net.minecraft.entity.boss.EntityDragon)1