Search in sources :

Example 6 with IMixinInventoryPlayer

use of org.spongepowered.common.interfaces.entity.player.IMixinInventoryPlayer in project SpongeCommon by SpongePowered.

the class CommandState method unwind.

@Override
public void unwind(CommandPhaseContext phaseContext) {
    Optional<EntityPlayer> playerSource = phaseContext.getSource(EntityPlayer.class);
    if (playerSource.isPresent()) {
        // Post event for inventory changes
        ((IMixinInventoryPlayer) playerSource.get().inventory).setCapture(false);
        List<SlotTransaction> list = ((IMixinInventoryPlayer) playerSource.get().inventory).getCapturedTransactions();
        if (!list.isEmpty()) {
            ChangeInventoryEvent event = SpongeEventFactory.createChangeInventoryEvent(Sponge.getCauseStackManager().getCurrentCause(), ((Inventory) playerSource.get().inventory), list);
            SpongeImpl.postEvent(event);
            PacketPhaseUtil.handleSlotRestore(playerSource.get(), null, list, event.isCancelled());
            list.clear();
        }
    }
    final CommandSource sender = phaseContext.getSource(CommandSource.class).orElseThrow(TrackingUtil.throwWithContext("Expected to be capturing a Command Sender, but none found!", phaseContext));
    phaseContext.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(list -> TrackingUtil.processBlockCaptures(list, this, phaseContext));
    try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().pushCause(sender);
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.PLACEMENT);
        phaseContext.getCapturedEntitySupplier().acceptAndClearIfNotEmpty(entities -> {
            // TODO the entity spawn causes are not likely valid,
            // need to investigate further.
            final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), entities);
            SpongeImpl.postEvent(spawnEntityEvent);
            if (!spawnEntityEvent.isCancelled()) {
                final boolean isPlayer = sender instanceof Player;
                final Player player = isPlayer ? (Player) sender : null;
                for (Entity entity : spawnEntityEvent.getEntities()) {
                    if (isPlayer) {
                        EntityUtil.toMixin(entity).setCreator(player.getUniqueId());
                    }
                    EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
                }
            }
        });
    }
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().pushCause(sender);
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.DROPPED_ITEM);
        phaseContext.getCapturedEntityDropSupplier().acceptIfNotEmpty(uuidItemStackMultimap -> {
            for (Map.Entry<UUID, Collection<ItemDropData>> entry : uuidItemStackMultimap.asMap().entrySet()) {
                final UUID key = entry.getKey();
                @Nullable net.minecraft.entity.Entity foundEntity = null;
                for (WorldServer worldServer : WorldManager.getWorlds()) {
                    final net.minecraft.entity.Entity entityFromUuid = worldServer.getEntityFromUuid(key);
                    if (entityFromUuid != null) {
                        foundEntity = entityFromUuid;
                        break;
                    }
                }
                final Optional<Entity> affectedEntity = Optional.ofNullable((Entity) foundEntity);
                if (!affectedEntity.isPresent()) {
                    continue;
                }
                final Collection<ItemDropData> itemStacks = entry.getValue();
                if (itemStacks.isEmpty()) {
                    return;
                }
                final List<ItemDropData> items = new ArrayList<>();
                items.addAll(itemStacks);
                itemStacks.clear();
                final WorldServer minecraftWorld = EntityUtil.getMinecraftWorld(affectedEntity.get());
                if (!items.isEmpty()) {
                    final List<Entity> itemEntities = items.stream().map(data -> data.create(minecraftWorld)).map(EntityUtil::fromNative).collect(Collectors.toList());
                    Sponge.getCauseStackManager().pushCause(affectedEntity.get());
                    final DropItemEvent.Destruct destruct = SpongeEventFactory.createDropItemEventDestruct(Sponge.getCauseStackManager().getCurrentCause(), itemEntities);
                    SpongeImpl.postEvent(destruct);
                    Sponge.getCauseStackManager().popCause();
                    if (!destruct.isCancelled()) {
                        final boolean isPlayer = sender instanceof Player;
                        final Player player = isPlayer ? (Player) sender : null;
                        for (Entity entity : destruct.getEntities()) {
                            if (isPlayer) {
                                EntityUtil.toMixin(entity).setCreator(player.getUniqueId());
                            }
                            EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
                        }
                    }
                }
            }
        });
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) ArrayList(java.util.ArrayList) WorldServer(net.minecraft.world.WorldServer) CauseStackManager(org.spongepowered.api.event.CauseStackManager) UUID(java.util.UUID) DropItemEvent(org.spongepowered.api.event.item.inventory.DropItemEvent) IMixinInventoryPlayer(org.spongepowered.common.interfaces.entity.player.IMixinInventoryPlayer) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Player(org.spongepowered.api.entity.living.player.Player) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) CommandSource(org.spongepowered.api.command.CommandSource) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) IMixinInventoryPlayer(org.spongepowered.common.interfaces.entity.player.IMixinInventoryPlayer) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ChangeInventoryEvent(org.spongepowered.api.event.item.inventory.ChangeInventoryEvent) Collection(java.util.Collection) ItemDropData(org.spongepowered.common.event.tracking.context.ItemDropData) Map(java.util.Map) Inventory(org.spongepowered.api.item.inventory.Inventory) Nullable(javax.annotation.Nullable)

Example 7 with IMixinInventoryPlayer

use of org.spongepowered.common.interfaces.entity.player.IMixinInventoryPlayer in project SpongeCommon by SpongePowered.

the class SwapHandItemsState method unwind.

@Override
public void unwind(InventoryPacketContext context) {
    final EntityPlayerMP player = context.getPacketPlayer();
    final Entity spongePlayer = EntityUtil.fromNative(player);
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        frame.pushCause(spongePlayer);
        final IMixinInventoryPlayer mixinInventory = ((IMixinInventoryPlayer) player.inventory);
        List<SlotTransaction> trans = mixinInventory.getCapturedTransactions();
        ChangeInventoryEvent.SwapHand swapItemEvent = this.createInventoryEvent(((Inventory) player.inventory), trans);
        SpongeImpl.postEvent(swapItemEvent);
        PacketPhaseUtil.handleSlotRestore(player, null, swapItemEvent.getTransactions(), swapItemEvent.isCancelled());
        mixinInventory.setCapture(false);
        mixinInventory.getCapturedTransactions().clear();
    }
}
Also used : IMixinInventoryPlayer(org.spongepowered.common.interfaces.entity.player.IMixinInventoryPlayer) Entity(org.spongepowered.api.entity.Entity) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ChangeInventoryEvent(org.spongepowered.api.event.item.inventory.ChangeInventoryEvent) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Inventory(org.spongepowered.api.item.inventory.Inventory)

Aggregations

IMixinInventoryPlayer (org.spongepowered.common.interfaces.entity.player.IMixinInventoryPlayer)7 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)4 EntityPlayer (net.minecraft.entity.player.EntityPlayer)3 ItemStack (net.minecraft.item.ItemStack)3 Inventory (org.spongepowered.api.item.inventory.Inventory)3 EntityItem (net.minecraft.entity.item.EntityItem)2 WorldServer (net.minecraft.world.WorldServer)2 Entity (org.spongepowered.api.entity.Entity)2 Player (org.spongepowered.api.entity.living.player.Player)2 CauseStackManager (org.spongepowered.api.event.CauseStackManager)2 StackFrame (org.spongepowered.api.event.CauseStackManager.StackFrame)2 ChangeInventoryEvent (org.spongepowered.api.event.item.inventory.ChangeInventoryEvent)2 Vector3d (com.flowpowered.math.vector.Vector3d)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Map (java.util.Map)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1