public override void OnHeldInteractStop(float secondsUsed, ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel) { if (byEntity.Attributes.GetInt("aimingCancel") == 1) { return; } byEntity.Attributes.SetInt("aiming", 0); byEntity.StopAnimation("aim"); if (secondsUsed < 0.35f) { return; } float damage = 1; ItemStack stack = slot.TakeOut(1); slot.MarkDirty(); IPlayer byPlayer = null; if (byEntity is EntityPlayer) { byPlayer = byEntity.World.PlayerByUid(((EntityPlayer)byEntity).PlayerUID); } byEntity.World.PlaySoundAt(new AssetLocation("sounds/player/throw"), byEntity, byPlayer, false, 8); EntityProperties type = byEntity.World.GetEntityType(new AssetLocation("thrownstone-" + Variant["rock"])); Entity entity = byEntity.World.ClassRegistry.CreateEntity(type); ((EntityThrownStone)entity).FiredBy = byEntity; ((EntityThrownStone)entity).Damage = damage; ((EntityThrownStone)entity).ProjectileStack = stack; float acc = (1 - byEntity.Attributes.GetFloat("aimingAccuracy", 0)); double rndpitch = byEntity.WatchedAttributes.GetDouble("aimingRandPitch", 1) * acc * 0.75; double rndyaw = byEntity.WatchedAttributes.GetDouble("aimingRandYaw", 1) * acc * 0.75; Vec3d pos = byEntity.ServerPos.XYZ.Add(0, byEntity.LocalEyePos.Y, 0); Vec3d aheadPos = pos.AheadCopy(1, byEntity.ServerPos.Pitch + rndpitch, byEntity.ServerPos.Yaw + rndyaw); Vec3d velocity = (aheadPos - pos) * 0.5; entity.ServerPos.SetPos( byEntity.ServerPos.BehindCopy(0.21).XYZ.Add(0, byEntity.LocalEyePos.Y, 0) ); entity.ServerPos.Motion.Set(velocity); entity.Pos.SetFrom(entity.ServerPos); entity.World = byEntity.World; ((EntityThrownStone)entity).SetRotation(); byEntity.World.SpawnEntity(entity); byEntity.StartAnimation("throw"); //byEntity.GetBehavior<EntityBehaviorHunger>()?.ConsumeSaturation(2f); }
public override void OnHeldInteractStart(ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel, bool firstEvent, ref EnumHandHandling handling) { if (((ItemCage)slot.Itemstack.Item).IsCase) { base.OnHeldInteractStart(slot, byEntity, blockSel, entitySel, firstEvent, ref handling); return; } if (byEntity.Controls.Sneak && ((ItemCage)slot.Itemstack.Item).IsEmpty) { IPlayer player = (byEntity as EntityPlayer).Player; InventoryCage inventory = new InventoryCage(player, slot); player.InventoryManager.OpenInventory(inventory); byEntity.Attributes.SetInt("opengui", 1); handling = EnumHandHandling.Handled; return; } byEntity.Attributes.SetInt("opengui", 0); byEntity.Attributes.SetInt("aiming", 1); byEntity.Attributes.SetInt("aimingCancel", 0); byEntity.StartAnimation("aim"); handling = EnumHandHandling.PreventDefault; }
public override void OnHeldInteractStart(ItemSlot itemslot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel, bool firstEvent, ref EnumHandHandling handling) { // Not ideal to code the aiming controls this way. Needs an elegant solution - maybe an event bus? byEntity.Attributes.SetInt("aiming", 1); byEntity.Attributes.SetInt("aimingCancel", 0); byEntity.StartAnimation("aim"); handling = EnumHandHandling.PreventDefault; }
public override void OnHeldInteractStop(float secondsUsed, ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel) { if (byEntity.Attributes.GetInt("aiming") == 0) { return; } byEntity.Attributes.SetInt("aiming", 0); byEntity.StopAnimation("aim"); if (secondsUsed < 0.35f) { return; } float damage = 0.5f; string rockType = slot.Itemstack.Collectible.FirstCodePart(1); ItemStack stack = slot.TakeOut(1); slot.MarkDirty(); IPlayer byPlayer = null; if (byEntity is EntityPlayer) { byPlayer = byEntity.World.PlayerByUid(((EntityPlayer)byEntity).PlayerUID); } byEntity.World.PlaySoundAt(new AssetLocation("sounds/player/throw"), byEntity, byPlayer, false, 8); EntityProperties type = byEntity.World.GetEntityType(new AssetLocation("thrownbeenade")); Entity entity = byEntity.World.ClassRegistry.CreateEntity(type); ((EntityThrownBeenade)entity).FiredBy = byEntity; ((EntityThrownBeenade)entity).Damage = damage; ((EntityThrownBeenade)entity).ProjectileStack = stack; float acc = (1 - byEntity.Attributes.GetFloat("aimingAccuracy", 0)); double rndpitch = byEntity.WatchedAttributes.GetDouble("aimingRandPitch", 1) * acc * 0.75; double rndyaw = byEntity.WatchedAttributes.GetDouble("aimingRandYaw", 1) * acc * 0.75; Vec3d pos = byEntity.ServerPos.XYZ.Add(0, byEntity.LocalEyePos.Y - 0.2, 0); Vec3d aheadPos = pos.AheadCopy(1, byEntity.ServerPos.Pitch + rndpitch, byEntity.ServerPos.Yaw + rndyaw); Vec3d velocity = (aheadPos - pos) * 0.5; entity.ServerPos.SetPos(byEntity.ServerPos.BehindCopy(0.21).XYZ.Add(0, byEntity.LocalEyePos.Y - 0.2, 0).Ahead(0.25, 0, byEntity.ServerPos.Yaw + GameMath.PIHALF)); entity.ServerPos.Motion.Set(velocity); entity.Pos.SetFrom(entity.ServerPos); entity.World = byEntity.World; byEntity.World.SpawnEntity(entity); byEntity.StartAnimation("throw"); }
public override void OnHeldInteractStart(ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel, bool firstEvent, ref EnumHandHandling handling) { if (byEntity.Controls.Sneak) { return; } byEntity.Attributes.SetInt("aiming", 1); byEntity.Attributes.SetInt("aimingCancel", 0); byEntity.StartAnimation("aim"); handling = EnumHandHandling.PreventDefault; }
public override void OnHeldInteractStop(float secondsUsed, ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel) { if (byEntity.Attributes.GetInt("aimingCancel") == 1) { return; } byEntity.Attributes.SetInt("aiming", 0); byEntity.StopAnimation("aim"); if (secondsUsed < 0.35f) { return; } ItemStack stack; if ((byEntity as EntityPlayer)?.Player.WorldData.CurrentGameMode != EnumGameMode.Creative) { stack = slot.TakeOut(1); } else { stack = slot.TakeOut(0); stack.StackSize = 1; } slot.MarkDirty(); IPlayer byPlayer = null; if (byEntity is EntityPlayer) { byPlayer = byEntity.World.PlayerByUid(((EntityPlayer)byEntity).PlayerUID); } byEntity.World.PlaySoundAt(new AssetLocation("game", "sounds/player/throw"), byEntity, byPlayer, false, 8); EntityProperties type = byEntity.World.GetEntityType(new AssetLocation(HappyWinter.MOD_ID, "thrownsnowball")); Entity entity = byEntity.World.ClassRegistry.CreateEntity(type); ((EntityThrownSnowball)entity).FiredBy = byEntity; ((EntityThrownSnowball)entity).ProjectileStack = stack; ((EntityThrownSnowball)entity).Damage = damage; float acc = (1 - byEntity.Attributes.GetFloat("aimingAccuracy", 0)); double rndpitch = byEntity.WatchedAttributes.GetDouble("aimingRandPitch", 1) * acc * 0.75; double rndyaw = byEntity.WatchedAttributes.GetDouble("aimingRandYaw", 1) * acc * 0.75; Vec3d pos = byEntity.ServerPos.XYZ.Add(0, byEntity.LocalEyePos.Y - 0.2, 0); Vec3d aheadPos = pos.AheadCopy(1, byEntity.ServerPos.Pitch + rndpitch, byEntity.ServerPos.Yaw + rndyaw); Vec3d velocity = (aheadPos - pos) * 0.5; entity.ServerPos.SetPos( byEntity.ServerPos.BehindCopy(0.21).XYZ.Add(0, byEntity.LocalEyePos.Y - 0.2, 0) ); entity.ServerPos.Motion.Set(velocity); entity.Pos.SetFrom(entity.ServerPos); entity.World = byEntity.World; byEntity.World.SpawnEntity(entity); byEntity.StartAnimation("throw"); }
public override void OnHeldInteractStop(float secondsUsed, ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel) { if (byEntity.Attributes.GetInt("aimingCancel") == 1) { return; } byEntity.Attributes.SetInt("aiming", 0); byEntity.StopAnimation("aim"); if (secondsUsed < 0.35f) { return; } float damage = 1.5f; if (slot.Itemstack.Collectible.Attributes != null) { damage = slot.Itemstack.Collectible.Attributes["damage"].AsFloat(0); } string spearMaterial = slot.Itemstack.Collectible.FirstCodePart(1); ItemStack stack = slot.TakeOut(1); stack.Collectible.DamageItem(byEntity.World, byEntity, new DummySlot(stack)); IPlayer byPlayer = null; if (byEntity is EntityPlayer) { byPlayer = byEntity.World.PlayerByUid(((EntityPlayer)byEntity).PlayerUID); } byEntity.World.PlaySoundAt(new AssetLocation("sounds/player/throw"), byEntity, byPlayer, false, 8); EntityProperties type = byEntity.World.GetEntityType(new AssetLocation(Attributes["spearEntityCode"].AsString())); Entity entity = byEntity.World.ClassRegistry.CreateEntity(type); ((EntityProjectile)entity).FiredBy = byEntity; ((EntityProjectile)entity).Damage = damage; ((EntityProjectile)entity).ProjectileStack = stack; ((EntityProjectile)entity).DropOnImpactChance = 1.1f; ((EntityProjectile)entity).Weight = 0.3f; int?texIndex = type.Attributes?["texturealternateMapping"]?[spearMaterial].AsInt(0); entity.WatchedAttributes.SetInt("textureIndex", texIndex == null ? 0 : (int)texIndex); float acc = (1 - byEntity.Attributes.GetFloat("aimingAccuracy", 0)); double rndpitch = byEntity.WatchedAttributes.GetDouble("aimingRandPitch", 1) * acc * 0.75; double rndyaw = byEntity.WatchedAttributes.GetDouble("aimingRandYaw", 1) * acc * 0.75; Vec3d pos = byEntity.ServerPos.XYZ.Add(0, byEntity.EyeHeight - 0.2, 0); Vec3d aheadPos = pos.AheadCopy(1, byEntity.ServerPos.Pitch + rndpitch, byEntity.ServerPos.Yaw + rndyaw); Vec3d velocity = (aheadPos - pos) * 0.65; entity.ServerPos.SetPos(byEntity.ServerPos.BehindCopy(0.21).XYZ.Add(0, byEntity.EyeHeight - 0.2, 0)); entity.ServerPos.Motion.Set(velocity); entity.Pos.SetFrom(entity.ServerPos); entity.World = byEntity.World; ((EntityProjectile)entity).SetRotation(); byEntity.World.SpawnEntity(entity); byEntity.StartAnimation("throw"); RefillSlotIfEmpty(slot, byEntity); }
public override void OnHeldInteractStart(ItemSlot itemslot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel, bool firstEvent, ref EnumHandHandling handling) { if (blockSel != null && byEntity.World.BlockAccessor.GetBlock(blockSel.Position) is BlockDisplayCase) { handling = EnumHandHandling.NotHandled; return; } bool knappable = itemslot.Itemstack.Collectible.Attributes != null && itemslot.Itemstack.Collectible.Attributes["knappable"].AsBool(false); bool haveKnappableStone = false; IPlayer byPlayer = (byEntity as EntityPlayer)?.Player; if (byEntity.Controls.Sneak && blockSel != null) { Block block = byEntity.World.BlockAccessor.GetBlock(blockSel.Position); haveKnappableStone = block.Code.Path.StartsWith("loosestones") && block.FirstCodePart(1).Equals(itemslot.Itemstack.Collectible.FirstCodePart(1)) ; } if (haveKnappableStone) { if (!knappable) { if (byEntity.World.Side == EnumAppSide.Client) { (this.api as ICoreClientAPI).TriggerIngameError(this, "toosoft", Lang.Get("This type of stone is too soft to be used for knapping.")); } return; } if (!byEntity.World.Claims.TryAccess(byPlayer, blockSel.Position, EnumBlockAccessFlags.Use)) { itemslot.MarkDirty(); return; } IWorldAccessor world = byEntity.World; Block knappingBlock = world.GetBlock(new AssetLocation("knappingsurface")); if (knappingBlock == null) { return; } string failCode = ""; BlockPos pos = blockSel.Position; knappingBlock.CanPlaceBlock(world, byPlayer, blockSel, ref failCode); if (failCode == "entityintersecting") { bool selfBlocked = false; bool entityBlocked = world.GetIntersectingEntities(pos, knappingBlock.GetCollisionBoxes(world.BlockAccessor, pos), e => { selfBlocked = e == byEntity; return(!(e is EntityItem)); }).Length != 0; string err = entityBlocked ? (selfBlocked ? Lang.Get("Cannot place a knapping surface here, too close to you") : Lang.Get("Cannot place a knapping surface here, to close to another player or creature.")) : Lang.Get("Cannot place a knapping surface here") ; (api as ICoreClientAPI).TriggerIngameError(this, "cantplace", err); return; } world.BlockAccessor.SetBlock(knappingBlock.BlockId, pos); world.BlockAccessor.TriggerNeighbourBlockUpdate(blockSel.Position); if (knappingBlock.Sounds != null) { world.PlaySoundAt(knappingBlock.Sounds.Place, blockSel.Position.X, blockSel.Position.Y, blockSel.Position.Z); } BlockEntityKnappingSurface bec = world.BlockAccessor.GetBlockEntity(pos) as BlockEntityKnappingSurface; if (bec != null) { bec.BaseMaterial = itemslot.Itemstack.Clone(); bec.BaseMaterial.StackSize = 1; if (byEntity.World is IClientWorldAccessor) { bec.OpenDialog(world as IClientWorldAccessor, pos, itemslot.Itemstack); } } //itemslot.Take(1); handling = EnumHandHandling.PreventDefault; byEntity.Attributes.SetInt("aimingCancel", 1); return; } if (blockSel != null && byEntity?.World != null && byEntity.Controls.Sneak) { IWorldAccessor world = byEntity.World; Block block = world.GetBlock(CodeWithPath("loosestones-" + LastCodePart() + "-free")); if (block == null) { block = world.GetBlock(CodeWithPath("loosestones-" + LastCodePart(1) + "-" + LastCodePart(0) + "-free")); } if (block == null) { return; } if (!world.BlockAccessor.GetBlock(blockSel.Position).SideSolid[BlockFacing.UP.Index]) { return; } BlockPos targetpos = blockSel.Position.AddCopy(blockSel.Face); BlockSelection placeSel = blockSel.Clone(); placeSel.Position = targetpos; placeSel.DidOffset = true; string error = ""; if (!block.TryPlaceBlock(world, byPlayer, itemslot.Itemstack, placeSel, ref error)) { if (api.Side == EnumAppSide.Client) { (api as ICoreClientAPI).TriggerIngameError(this, "cantplace", Lang.Get("placefailure-" + error)); } return; } world.BlockAccessor.TriggerNeighbourBlockUpdate(blockSel.Position); if (block.Sounds != null) { world.PlaySoundAt(block.Sounds.Place, blockSel.Position.X, blockSel.Position.Y, blockSel.Position.Z); } itemslot.Itemstack.StackSize--; handling = EnumHandHandling.PreventDefault; byEntity.Attributes.SetInt("aimingCancel", 1); return; } if (byEntity.Controls.Sneak) { return; } // Not ideal to code the aiming controls this way. Needs an elegant solution - maybe an event bus? byEntity.Attributes.SetInt("aiming", 1); byEntity.Attributes.SetInt("aimingCancel", 0); byEntity.StartAnimation("aim"); handling = EnumHandHandling.PreventDefault; }
public override void OnHeldInteractStop(float secondsUsed, ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel) { if (byEntity.Attributes.GetInt("aimingCancel") == 1) { return; } byEntity.Attributes.SetInt("aiming", 0); byEntity.StopAnimation("aim"); if (secondsUsed < 0.35f) { return; } float damage = 1.5f; if (slot.Itemstack.Collectible.Attributes != null) { damage = slot.Itemstack.Collectible.Attributes["damage"].AsFloat(0); } (api as ICoreClientAPI)?.World.AddCameraShake(0.17f); ItemStack stack = slot.TakeOut(1); slot.MarkDirty(); stack.Collectible.DamageItem(byEntity.World, byEntity, new DummySlot(stack)); IPlayer byPlayer = null; if (byEntity is EntityPlayer) { byPlayer = byEntity.World.PlayerByUid(((EntityPlayer)byEntity).PlayerUID); } byEntity.World.PlaySoundAt(new AssetLocation("sounds/player/throw"), byEntity, byPlayer, false, 8); EntityProperties type = byEntity.World.GetEntityType(new AssetLocation(Attributes["spearEntityCode"].AsString())); Entity entity = byEntity.World.ClassRegistry.CreateEntity(type); ((EntityProjectile)entity).FiredBy = byEntity; ((EntityProjectile)entity).Damage = damage; ((EntityProjectile)entity).ProjectileStack = stack; ((EntityProjectile)entity).DropOnImpactChance = 1.1f; ((EntityProjectile)entity).Weight = 0.3f; float acc = (1 - byEntity.Attributes.GetFloat("aimingAccuracy", 0)); double rndpitch = byEntity.WatchedAttributes.GetDouble("aimingRandPitch", 1) * acc * 0.75; double rndyaw = byEntity.WatchedAttributes.GetDouble("aimingRandYaw", 1) * acc * 0.75; Vec3d pos = byEntity.ServerPos.XYZ.Add(0, byEntity.LocalEyePos.Y - 0.2, 0); Vec3d aheadPos = pos.AheadCopy(1, byEntity.ServerPos.Pitch + rndpitch, byEntity.ServerPos.Yaw + rndyaw); Vec3d velocity = (aheadPos - pos) * 0.65; Vec3d spawnPos = byEntity.ServerPos.BehindCopy(0.21).XYZ.Add(byEntity.LocalEyePos.X, byEntity.LocalEyePos.Y - 0.2, byEntity.LocalEyePos.Z); entity.ServerPos.SetPos(spawnPos); entity.ServerPos.Motion.Set(velocity); //byEntity.World.SpawnParticles(1, ColorUtil.WhiteArgb, spawnPos, spawnPos, new Vec3f(), new Vec3f(), 1.5f, 0, 1); entity.Pos.SetFrom(entity.ServerPos); entity.World = byEntity.World; ((EntityProjectile)entity).SetRotation(); byEntity.World.SpawnEntity(entity); byEntity.StartAnimation("throw"); RefillSlotIfEmpty(slot, byEntity); byPlayer.Entity.World.PlaySoundAt(new AssetLocation("sounds/player/strike"), byPlayer.Entity, byPlayer, 0.9f + (float)api.World.Rand.NextDouble() * 0.2f, 16, 0.5f); }