use of net.minecraft.util.EnumHand in project SpongeForge by SpongePowered.
the class SpongeToForgeEventFactory method createAndPostBlockPlaceEvent.
@SuppressWarnings("deprecation")
private static boolean createAndPostBlockPlaceEvent(final SpongeToForgeEventData eventData) {
final ChangeBlockEvent.Place spongeEvent = (ChangeBlockEvent.Place) eventData.getSpongeEvent();
BlockEvent.PlaceEvent forgeEvent = (BlockEvent.PlaceEvent) eventData.getForgeEvent();
if (!(spongeEvent.getCause().root() instanceof Player)) {
return false;
}
if (forgeEvent == null) {
final EntityPlayer player = (EntityPlayer) spongeEvent.getCause().root();
final net.minecraft.world.World world = player.world;
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
final PhaseContext<?> currentContext = phaseTracker.getCurrentContext();
PhaseContext<?> target = currentContext;
if (currentContext instanceof UnwindingPhaseContext) {
target = ((UnwindingPhaseContext) currentContext).getUnwindingContext();
}
final PacketContext<?> context = target instanceof PacketContext<?> ? (PacketContext<?>) target : null;
final Packet<?> contextPacket = context != null ? context.getPacket() : null;
if (contextPacket == null) {
return false;
}
if (spongeEvent.getTransactions().size() == 1) {
final BlockPos pos = VecHelper.toBlockPos(spongeEvent.getTransactions().get(0).getOriginal().getPosition());
final IBlockState state = (IBlockState) spongeEvent.getTransactions().get(0).getOriginal().getState();
final net.minecraftforge.common.util.BlockSnapshot blockSnapshot = new net.minecraftforge.common.util.BlockSnapshot(world, pos, state);
IBlockState placedAgainst = Blocks.AIR.getDefaultState();
EnumHand hand = EnumHand.MAIN_HAND;
if (contextPacket instanceof CPacketPlayerTryUseItemOnBlock) {
final CPacketPlayerTryUseItemOnBlock packet = (CPacketPlayerTryUseItemOnBlock) contextPacket;
final EnumFacing facing = packet.getDirection();
placedAgainst = blockSnapshot.getWorld().getBlockState(blockSnapshot.getPos().offset(facing.getOpposite()));
hand = packet.getHand();
}
forgeEvent = new BlockEvent.PlaceEvent(blockSnapshot, placedAgainst, player, hand);
eventData.setForgeEvent(forgeEvent);
} else {
// multi
final Iterator<Transaction<BlockSnapshot>> iterator = spongeEvent.getTransactions().iterator();
final List<net.minecraftforge.common.util.BlockSnapshot> blockSnapshots = new ArrayList<>();
while (iterator.hasNext()) {
final Transaction<BlockSnapshot> transaction = iterator.next();
final Location<World> location = transaction.getOriginal().getLocation().get();
final IBlockState state = (IBlockState) transaction.getOriginal().getState();
final BlockPos pos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
final net.minecraftforge.common.util.BlockSnapshot blockSnapshot = new net.minecraftforge.common.util.BlockSnapshot(world, pos, state);
blockSnapshots.add(blockSnapshot);
}
IBlockState placedAgainst = Blocks.AIR.getDefaultState();
EnumHand hand = EnumHand.MAIN_HAND;
if (contextPacket instanceof CPacketPlayerTryUseItemOnBlock) {
final CPacketPlayerTryUseItemOnBlock packet = (CPacketPlayerTryUseItemOnBlock) contextPacket;
final EnumFacing facing = packet.getDirection();
placedAgainst = blockSnapshots.get(0).getWorld().getBlockState(blockSnapshots.get(0).getPos().offset(facing.getOpposite()));
hand = packet.getHand();
}
forgeEvent = new BlockEvent.MultiPlaceEvent(blockSnapshots, placedAgainst, player, hand);
eventData.setForgeEvent(forgeEvent);
}
}
forgeEventBus.forgeBridge$post(eventData);
return true;
}
use of net.minecraft.util.EnumHand in project PizzaCraft by Tiviacz1337.
the class CommonEventHandler method onShieldBlock.
@SubscribeEvent
public static void onShieldBlock(LivingAttackEvent event) {
ItemStack activeItemStack;
EntityPlayer player;
if (!(event.getEntityLiving() instanceof EntityPlayer)) {
return;
}
player = (EntityPlayer) event.getEntityLiving();
if (player.getActiveItemStack() == null) {
return;
}
activeItemStack = player.getActiveItemStack();
float damage = event.getAmount();
if (damage > 0.0F && activeItemStack != null && activeItemStack.getItem() instanceof ItemPizzaShield) {
int i = 1 + MathHelper.floor(damage);
activeItemStack.damageItem(i, player);
if (activeItemStack.getCount() <= 0) {
EnumHand enumhand = player.getActiveHand();
ForgeEventFactory.onPlayerDestroyItem(player, activeItemStack, enumhand);
if (enumhand == EnumHand.MAIN_HAND) {
player.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, ItemStack.EMPTY);
} else {
player.setItemStackToSlot(EntityEquipmentSlot.OFFHAND, ItemStack.EMPTY);
}
activeItemStack = ItemStack.EMPTY;
if (player.world.isRemote) {
player.playSound(SoundEvents.ITEM_SHIELD_BREAK, 0.8F, 0.8F + player.world.rand.nextFloat() * 0.4F);
}
}
}
}
use of net.minecraft.util.EnumHand in project takumicraft by TNTModders.
the class BlockTakumiAltar method onBlockActivated.
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
boolean flg = super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);
Entity entity = null;
if (canSpawnWither(worldIn, pos)) {
spawnWither(worldIn, pos);
return true;
} else if (worldIn.getBlockState(pos.down()).getBlock() == TakumiBlockCore.CREEPER_BOMB) {
entity = new EntityKingCreeper(worldIn);
} else if (worldIn.getBlockState(pos.down()).getBlock() == Blocks.EMERALD_BLOCK) {
entity = new EntityAngelCreeper(worldIn);
} else if (worldIn.getBlockState(pos.down()).getBlock() == TakumiBlockCore.MAGIC_BLOCK) {
entity = new EntityGemCreeper(worldIn);
} else if (worldIn.getBlockState(pos.up()).getBlock() == Blocks.CAKE) {
entity = new EntityAnnivCreeper(worldIn);
} else if (worldIn.getBlockState(pos.down()).getBlock() == Blocks.DIAMOND_BLOCK) {
entity = new EntitySuperDiamondCreeper(worldIn);
} else {
List<ITakumiEntity> entities = new ArrayList<>();
TakumiEntityCore.getEntityList().forEach(iTakumiEntity -> {
if (iTakumiEntity.takumiRank() == EnumTakumiRank.HIGH) {
entities.add(iTakumiEntity);
}
});
if (!entities.isEmpty()) {
entities.removeIf(iTakumiEntity -> iTakumiEntity instanceof EntityAnnivCreeper || iTakumiEntity instanceof EntitySuperDiamondCreeper);
try {
entity = (Entity) entities.get(worldIn.rand.nextInt(entities.size())).getClass().getConstructor(World.class).newInstance(worldIn);
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
if (entity != null) {
entity.setPosition(pos.getX(), pos.getY(), pos.getZ());
worldIn.setBlockToAir(pos);
if (worldIn.getBlockState(pos.down()).getBlockHardness(worldIn, pos.down()) > 0) {
worldIn.setBlockToAir(pos.down());
}
if (!worldIn.isRemote) {
worldIn.createExplosion(new EntityAlterDummy(worldIn), pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, 3f, true);
worldIn.loadedEntityList.removeIf(entity1 -> entity1 instanceof EntityAlterDummy);
if (worldIn.spawnEntity(entity)) {
return true;
}
} else {
if (entity instanceof EntityKingCreeper && !TakumiUtils.getAdvancementUnlocked(new ResourceLocation("takumicraft:kingslayer"))) {
if (FMLCommonHandler.instance().getSide().isClient()) {
this.proxyShowGuiToast();
}
}
}
}
return flg;
}
use of net.minecraft.util.EnumHand in project takumicraft by TNTModders.
the class ItemAttackBlock method onItemUse.
@Override
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
if (!worldIn.isRemote && worldIn.loadedEntityList.stream().noneMatch(entity -> entity instanceof EntityAttackBlock && !entity.isDead)) {
EntityAttackBlock attackBlock = new EntityAttackBlock(worldIn);
BlockPos blockPos = pos.offset(facing);
if (worldIn.isAirBlock(blockPos) && worldIn.isAirBlock(blockPos.up())) {
attackBlock.setPosition(blockPos.getX() + 0.5, blockPos.getY(), blockPos.getZ() + 0.5);
}
double theta = Math.toRadians(worldIn.rand.nextFloat() * 360);
BlockPos bigPos = blockPos.add(Math.cos(theta) * EntityAttackBlock.DIST, 0, Math.sin(theta) * EntityAttackBlock.DIST);
bigPos = worldIn.getHeight(bigPos);
attackBlock.setPos(bigPos);
double dx = bigPos.getX() - blockPos.getX();
attackBlock.setDX(((float) (dx / EntityAttackBlock.ATTACK_TICK)));
double dz = bigPos.getZ() - blockPos.getZ();
attackBlock.setDZ(((float) (dz / EntityAttackBlock.ATTACK_TICK)));
if (worldIn.spawnEntity(attackBlock)) {
EntityBigCreeperDummy bigCreeperDummy = new EntityBigCreeperDummy(worldIn);
bigCreeperDummy.setPosition(bigPos.getX(), bigPos.getY(), bigPos.getZ());
worldIn.spawnEntity(bigCreeperDummy);
worldIn.playBroadcastSound(1038, pos, 0);
if (!player.isCreative()) {
player.getHeldItem(hand).shrink(1);
}
worldIn.getPlayers(EntityPlayer.class, input -> attackBlock.getDistanceSq(input) < 10000).forEach(player1 -> {
player1.sendMessage(new TextComponentTranslation("entity.attackblock.message.summon.01"));
player1.sendMessage(new TextComponentTranslation("entity.attackblock.message.summon.02"));
player1.sendMessage(new TextComponentTranslation("entity.attackblock.message.summon.03"));
});
}
}
return EnumActionResult.SUCCESS;
}
use of net.minecraft.util.EnumHand in project Spark-Client by Spark-Client-Development.
the class SwitchManager method getCalculateAction.
// gets action that needs to be done for switching to item
public ItemSwitcher.SwitchResult getCalculateAction(SwitchItem switcher, ItemSwitcher.usedHand handType, ItemSwitcher.switchType type) {
if (type == ItemSwitcher.switchType.NoSwitch || handType == ItemSwitcher.usedHand.Offhand) {
float main = ((handType == ItemSwitcher.usedHand.Both || handType == ItemSwitcher.usedHand.Mainhand) ? switcher.isItemGood(mc.player.getHeldItemMainhand()) : 0);
float off = ((handType == ItemSwitcher.usedHand.Both || handType == ItemSwitcher.usedHand.Offhand) ? switcher.isItemGood(mc.player.getHeldItemOffhand()) : 0);
if (main > 0 || off > 0)
return new ItemSwitcher.NoSwitchResult(main > off ? EnumHand.MAIN_HAND : EnumHand.OFF_HAND);
} else if (type == ItemSwitcher.switchType.Const) {
int id = ItemSwitcher.FindStackInInventory(switcher, handType != ItemSwitcher.usedHand.Mainhand);
if (id == 45)
return new ItemSwitcher.NoSwitchResult(EnumHand.OFF_HAND);
if (id == mc.player.inventory.currentItem + 36)
return new ItemSwitcher.InventorySwitchResult(id);
if (id != -1 && !didInventorySwitch)
return new ItemSwitcher.InventorySwitchResult(id);
} else if (type == ItemSwitcher.switchType.Normal || type == ItemSwitcher.switchType.Silent) {
float Best = 0;
EnumHand hand = null;
if (handType == ItemSwitcher.usedHand.Both || handType == ItemSwitcher.usedHand.Offhand || type == ItemSwitcher.switchType.NoSwitch) {
float offhandVal = switcher.isItemGood(mc.player.getHeldItemOffhand());
if (offhandVal > Best) {
Best = offhandVal;
hand = EnumHand.OFF_HAND;
}
}
int newSlot = mc.player.inventory.currentItem;
if (handType == ItemSwitcher.usedHand.Both || handType == ItemSwitcher.usedHand.Mainhand) {
int i = 0;
while (i < 9) {
float val = switcher.isItemGood(mc.player.inventory.getStackInSlot(i));
if (i == mc.player.inventory.currentItem)
val *= 1.1f;
if (val > Best) {
newSlot = i;
Best = val;
hand = EnumHand.MAIN_HAND;
}
i++;
}
} else if (type == ItemSwitcher.switchType.NoSwitch) {
float val = switcher.isItemGood(mc.player.getHeldItemMainhand()) * 1.1f;
if (val > Best) {
Best = val;
hand = EnumHand.MAIN_HAND;
}
}
if (Best > 0)
return new ItemSwitcher.HotbarSwitchResult(hand, newSlot);
}
return null;
}
Aggregations