public override bool OnBlockBrokenWith(IWorldAccessor world, Entity byEntity, ItemSlot itemslot, BlockSelection blockSel) { if (base.OnBlockBrokenWith(world, byEntity, itemslot, blockSel)) { if (byEntity is EntityPlayer) { IPlayer player = world.PlayerByUid((byEntity as EntityPlayer).PlayerUID); switch (blockSel.Face.Axis) { case EnumAxis.X: destroyBlocks(world, blockSel.Position.AddCopy(0, -1, -1), blockSel.Position.AddCopy(0, 1, 1), player); break; case EnumAxis.Y: destroyBlocks(world, blockSel.Position.AddCopy(-1, 0, -1), blockSel.Position.AddCopy(1, 0, 1), player); break; case EnumAxis.Z: destroyBlocks(world, blockSel.Position.AddCopy(-1, -1, 0), blockSel.Position.AddCopy(1, 1, 0), player); break; } } return(true); } return(false); }
public bool DamagePart(IWorldAccessor world, Entity byEntity, IItemStack itemstack, int amount = 1) { ITreeAttribute ToolSlots = itemstack.Attributes.GetOrAddTreeAttribute("toolparts"); IEnumerable <KeyValuePair <string, IAttribute> > validParts = ToolSlots.Where(pair => { ItemStack part = (ItemStack)pair.Value.GetValue(); return(part != null && part.Attributes.GetInt("durability", part.Collectible.GetDurability(part)) > 0); }); // Damage sub parts, if there are any if (validParts.Count() > 0) { var pair = validParts.ElementAt(new Random().Next(0, validParts.Count())); IItemStack stack = pair.Value.GetValue() as IItemStack; ToolPart part = stack.Item as ToolPart; bool broken = false; if (part != null) { broken = part.DamagePart(world, byEntity, stack); } // If the part is broken, then remove it if (broken) { RemovePart(itemstack, pair.Key); } } // Damamge the tool int leftDurability = itemstack.Attributes.GetInt("durability", GetDurability(itemstack)); leftDurability -= amount; itemstack.Attributes.SetInt("durability", leftDurability); if (leftDurability <= 0) { // Bound minimum durability leftDurability = 0; if (byEntity is EntityPlayer) { IPlayer player = world.PlayerByUid(((EntityPlayer)byEntity).PlayerUID); world.PlaySoundAt(new AssetLocation("sounds/effect/toolbreak"), player, player); } else { world.PlaySoundAt(new AssetLocation("sounds/effect/toolbreak"), byEntity.Pos.X, byEntity.Pos.Y, byEntity.Pos.Z); } } itemstack.Attributes.SetInt("durability", leftDurability); // Notify the containing item if this part has broken return(TinkerProps.Breaks && leftDurability == 0); }
public override void OnHeldInteractStop(float secondsUsed, ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel) { if (blockSel == null) { return; } if (secondsUsed < 1.9f) { return; } IWorldAccessor world = byEntity.World; Block block = byEntity.World.BlockAccessor.GetBlock(blockSel.Position); if (!CanSqueezeInto(block)) { return; } BlockBucket blockbucket = block as BlockBucket; if (blockbucket != null) { if (blockbucket.TryPutContent(world, blockSel.Position, new ItemStack(world.GetItem(new AssetLocation("honeyportion"))), 1) == 0) { return; } } else { AssetLocation loc = new AssetLocation(block.Attributes["contentItem2BlockCodes"]["honeyportion"].AsString()); world.BlockAccessor.SetBlock(world.GetBlock(loc).BlockId, blockSel.Position); } slot.TakeOut(1); slot.MarkDirty(); IPlayer byPlayer = null; if (byEntity is EntityPlayer) { byPlayer = world.PlayerByUid(((EntityPlayer)byEntity).PlayerUID); } ItemStack stack = new ItemStack(world.GetItem(new AssetLocation("beeswax"))); if (byPlayer?.InventoryManager.TryGiveItemstack(stack) == false) { byEntity.World.SpawnItemEntity(stack, byEntity.SidedPos.XYZ); } }
public void ResendBlockHighlights(WorldEdit we) { var player = world.PlayerByUid(PlayerUID); HighlightSelectedArea(); if (ToolsEnabled) { EnumHighlightBlocksMode mode = EnumHighlightBlocksMode.CenteredToSelectedBlock; if (ToolOffsetMode == EnumToolOffsetMode.Attach) { mode = EnumHighlightBlocksMode.AttachedToSelectedBlock; } if (ToolInstance != null) { world.HighlightBlocks(player, (int)EnumHighlightSlot.Brush, ToolInstance.GetBlockHighlights(we), ToolInstance.GetBlockHighlightColors(we), mode, EnumHighlightShape.Arbitrary); } } else { world.HighlightBlocks(player, (int)EnumHighlightSlot.Brush, new List <BlockPos>(), new List <int>()); } }
public override void OnHeldInteractStop(float secondsUsed, IItemSlot slot, IEntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel) { if (blockSel == null) { return; } if (secondsUsed < 1.9f) { return; } IWorldAccessor world = byEntity.World; Block block = byEntity.World.BlockAccessor.GetBlock(blockSel.Position); if (!CanSqueezeInto(block)) { return; } BlockBucket blockbucket = block as BlockBucket; if (blockbucket != null) { if (blockbucket.TryAddContent(world, blockSel.Position, new ItemStack(world.GetItem(new AssetLocation("honeyportion"))), 1) == 0) { return; } } else { world.BlockAccessor.SetBlock(world.GetBlock(new AssetLocation("bowl-honey")).BlockId, blockSel.Position); } slot.TakeOut(1); slot.MarkDirty(); IPlayer byPlayer = null; if (byEntity is IEntityPlayer) { byPlayer = world.PlayerByUid(((IEntityPlayer)byEntity).PlayerUID); } byPlayer?.InventoryManager.TryGiveItemstack(new ItemStack(world.GetItem(new AssetLocation("beeswax")))); }
public override bool OnBlockBrokenWith(IWorldAccessor world, Entity byEntity, ItemSlot itemslot, BlockSelection blockSel, float dropQuantityMultiplier = 1) { Block block = world.BlockAccessor.GetBlock(blockSel.Position); base.OnBlockBrokenWith(world, byEntity, itemslot, blockSel, dropQuantityMultiplier); if (byEntity as EntityPlayer == null || itemslot.Itemstack == null) { return(true); } IPlayer plr = world.PlayerByUid((byEntity as EntityPlayer).PlayerUID); if (!CanMultiBreak(block)) { return(true); } Vec3d hitPos = blockSel.Position.ToVec3d().Add(blockSel.HitPosition); var orderedPositions = GetNearblyMultibreakables(world, blockSel.Position, hitPos).OrderBy(x => x.Value); int leftDurability = itemslot.Itemstack.Attributes.GetInt("durability", Durability); int q = 0; foreach (var val in orderedPositions) { if (!plr.Entity.World.Claims.TryAccess(plr, val.Key, EnumBlockAccessFlags.BuildOrBreak)) { continue; } world.BlockAccessor.BreakBlock(val.Key, plr); world.BlockAccessor.MarkBlockDirty(val.Key); DamageItem(world, byEntity, itemslot); q++; if (q >= MultiBreakQuantity || itemslot.Itemstack == null) { break; } } return(true); }
public override bool OnBlockBrokenWith(IWorldAccessor world, Entity byEntity, ItemSlot itemslot, BlockSelection blockSel) { Block block = world.BlockAccessor.GetBlock(blockSel.Position); base.OnBlockBrokenWith(world, byEntity, itemslot, blockSel); if (byEntity as EntityPlayer == null || itemslot.Itemstack == null) { return(true); } IPlayer plr = world.PlayerByUid((byEntity as EntityPlayer).PlayerUID); if (!CanMultiBreak(block)) { return(true); } Vec3d hitPos = blockSel.Position.ToVec3d().Add(blockSel.HitPosition); var orderedPositions = GetNearblyMultibreakables(world, blockSel.Position, hitPos).OrderBy(x => x.Value); int leftDurability = itemslot.Itemstack.Attributes.GetInt("durability", Durability); int q = 0; foreach (var val in orderedPositions) { world.BlockAccessor.BreakBlock(val.Key, plr); world.BlockAccessor.MarkBlockDirty(val.Key); DamageItem(world, byEntity, itemslot); q++; if (q >= MultiBreakQuantity || itemslot.Itemstack == null) { break; } } //`byEntity.GetBehavior<EntityBehaviorHunger>()?.ConsumeSaturation(GameMath.Sqrt(q)); return(true); }
internal void PlaySound(BlockPos pos, IWorldAccessor world, EntityPlayer entityPlayer = null) { const float SOUND_RANGE = 16.0F; const float SOUND_VOLUME = 0.8F; // TODO: In 1.7.0, Block.Sounds should not be null anymore. if (Block.Sounds?.Place == null) { return; } var player = (entityPlayer != null) && (world.Side == EnumAppSide.Server) ? world.PlayerByUid(entityPlayer.PlayerUID) : null; world.PlaySoundAt(Block.Sounds.Place, pos.X + 0.5, pos.Y + 0.25, pos.Z + 0.5, player, range: SOUND_RANGE, volume: SOUND_VOLUME); }
/// <summary> Attempts to place down a <see cref="CarriedBlock"/> at the specified world, /// selection and by the entity (if any), returning whether it was successful. </summary> /// <example cref="ArgumentNullException"> Thrown if world or pos is null. </exception> public bool PlaceDown(IWorldAccessor world, BlockSelection selection, Entity entity = null) { if (world == null) { throw new ArgumentNullException(nameof(world)); } if (selection == null) { throw new ArgumentNullException(nameof(selection)); } if (!world.BlockAccessor.IsValidPos(selection.Position)) { return(false); } if (entity is EntityPlayer playerEntity) { var player = world.PlayerByUid(playerEntity.PlayerUID); var failureCode = "__ignore__"; if (!Block.TryPlaceBlock(world, player, ItemStack, selection, ref failureCode)) { return(false); } } else { world.BlockAccessor.SetBlock(Block.Id, selection.Position); // TODO: Handle type attribute. } RestoreBlockEntityData(world, selection.Position); world.BlockAccessor.TriggerNeighbourBlockUpdate(selection.Position); if (entity != null) { Remove(entity, Slot); } PlaySound(selection.Position, world, (entity as EntityPlayer)); return(true); }
public override bool OnBlockBrokenWith(IWorldAccessor world, Entity byEntity, ItemSlot slot, BlockSelection blockSel, float dropQuantityMultiplier = 1) { int energy = slot.Itemstack.Attributes.GetInt("energy", 0); if (energy >= consume) { if (base.OnBlockBrokenWith(world, byEntity, slot, blockSel, dropQuantityMultiplier)) { if (byEntity is EntityPlayer) { var player = world.PlayerByUid((byEntity as EntityPlayer).PlayerUID); { switch (blockSel.Face.Axis) { case EnumAxis.X: destroyBlocks(world, blockSel.Position.AddCopy(0, -1, -1), blockSel.Position.AddCopy(0, 1, 1), player, blockSel.Position, slot); break; case EnumAxis.Y: destroyBlocks(world, blockSel.Position.AddCopy(-1, 0, -1), blockSel.Position.AddCopy(1, 0, 1), player, blockSel.Position, slot); break; case EnumAxis.Z: destroyBlocks(world, blockSel.Position.AddCopy(-1, -1, 0), blockSel.Position.AddCopy(1, 1, 0), player, blockSel.Position, slot); break; } } } return(true); } return(false); } else { return(false); } }
/// <summary> /// Attempts to place down a <see cref="CarriedBlock"/> at the specified world, /// selection and by the entity (if any), returning whether it was successful. /// </summary> /// <example cref="ArgumentNullException"> Thrown if world or pos is null. </exception> public bool PlaceDown(IWorldAccessor world, BlockSelection selection, IEntity entity = null) { if (world == null) { throw new ArgumentNullException(nameof(world)); } if (selection == null) { throw new ArgumentNullException(nameof(selection)); } if (!world.BlockAccessor.IsValidPos(selection.Position)) { return(false); } if (entity is IEntityPlayer playerEntity) { var player = world.PlayerByUid(playerEntity.PlayerUID); if (!Block.TryPlaceBlock(world, player, ItemStack, selection)) { return(false); } } else { world.BlockAccessor.SetBlock((ushort)Block.Id, selection.Position); // TODO: Handle type attribute. } RestoreBlockEntityData(world, selection.Position); PlaySound(selection.Position, entity); if (entity != null) { Remove(entity); } return(true); }
void ProbeGround(IWorldAccessor world, IEntity byEntity, IItemSlot itemslot, BlockSelection blockSel) { IPlayer byPlayer = null; if (byEntity is IEntityPlayer) { byPlayer = world.PlayerByUid(((IEntityPlayer)byEntity).PlayerUID); } Block block = world.BlockAccessor.GetBlock(blockSel.Position); block.OnBlockBroken(world, blockSel.Position, byPlayer, 0); if (!block.Code.Path.StartsWith("rock") && !block.Code.Path.StartsWith("ore")) { return; } IServerPlayer splr = byPlayer as IServerPlayer; if (splr != null) { IntArrayAttribute attr = itemslot.Itemstack.Attributes["probePositions"] as IntArrayAttribute; if (attr == null || attr.value == null || attr.value.Length == 0) { itemslot.Itemstack.Attributes["probePositions"] = attr = new IntArrayAttribute(); attr.AddInt(blockSel.Position.X, blockSel.Position.Y, blockSel.Position.Z); splr.SendMessage(GlobalConstants.CurrentChatGroup, "Ok, need 2 more samples", EnumChatType.Notification); } else { float requiredSamples = 2; attr.AddInt(blockSel.Position.X, blockSel.Position.Y, blockSel.Position.Z); int[] vals = attr.value; for (int i = 0; i < vals.Length; i += 3) { int x = vals[i]; int z = vals[i + 2]; float mindist = 99; for (int j = i + 3; j < vals.Length; j += 3) { int dx = x - vals[j]; int dz = z - vals[j + 2]; mindist = Math.Min(mindist, GameMath.Sqrt(dx * dx + dz * dz)); } if (i + 3 < vals.Length) { requiredSamples -= GameMath.Clamp(mindist * mindist, 0, 16) / 16; if (mindist > 16) { splr.SendMessage(GlobalConstants.CurrentChatGroup, "Sample too far away from initial reading. Sampling around this point now, need 2 more samples.", EnumChatType.Notification); attr.value = new int[] { blockSel.Position.X, blockSel.Position.Y, blockSel.Position.Z }; return; } } } if (requiredSamples > 0) { int q = (int)Math.Ceiling(requiredSamples); splr.SendMessage(GlobalConstants.CurrentChatGroup, "Ok, need " + q + " more " + (q == 1 ? "sample" : "samples"), EnumChatType.Notification); } else { int startX = vals[0]; int startY = vals[1]; int startZ = vals[2]; PrintProbeResults(world, splr, itemslot, new BlockPos(startX, startY, startZ)); attr.value = new int[0]; } } } }
protected virtual void ProbeBlockNodeMode(IWorldAccessor world, Entity byEntity, ItemSlot itemslot, BlockSelection blockSel, int radius) { IPlayer byPlayer = null; if (byEntity is EntityPlayer) { byPlayer = world.PlayerByUid(((EntityPlayer)byEntity).PlayerUID); } Block block = world.BlockAccessor.GetBlock(blockSel.Position); block.OnBlockBroken(world, blockSel.Position, byPlayer, 0); if (!block.Code.Path.StartsWith("rock") && !block.Code.Path.StartsWith("ore")) { return; } IServerPlayer splr = byPlayer as IServerPlayer; if (splr == null) { return; } BlockPos pos = blockSel.Position.Copy(); Dictionary <string, int> quantityFound = new Dictionary <string, int>(); api.World.BlockAccessor.WalkBlocks(pos.AddCopy(radius, radius, radius), pos.AddCopy(-radius, -radius, -radius), (nblock, bp) => { if (nblock.BlockMaterial == EnumBlockMaterial.Ore && nblock.Variant.ContainsKey("type")) { string key = "ore-" + nblock.Variant["type"]; int q = 0; quantityFound.TryGetValue(key, out q); quantityFound[key] = q + 1; } }); var resultsOrderedDesc = quantityFound.OrderByDescending(val => val.Value).ToList(); if (resultsOrderedDesc.Count == 0) { splr.SendMessage(GlobalConstants.InfoLogChatGroup, Lang.Get("No ore node nearby"), EnumChatType.Notification); } else { splr.SendMessage(GlobalConstants.InfoLogChatGroup, Lang.Get("Found the following ore nodes"), EnumChatType.Notification); foreach (var val in resultsOrderedDesc) { string orename = Lang.Get(val.Key); string resultText = Lang.Get(resultTextByQuantity(val.Value), Lang.Get(val.Key)); splr.SendMessage(GlobalConstants.InfoLogChatGroup, Lang.Get(resultText, orename), EnumChatType.Notification); } } }
public override void OnHeldInteractStop(float secondsUsed, ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel) { if (blockSel == null) { return; } if (secondsUsed < 1.9f) { return; } IWorldAccessor world = byEntity.World; Block block = byEntity.World.BlockAccessor.GetBlock(blockSel.Position); if (!CanSqueezeInto(block, blockSel.Position)) { return; } ItemStack honeyStack = new ItemStack(world.GetItem(new AssetLocation("honeyportion")), 99999); BlockLiquidContainerTopOpened blockCnt = block as BlockLiquidContainerTopOpened; if (blockCnt != null) { if (blockCnt.TryPutLiquid(blockSel.Position, honeyStack, ContainedHoneyLitres) == 0) { return; } } else { var beg = api.World.BlockAccessor.GetBlockEntity(blockSel.Position) as BlockEntityGroundStorage; if (beg != null) { ItemSlot squeezeIntoSlot = beg.Inventory.FirstOrDefault(gslot => gslot.Itemstack?.Block != null && CanSqueezeInto(gslot.Itemstack.Block, null)); if (squeezeIntoSlot != null) { blockCnt = squeezeIntoSlot.Itemstack.Block as BlockLiquidContainerTopOpened; blockCnt.TryPutLiquid(squeezeIntoSlot.Itemstack, honeyStack, ContainedHoneyLitres); beg.MarkDirty(true); } } } slot.TakeOut(1); slot.MarkDirty(); IPlayer byPlayer = null; if (byEntity is EntityPlayer) { byPlayer = world.PlayerByUid(((EntityPlayer)byEntity).PlayerUID); } ItemStack stack = new ItemStack(world.GetItem(new AssetLocation("beeswax"))); if (byPlayer?.InventoryManager.TryGiveItemstack(stack) == false) { byEntity.World.SpawnItemEntity(stack, byEntity.SidedPos.XYZ); } }
public static void Interact(ItemSlot itemslot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel, bool firstEvent, ref EnumHandHandling handHandling, ref EnumHandling handling) { IWorldAccessor world = byEntity?.World; if (blockSel == null || world == null || !byEntity.Controls.Sneak) { return; } IPlayer byPlayer = null; if (byEntity is EntityPlayer) { byPlayer = world.PlayerByUid(((EntityPlayer)byEntity).PlayerUID); } if (byPlayer == null) { return; } if (!world.Claims.TryAccess(byPlayer, blockSel.Position, EnumBlockAccessFlags.BuildOrBreak)) { itemslot.MarkDirty(); return; } BlockGroundStorage blockgs = world.GetBlock(new AssetLocation("groundstorage")) as BlockGroundStorage; if (blockgs == null) { return; } BlockEntity be = world.BlockAccessor.GetBlockEntity(blockSel.Position); BlockEntity beAbove = world.BlockAccessor.GetBlockEntity(blockSel.Position.UpCopy()); if (be is BlockEntityGroundStorage || beAbove is BlockEntityGroundStorage) { if (((be as BlockEntityGroundStorage) ?? (beAbove as BlockEntityGroundStorage)).OnPlayerInteractStart(byPlayer, blockSel)) { handHandling = EnumHandHandling.PreventDefault; } return; } // Must be aiming at the up face if (blockSel.Face != BlockFacing.UP) { return; } Block onBlock = world.BlockAccessor.GetBlock(blockSel.Position); // Must have a support below if (!onBlock.CanAttachBlockAt(world.BlockAccessor, blockgs, blockSel.Position, BlockFacing.UP)) { return; } // Must have empty space above BlockPos pos = blockSel.Position.AddCopy(blockSel.Face); if (world.BlockAccessor.GetBlock(pos).Replaceable < 6000) { return; } if (blockgs.CreateStorage(byEntity.World, blockSel, byPlayer)) { handHandling = EnumHandHandling.PreventDefault; } }
public override bool OnHeldInteractStart(IItemSlot slot, IEntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel) { IWorldAccessor world = byEntity.World; if (blockSel == null) { return(false); } BlockPos position = blockSel.Position; Block block1 = world.BlockAccessor.GetBlock(position); //string temp = block1.Code.Path; string str = ""; if (block1.Code.Path.StartsWith("quartz"))//replace with some sort of tree. { str = "quartzpillar-ud"; if (block1.Code.Path.StartsWith("quartzpillar")) { str = "quartz-ornate"; } } else if (block1.FirstCodePart(0) == "rock") { str = "stonebricks-" + block1.CodeEndWithoutParts(1); } else { return(false); } Block block2 = byEntity.World.GetBlock(new AssetLocation(str)); if (block2 == null) { return(false); } //sanity check IPlayer iplayer = null; if (byEntity is IEntityPlayer) { iplayer = world.PlayerByUid(((IEntityPlayer)byEntity).PlayerUID); } if (iplayer == null) { return(false); } IItemSlot hammerSlot = Gethammer(byEntity); if (hammerSlot == null) { return(false); } if (block1.Sounds != null) { world.PlaySoundAt(block1.Sounds.Place, position.X, position.Y, position.Z, iplayer, true, 32f, 1f); } world.BlockAccessor.SetBlock(block2.BlockId, position); world.BlockAccessor.MarkBlockDirty(position); hammerSlot.Itemstack.Collectible.DamageItem(world, byEntity, hammerSlot); slot.Itemstack.Collectible.DamageItem(world, byEntity, iplayer.InventoryManager.ActiveHotbarSlot); return(true); }
protected virtual void ProbeBlockDensityMode(IWorldAccessor world, Entity byEntity, ItemSlot itemslot, BlockSelection blockSel) { IPlayer byPlayer = null; if (byEntity is EntityPlayer) { byPlayer = world.PlayerByUid(((EntityPlayer)byEntity).PlayerUID); } Block block = world.BlockAccessor.GetBlock(blockSel.Position); float dropMul = 1f; if (block.BlockMaterial == EnumBlockMaterial.Ore || block.BlockMaterial == EnumBlockMaterial.Stone) { dropMul = 0; } block.OnBlockBroken(world, blockSel.Position, byPlayer, dropMul); if (!block.Code.Path.StartsWith("rock") && !block.Code.Path.StartsWith("ore")) { return; } IServerPlayer splr = byPlayer as IServerPlayer; if (splr == null) { return; } if (splr.WorldData.CurrentGameMode == EnumGameMode.Creative) { PrintProbeResults(world, splr, itemslot, blockSel.Position); return; } IntArrayAttribute attr = itemslot.Itemstack.Attributes["probePositions"] as IntArrayAttribute; if ((attr == null || attr.value == null || attr.value.Length == 0)) { itemslot.Itemstack.Attributes["probePositions"] = attr = new IntArrayAttribute(); attr.AddInt(blockSel.Position.X, blockSel.Position.Y, blockSel.Position.Z); splr.SendMessage(GlobalConstants.InfoLogChatGroup, Lang.Get("Ok, need 2 more samples"), EnumChatType.Notification); } else { float requiredSamples = 2; attr.AddInt(blockSel.Position.X, blockSel.Position.Y, blockSel.Position.Z); int[] vals = attr.value; for (int i = 0; i < vals.Length; i += 3) { int x = vals[i]; int y = vals[i + 1]; int z = vals[i + 2]; float mindist = 99; for (int j = i + 3; j < vals.Length; j += 3) { int dx = x - vals[j]; int dy = y - vals[j + 1]; int dz = z - vals[j + 2]; mindist = Math.Min(mindist, GameMath.Sqrt(dx * dx + dy * dy + dz * dz)); } if (i + 3 < vals.Length) { requiredSamples -= GameMath.Clamp(mindist * mindist, 3, 16) / 16; if (mindist > 20) { splr.SendMessage(GlobalConstants.InfoLogChatGroup, Lang.Get("Sample too far away from initial reading. Sampling around this point now, need 2 more samples."), EnumChatType.Notification); attr.value = new int[] { blockSel.Position.X, blockSel.Position.Y, blockSel.Position.Z }; return; } } } if (requiredSamples > 0) { int q = (int)Math.Ceiling(requiredSamples); splr.SendMessage(GlobalConstants.InfoLogChatGroup, q > 1 ? Lang.Get("propick-xsamples", q) : Lang.Get("propick-1sample"), EnumChatType.Notification); } else { int startX = vals[0]; int startY = vals[1]; int startZ = vals[2]; PrintProbeResults(world, splr, itemslot, new BlockPos(startX, startY, startZ)); attr.value = new int[0]; } } }
public static void DamageTool(IWorldAccessor world, Entity byEntity, ItemSlot itemslot, int amount = 1) { ITreeAttribute ToolSlots = itemslot.Itemstack.Attributes.GetOrAddTreeAttribute("toolparts"); IEnumerable <KeyValuePair <string, IAttribute> > validParts = ToolSlots.Where(pair => { ItemStack partstack = (ItemStack)pair.Value.GetValue(); partstack.ResolveBlockOrItem(world); return(partstack != null && partstack.Attributes.GetInt("durability", partstack.Item.GetDurability(partstack)) > 0); }); ItemStack itemstack = itemslot.Itemstack; bool shouldBreak = false; // Damage a subpart if they can be damaged if (validParts.Count() > 0) { ItemStack stack = validParts.ElementAt(new Random().Next(0, validParts.Count())).Value.GetValue() as ItemStack; stack.ResolveBlockOrItem(world); IToolPart part = (IToolPart)stack.Item; bool brokenPart = false; if (part != null) { brokenPart = part.DamagePart(world, byEntity, stack, amount); } shouldBreak = brokenPart && !HasNeededParts(itemstack); } // Damamge the tool int leftDurability = itemstack.Attributes.GetInt("durability", GetDurability(itemstack)); leftDurability -= amount; itemstack.Attributes.SetInt("durability", leftDurability); if (leftDurability <= 0) { // Bound minimum durability leftDurability = 0; if (byEntity is EntityPlayer) { IPlayer player = world.PlayerByUid(((EntityPlayer)byEntity).PlayerUID); world.PlaySoundAt(new AssetLocation("sounds/effect/toolbreak"), player, player); } else { world.PlaySoundAt(new AssetLocation("sounds/effect/toolbreak"), byEntity.Pos.X, byEntity.Pos.Y, byEntity.Pos.Z); } } itemstack.Attributes.SetInt("durability", leftDurability); if (shouldBreak) { if (byEntity is EntityPlayer) { IPlayer player = world.PlayerByUid(((EntityPlayer)byEntity).PlayerUID); world.PlaySoundAt(new AssetLocation("sounds/effect/toolbreak"), player, player); } else { world.PlaySoundAt(new AssetLocation("sounds/effect/toolbreak"), byEntity.Pos.X, byEntity.Pos.Y, byEntity.Pos.Z); } itemslot.Itemstack = null; } itemslot.MarkDirty(); }