private bool SpillContents(IItemSlot bucketSlot, IEntityAgent byEntity, BlockSelection blockSel) { BlockPos pos = blockSel.Position; IPlayer byPlayer = (byEntity as EntityPlayer)?.Player; IBlockAccessor blockAcc = byEntity.World.BlockAccessor; BlockPos secondPos = blockSel.Position.AddCopy(blockSel.Face); WaterTightContainableProps props = GetContentProps(byEntity.World, bucketSlot.Itemstack); if (props == null || !props.AllowSpill || props.WhenSpilled == null) { return(false); } if (props.WhenSpilled.Action == WaterTightContainableProps.EnumSpilledAction.PlaceBlock) { Block waterBlock = byEntity.World.GetBlock(props.WhenSpilled.Stack.Code); if (props.WhenSpilled.StackByFillLevel != null) { float currentlitres = GetCurrentLitres(byEntity.World, bucketSlot.Itemstack); JsonItemStack fillLevelStack = null; props.WhenSpilled.StackByFillLevel.TryGetValue((int)currentlitres, out fillLevelStack); if (fillLevelStack != null) { waterBlock = byEntity.World.GetBlock(fillLevelStack.Code); } } if (blockAcc.GetBlock(pos).Replaceable >= 6000) { blockAcc.SetBlock(waterBlock.BlockId, pos); blockAcc.MarkBlockDirty(pos); } else { if (blockAcc.GetBlock(secondPos).Replaceable >= 6000) { blockAcc.SetBlock(waterBlock.BlockId, secondPos); blockAcc.MarkBlockDirty(secondPos); } else { return(false); } } } if (props.WhenSpilled.Action == WaterTightContainableProps.EnumSpilledAction.DropContents) { props.WhenSpilled.Stack.Resolve(byEntity.World, "bucketspill"); ItemStack stack = props.WhenSpilled.Stack.ResolvedItemstack.Clone(); stack.StackSize = (int)(props.ItemsPerLitre * GetContent(byEntity.World, bucketSlot.Itemstack).StackSize); byEntity.World.SpawnItemEntity(stack, blockSel.Position.ToVec3d().Add(blockSel.HitPosition)); } ItemStack emptyBucketStack = new ItemStack(this); if (bucketSlot.Itemstack.StackSize <= 1) { bucketSlot.Itemstack = emptyBucketStack; bucketSlot.MarkDirty(); } else { bucketSlot.TakeOut(1); if (!byPlayer.InventoryManager.TryGiveItemstack(emptyBucketStack, true)) { byEntity.World.SpawnItemEntity(emptyBucketStack, byEntity.LocalPos.XYZ); } } byEntity.World.PlaySoundAt(props.FillSpillSound, pos.X, pos.Y, pos.Z, byPlayer); return(true); }
public bool TryFillBucketFromBlock(IItemSlot itemslot, IEntityAgent byEntity, BlockPos pos) { IPlayer byPlayer = (byEntity as EntityPlayer)?.Player; IBlockAccessor blockAcc = byEntity.World.BlockAccessor; Block block = blockAcc.GetBlock(pos); if (block.Attributes?["waterTightContainerProps"].Exists == false) { return(false); } WaterTightContainableProps props = block.Attributes?["waterTightContainerProps"]?.AsObject <WaterTightContainableProps>(); if (props?.WhenFilled == null || !props.Containable) { return(false); } props.WhenFilled.Stack.Resolve(byEntity.World, "blockbucket"); ItemStack contentStack = GetContent(byEntity.World, itemslot.Itemstack); if (contentStack != null && contentStack.Equals(props.WhenFilled.Stack.ResolvedItemstack)) { SetContent(itemslot.Itemstack, contentStack); itemslot.MarkDirty(); return(true); } // Is full if (contentStack != null && contentStack.StackSize == (int)(props.ItemsPerLitre * BucketCapacityLitres)) { return(false); } contentStack = props.WhenFilled.Stack.ResolvedItemstack.Clone(); contentStack.StackSize = (int)(props.ItemsPerLitre * BucketCapacityLitres); ItemStack fullBucketStack = new ItemStack(this); SetContent(fullBucketStack, contentStack); if (itemslot.Itemstack.StackSize <= 1) { itemslot.Itemstack = fullBucketStack; } else { itemslot.TakeOut(1); if (!byPlayer.InventoryManager.TryGiveItemstack(fullBucketStack, true)) { byEntity.World.SpawnItemEntity(fullBucketStack, byEntity.LocalPos.XYZ); } } itemslot.MarkDirty(); byEntity.World.PlaySoundAt(props.FillSpillSound, pos.X, pos.Y, pos.Z, byPlayer); return(true); }
public override void OnHeldInteractStop(float secondsUsed, IItemSlot slot, IEntityAgent 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; string rockType = slot.Itemstack.Collectible.FirstCodePart(1); ItemStack stack = slot.TakeOut(1); slot.MarkDirty(); IPlayer byPlayer = null; if (byEntity is IEntityPlayer) { byPlayer = byEntity.World.PlayerByUid(((IEntityPlayer)byEntity).PlayerUID); } byEntity.World.PlaySoundAt(new AssetLocation("sounds/player/throw"), byEntity, byPlayer, false, 8); EntityProperties type = byEntity.World.GetEntityType(new AssetLocation("thrownstone")); Entity entity = byEntity.World.ClassRegistry.CreateEntity(type); ((EntityThrownStone)entity).FiredBy = byEntity; ((EntityThrownStone)entity).Damage = damage; ((EntityThrownStone)entity).ProjectileStack = stack; int?texIndex = type.Attributes?["texturealternateMapping"]?[rockType].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.5; entity.ServerPos.SetPos( byEntity.ServerPos.BehindCopy(0.21).XYZ.Add(0, byEntity.EyeHeight - 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; ((EntityThrownStone)entity).SetRotation(); byEntity.World.SpawnEntity(entity); byEntity.StartAnimation("throw"); }
internal bool OnPlayerInteract(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel) { IItemSlot slot = byPlayer.InventoryManager.ActiveHotbarSlot; if (!byPlayer.Entity.Controls.Sneak) { if (contents == null) { return(false); } ItemStack split = contents.Clone(); split.StackSize = 1; contents.StackSize--; if (contents.StackSize == 0) { contents = null; } //api.World.Logger.Notification("Forge item retrieve temp: {0}, side {1}", split.Collectible.GetTemperature(api.World, split), api.Side); if (!byPlayer.InventoryManager.TryGiveItemstack(split)) { world.SpawnItemEntity(contents, pos.ToVec3d().Add(0.5, 0.5, 0.5)); } renderer?.SetContents(contents, fuelLevel, burning, true); MarkDirty(); api.World.PlaySoundAt(new AssetLocation("sounds/block/ingot"), pos.X, pos.Y, pos.Z, byPlayer, false); return(true); } else { if (slot.Itemstack == null) { return(false); } // Add fuel CombustibleProperties combprops = slot.Itemstack.Collectible.CombustibleProps; if (combprops != null && combprops.BurnTemperature > 1000) { if (fuelLevel >= 10 / 16f) { return(false); } fuelLevel += 1 / 16f; renderer?.SetContents(contents, fuelLevel, burning, false); MarkDirty(); slot.TakeOut(1); slot.MarkDirty(); return(true); } string firstCodePart = slot.Itemstack.Collectible.FirstCodePart(); // Add heatable item if (contents == null && (firstCodePart == "ingot" || firstCodePart == "metalplate" || firstCodePart == "workitem")) { contents = slot.Itemstack.Clone(); contents.StackSize = 1; slot.TakeOut(1); slot.MarkDirty(); renderer?.SetContents(contents, fuelLevel, burning, true); MarkDirty(); api.World.PlaySoundAt(new AssetLocation("sounds/block/ingot"), pos.X, pos.Y, pos.Z, byPlayer, false); return(true); } // Merge heatable item if (contents != null && contents.Equals(api.World, slot.Itemstack, GlobalConstants.IgnoredStackAttributes) && contents.StackSize < 4 && contents.StackSize < contents.Collectible.MaxStackSize) { float myTemp = contents.Collectible.GetTemperature(api.World, contents); float histemp = slot.Itemstack.Collectible.GetTemperature(api.World, slot.Itemstack); contents.Collectible.SetTemperature(world, contents, (myTemp * contents.StackSize + histemp * 1) / (contents.StackSize + 1)); contents.StackSize++; slot.TakeOut(1); slot.MarkDirty(); renderer?.SetContents(contents, fuelLevel, burning, true); api.World.PlaySoundAt(new AssetLocation("sounds/block/ingot"), pos.X, pos.Y, pos.Z, byPlayer, false); MarkDirty(); return(true); } return(false); } }
public override void OnHeldInteractStart(IItemSlot slot, IEntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel, ref EnumHandHandling handling) { IPlayer byPlayer = null; if (byEntity is IEntityPlayer) { byPlayer = byEntity.World.PlayerByUid(((IEntityPlayer)byEntity).PlayerUID); } if (byEntity.Controls.Sneak && blockSel != null) { IWorldAccessor world = byEntity.World; Block knappingBlock = world.GetBlock(new AssetLocation("knappingsurface")); if (knappingBlock == null) { return; } Block block = world.BlockAccessor.GetBlock(blockSel.Position); if (!block.CanAttachBlockAt(byEntity.World.BlockAccessor, knappingBlock, blockSel.Position, BlockFacing.UP)) { return; } BlockPos pos = blockSel.Position.AddCopy(blockSel.Face); if (!world.BlockAccessor.GetBlock(pos).IsReplacableBy(knappingBlock)) { return; } BlockSelection placeSel = blockSel.Clone(); placeSel.Position = pos; placeSel.DidOffset = true; if (!knappingBlock.TryPlaceBlock(world, byPlayer, slot.Itemstack, placeSel)) { return; } 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 = slot.Itemstack.Clone(); bec.BaseMaterial.StackSize = 1; if (byEntity.World is IClientWorldAccessor) { bec.OpenDialog(world as IClientWorldAccessor, pos, slot.Itemstack); } } slot.TakeOut(1); handling = EnumHandHandling.PreventDefaultAction; return; } base.OnHeldInteractStart(slot, byEntity, blockSel, entitySel, ref handling); }
public override void OnHeldInteractStart(IItemSlot slot, IEntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel, ref EnumHandHandling handHandling) { if (blockSel == null || byEntity?.World == null || !byEntity.Controls.Sneak) { return; } IWorldAccessor world = byEntity.World; BlockPos offsetedPos = blockSel.Position.AddCopy(blockSel.Face); BlockPos belowPos = offsetedPos.DownCopy(); Block targetedBlock = world.BlockAccessor.GetBlock(blockSel.Position); Block nextblock; AssetLocation loc = new AssetLocation(this.Attributes["blockfirstcodepart"].AsString()); string firstcodepart = loc.Path; IPlayer player = byEntity.World.PlayerByUid((byEntity as EntityPlayer)?.PlayerUID); if (!byEntity.World.TestPlayerAccessBlock(player, blockSel.Position, EnumBlockAccessFlags.BuildOrBreak)) { slot.MarkDirty(); return; } if (targetedBlock.FirstCodePart() == firstcodepart) { int stage = 1; int.TryParse(targetedBlock.LastCodePart(), out stage); if (stage == 9) { return; } nextblock = world.GetBlock(targetedBlock.CodeWithPart("" + (stage + 1), 1)); world.BlockAccessor.SetBlock(nextblock.BlockId, blockSel.Position); } else { nextblock = byEntity.World.GetBlock(loc.WithPathAppendix("-1")); if (nextblock == null) { return; } Block blockAtTargetPos = world.BlockAccessor.GetBlock(offsetedPos); if (!blockAtTargetPos.IsReplacableBy(nextblock)) { return; } if (!world.BlockAccessor.GetBlock(belowPos).SideSolid[BlockFacing.UP.Index]) { return; } world.BlockAccessor.SetBlock(nextblock.BlockId, offsetedPos); } slot.TakeOut(1); slot.MarkDirty(); if (nextblock.Sounds != null) { IPlayer byPlayer = null; if (byEntity is IEntityPlayer) { byPlayer = byEntity.World.PlayerByUid(((IEntityPlayer)byEntity).PlayerUID); } world.PlaySoundAt(nextblock.Sounds.Place, blockSel.Position.X, blockSel.Position.Y, blockSel.Position.Z, byPlayer); } handHandling = EnumHandHandling.PreventDefault; }