Search in sources :

Example 1 with EnumHand

use of net.minecraft.util.EnumHand in project AgriCraft by AgriCraft.

the class DebugModeCheckSoil method debugActionBlockClicked.

@Override
public void debugActionBlockClicked(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
    FuzzyStack soil = FuzzyStack.from(world.getBlockState(pos)).orElse(null);
    String type = AgriApi.getSoilRegistry().all().stream().filter(s -> s.isVarient(soil)).map(s -> s.getName()).findFirst().orElse("Unknown Soil");
    MessageUtil.messagePlayer(player, "{0} Soil Info:", FMLCommonHandler.instance().getSide());
    MessageUtil.messagePlayer(player, " - Soil Type: \"{0}\"", type);
}
Also used : ItemStack(net.minecraft.item.ItemStack) DebugMode(com.infinityraider.infinitylib.utility.debug.DebugMode) AgriApi(com.infinityraider.agricraft.api.v1.AgriApi) EntityLivingBase(net.minecraft.entity.EntityLivingBase) EntityPlayer(net.minecraft.entity.player.EntityPlayer) World(net.minecraft.world.World) EnumHand(net.minecraft.util.EnumHand) EnumFacing(net.minecraft.util.EnumFacing) FMLCommonHandler(net.minecraftforge.fml.common.FMLCommonHandler) BlockPos(net.minecraft.util.math.BlockPos) FuzzyStack(com.infinityraider.agricraft.api.v1.util.FuzzyStack) MessageUtil(com.infinityraider.infinitylib.utility.MessageUtil) FuzzyStack(com.infinityraider.agricraft.api.v1.util.FuzzyStack)

Example 2 with EnumHand

use of net.minecraft.util.EnumHand in project MorePlanets by SteveKunG.

the class GeneralEventHandler method setFarmland.

private void setFarmland(UseHoeEvent event, World world, BlockPos pos, IBlockState state, IProperty<?> property, Object value, Block dirt, Block farmland) {
    if (state.getValue(property) == value) {
        world.setBlockState(pos, dirt.getDefaultState());
    } else {
        world.setBlockState(pos, farmland.getDefaultState());
    }
    event.setResult(Result.ALLOW);
    world.playSound(null, pos.getX(), pos.getY(), pos.getZ(), SoundType.GROUND.getStepSound(), SoundCategory.BLOCKS, (SoundType.GROUND.getVolume() + 1.0F) / 2.0F, SoundType.GROUND.getPitch() * 0.8F);
    for (EnumHand hand : CachedEnumUtil.valuesHandCached()) {
        event.getEntityPlayer().swingArm(hand);
    }
}
Also used : EnumHand(net.minecraft.util.EnumHand)

Example 3 with EnumHand

use of net.minecraft.util.EnumHand in project SpongeCommon by SpongePowered.

the class MixinNetHandlerPlayServer method processUseEntity.

/**
 * @author blood - April 5th, 2016
 *
 * @reason Due to all the changes we now do for this packet, it is much easier
 * to read it all with an overwrite. Information detailing on why each change
 * was made can be found in comments below.
 *
 * @param packetIn The entity use packet
 */
@Overwrite
public void processUseEntity(CPacketUseEntity packetIn) {
    // All packets received by server are handled first on the Netty Thread
    if (!SpongeImpl.getServer().isCallingFromMinecraftThread()) {
        if (packetIn.getAction() == CPacketUseEntity.Action.INTERACT) {
            // when INTERACT_AT does not return a successful result.
            return;
        } else {
            // queue packet for main thread
            PacketThreadUtil.checkThreadAndEnqueue(packetIn, (NetHandlerPlayServer) (Object) this, this.player.getServerWorld());
            return;
        }
    }
    // Sponge end
    WorldServer worldserver = this.serverController.getWorld(this.player.dimension);
    Entity entity = packetIn.getEntityFromWorld(worldserver);
    this.player.markPlayerActive();
    if (entity != null) {
        boolean flag = this.player.canEntityBeSeen(entity);
        // 6 blocks
        double d0 = 36.0D;
        if (!flag) {
            // 1.5 blocks
            d0 = 9.0D;
        }
        if (this.player.getDistanceSq(entity) < d0) {
            if (packetIn.getAction() == CPacketUseEntity.Action.INTERACT_AT) {
                // Sponge start - Fire interact events
                EnumHand hand = packetIn.getHand();
                ItemStack itemstack = hand != null ? this.player.getHeldItem(hand) : ItemStack.EMPTY;
                Sponge.getCauseStackManager().addContext(EventContextKeys.USED_ITEM, ItemStackUtil.snapshotOf(itemstack));
                SpongeCommonEventFactory.lastSecondaryPacketTick = this.serverController.getTickCounter();
                // Is interaction allowed with item in hand
                if (SpongeCommonEventFactory.callInteractItemEventSecondary(this.player, itemstack, hand, VecHelper.toVector3d(packetIn.getHitVec()), entity).isCancelled() || SpongeCommonEventFactory.callInteractEntityEventSecondary(this.player, entity, hand, VecHelper.toVector3d(entity.getPositionVector().add(packetIn.getHitVec()))).isCancelled()) {
                    // Restore held item in hand
                    int index = ((IMixinInventoryPlayer) this.player.inventory).getHeldItemIndex(hand);
                    Slot slot = this.player.openContainer.getSlotFromInventory(this.player.inventory, index);
                    sendPacket(new SPacketSetSlot(this.player.openContainer.windowId, slot.slotNumber, itemstack));
                    // which means that we need to force an update
                    if (itemstack.getItem() == Items.LEAD) {
                        // Detach entity again
                        sendPacket(new SPacketEntityAttach(entity, null));
                    } else {
                        // Other cases may involve a specific DataParameter of the entity
                        // We fix the client state by marking it as dirty so it will be updated on the client the next tick
                        DataParameter<?> parameter = findModifiedEntityInteractDataParameter(itemstack, entity);
                        if (parameter != null) {
                            entity.getDataManager().setDirty(parameter);
                        }
                    }
                    return;
                }
                // If INTERACT_AT is not successful, run the INTERACT logic
                if (entity.applyPlayerInteraction(this.player, packetIn.getHitVec(), hand) != EnumActionResult.SUCCESS) {
                    this.player.interactOn(entity, hand);
                }
            // Sponge end
            } else if (packetIn.getAction() == CPacketUseEntity.Action.ATTACK) {
                // Sponge start - Call interact event
                // Will be null in the packet during ATTACK
                EnumHand hand = EnumHand.MAIN_HAND;
                ItemStack itemstack = this.player.getHeldItem(hand);
                SpongeCommonEventFactory.lastPrimaryPacketTick = this.serverController.getTickCounter();
                Vector3d hitVec = null;
                if (packetIn.getHitVec() == null) {
                    final RayTraceResult result = SpongeImplHooks.rayTraceEyes(player, SpongeImplHooks.getBlockReachDistance(player));
                    hitVec = result == null ? null : VecHelper.toVector3d(result.hitVec);
                }
                if (SpongeCommonEventFactory.callInteractItemEventPrimary(this.player, itemstack, hand, hitVec, entity).isCancelled()) {
                    ((IMixinEntityPlayerMP) this.player).restorePacketItem(hand);
                    return;
                }
                if (entity instanceof EntityItem || entity instanceof EntityXPOrb || entity instanceof EntityArrow || entity == this.player) {
                    this.disconnect(new TextComponentTranslation("multiplayer.disconnect.invalid_entity_attacked"));
                    this.serverController.logWarning("Player " + this.player.getName() + " tried to attack an invalid entity");
                    return;
                }
                // Sponge start
                if (entity instanceof Player && !((World) this.player.world).getProperties().isPVPEnabled()) {
                    // PVP is disabled, ignore
                    return;
                }
                if (SpongeCommonEventFactory.callInteractEntityEventPrimary(this.player, entity, hand, hitVec).isCancelled()) {
                    ((IMixinEntityPlayerMP) this.player).restorePacketItem(hand);
                    return;
                }
                // Sponge end
                this.player.attackTargetEntityWithCurrentItem(entity);
            }
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) CPacketUseEntity(net.minecraft.network.play.client.CPacketUseEntity) Entity(net.minecraft.entity.Entity) EntityArrow(net.minecraft.entity.projectile.EntityArrow) TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) Player(org.spongepowered.api.entity.living.player.Player) IMixinEntityPlayer(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayer) EntityPlayer(net.minecraft.entity.player.EntityPlayer) CPacketPlayer(net.minecraft.network.play.client.CPacketPlayer) IMixinInventoryPlayer(org.spongepowered.common.interfaces.entity.player.IMixinInventoryPlayer) RayTraceResult(net.minecraft.util.math.RayTraceResult) WorldServer(net.minecraft.world.WorldServer) World(org.spongepowered.api.world.World) SPacketEntityAttach(net.minecraft.network.play.server.SPacketEntityAttach) IMixinInventoryPlayer(org.spongepowered.common.interfaces.entity.player.IMixinInventoryPlayer) Vector3d(com.flowpowered.math.vector.Vector3d) EnumHand(net.minecraft.util.EnumHand) SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) Slot(net.minecraft.inventory.Slot) SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem) EntityXPOrb(net.minecraft.entity.item.EntityXPOrb) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 4 with EnumHand

use of net.minecraft.util.EnumHand in project SpongeCommon by SpongePowered.

the class PlaceBlockPacketState method unwind.

@Override
public void unwind(BasicPacketContext context) {
    final Packet<?> packet = context.getPacket();
    final EntityPlayerMP player = context.getPacketPlayer();
    final IMixinWorldServer mixinWorld = (IMixinWorldServer) player.world;
    // Note - CPacketPlayerTryUseItem is swapped with
    // CPacketPlayerBlockPlacement
    final ItemStack itemStack = context.getItemUsed();
    final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(itemStack);
    context.getCapturedEntitySupplier().acceptAndClearIfNotEmpty(entities -> {
        try (@SuppressWarnings("unused") CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
            Sponge.getCauseStackManager().pushCause(player);
            Sponge.getCauseStackManager().pushCause(snapshot);
            Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.SPAWN_EGG);
            final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), entities);
            SpongeImpl.postEvent(spawnEntityEvent);
            if (!spawnEntityEvent.isCancelled()) {
                processSpawnedEntities(player, spawnEntityEvent);
            }
        }
    });
    context.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(originalBlocks -> {
        Sponge.getCauseStackManager().pushCause(player);
        boolean success = TrackingUtil.processBlockCaptures(originalBlocks, this, context);
        if (!success && snapshot != ItemTypeRegistryModule.NONE_SNAPSHOT) {
            Sponge.getCauseStackManager().pushCause(player);
            EnumHand hand = ((CPacketPlayerTryUseItemOnBlock) packet).getHand();
            PacketPhaseUtil.handlePlayerSlotRestore(player, (net.minecraft.item.ItemStack) itemStack, hand);
        }
        Sponge.getCauseStackManager().popCause();
    });
    context.getCapturedItemStackSupplier().acceptAndClearIfNotEmpty(drops -> {
        final List<Entity> entities = drops.stream().map(drop -> drop.create(player.getServerWorld())).map(EntityUtil::fromNative).collect(Collectors.toList());
        if (!entities.isEmpty()) {
            try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
                Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.PLACEMENT);
                Sponge.getCauseStackManager().pushCause(player);
                DropItemEvent.Custom event = SpongeEventFactory.createDropItemEventCustom(Sponge.getCauseStackManager().getCurrentCause(), entities);
                SpongeImpl.postEvent(event);
                if (!event.isCancelled()) {
                    for (Entity droppedItem : event.getEntities()) {
                        droppedItem.setCreator(player.getUniqueID());
                        mixinWorld.forceSpawnEntity(droppedItem);
                    }
                }
            }
        }
    });
    final IMixinContainer mixinContainer = ContainerUtil.toMixin(player.openContainer);
    mixinContainer.setCaptureInventory(false);
    mixinContainer.getCapturedTransactions().clear();
}
Also used : Entity(org.spongepowered.api.entity.Entity) DropItemEvent(org.spongepowered.api.event.item.inventory.DropItemEvent) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) CPacketPlayerTryUseItemOnBlock(net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock) IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) CauseStackManager(org.spongepowered.api.event.CauseStackManager) EnumHand(net.minecraft.util.EnumHand) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 5 with EnumHand

use of net.minecraft.util.EnumHand in project SilentGems by SilentChaos512.

the class GuiHandlerSilentGems method getClientGuiElement.

@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
    TileEntity tile = world.getTileEntity(new BlockPos(x, y, z));
    if (ID != ID_QUIVER && tile == null) {
        SilentGems.logHelper.warning(String.format("Missing TileEntity at %d %d %d!", x, y, z));
        return null;
    }
    switch(ID) {
        case ID_ALTAR:
            if (tile instanceof TileChaosAltar) {
                TileChaosAltar tileAltar = (TileChaosAltar) tile;
                return new GuiChaosAltar(player.inventory, tileAltar);
            }
            return null;
        case ID_BURNER_PYLON:
            if (tile instanceof TileChaosPylon) {
                return new GuiBurnerPylon(player.inventory, (TileChaosPylon) tile);
            }
            return null;
        case ID_MATERIAL_GRADER:
            if (tile instanceof TileMaterialGrader) {
                return new GuiMaterialGrader(player.inventory, (TileMaterialGrader) tile);
            }
            return null;
        case ID_QUIVER:
            EnumHand hand = x == 1 ? EnumHand.OFF_HAND : EnumHand.MAIN_HAND;
            ItemStack stack = player.getHeldItem(hand);
            return new GuiQuiver(new ContainerQuiver(stack, player.inventory, hand));
        default:
            SilentGems.logHelper.warning("No GUI with ID " + ID + "!");
            return null;
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) ContainerQuiver(net.silentchaos512.gems.inventory.ContainerQuiver) TileChaosAltar(net.silentchaos512.gems.tile.TileChaosAltar) EnumHand(net.minecraft.util.EnumHand) TileChaosPylon(net.silentchaos512.gems.tile.TileChaosPylon) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) TileMaterialGrader(net.silentchaos512.gems.tile.TileMaterialGrader)

Aggregations

EnumHand (net.minecraft.util.EnumHand)40 ItemStack (net.minecraft.item.ItemStack)30 EntityPlayer (net.minecraft.entity.player.EntityPlayer)23 BlockPos (net.minecraft.util.math.BlockPos)18 World (net.minecraft.world.World)13 EnumFacing (net.minecraft.util.EnumFacing)11 IBlockState (net.minecraft.block.state.IBlockState)9 Entity (net.minecraft.entity.Entity)9 TileEntity (net.minecraft.tileentity.TileEntity)9 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)9 SideOnly (net.minecraftforge.fml.relauncher.SideOnly)8 Block (net.minecraft.block.Block)7 EntityLivingBase (net.minecraft.entity.EntityLivingBase)7 Item (net.minecraft.item.Item)7 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)5 EnumActionResult (net.minecraft.util.EnumActionResult)5 RayTraceResult (net.minecraft.util.math.RayTraceResult)5 Side (net.minecraftforge.fml.relauncher.Side)5 Nullable (javax.annotation.Nullable)4 List (java.util.List)3