public override void OnBlockBroken(IWorldAccessor world, BlockPos Pos, IPlayer byPlayer, float dropQuantityMultiplier = 1) { base.OnBlockBroken(world, Pos, byPlayer, dropQuantityMultiplier); ItemSlot itemslot = byPlayer?.InventoryManager?.ActiveHotbarSlot; if (FirstCodePart() == "palmlog") { if (itemslot?.Itemstack == null || !Code.ToString().Contains("grown") || !world.Side.IsServer()) { return; } if (itemslot?.Itemstack.Collectible.Tool == EnumTool.Axe) { int numfelled = 0; for (int x = -2; x <= 2; x++) { for (int z = -2; z <= 2; z++) { for (int y = 0; y <= 16; y++) { BlockPos bPos = new BlockPos(Pos.X + x, Pos.Y + y, Pos.Z + z); Block bBlock = world.BlockAccessor.GetBlock(bPos); if (bBlock is BlockPalmTree || bBlock.FirstCodePart() == "palmfruits") { if (itemslot.Itemstack == null) { return; } foreach (ItemStack item in GetDrops(world, bPos, byPlayer)) { world.SpawnItemEntity(item, bPos.ToVec3d()); } world.BlockAccessor.SetBlock(0, bPos); numfelled++; } } } } if (numfelled > 15) { Vec3d vec3d = Pos.MidPoint(); this.api.World.PlaySoundAt(new AssetLocation("sounds/effect/treefell"), vec3d.X, vec3d.Y, vec3d.Z, null, false, 32f, GameMath.Clamp((float)numfelled / 30, 0.25f, 1f)); } } } }
public bool OnPlayerInteract(IPlayer byPlayer, BlockSelection blockSel) { BlockPos Pos = blockSel?.Position; Block block = Pos?.GetBlock(byPlayer.Entity.World); ItemSlot slot = byPlayer?.InventoryManager?.ActiveHotbarSlot; if (block == null || slot?.Itemstack == null) { return(false); } bool shouldbreak = false; foreach (var val in InWorldCraftingRecipes) { foreach (var recipe in val.Value) { if (recipe.Disabled || (recipe.Takes.AllowedVariants != null && !block.WildCardMatch(recipe.Takes.AllowedVariants)) || (recipe.Tool.AllowedVariants != null && !slot.Itemstack.Collectible.WildCardMatch(recipe.Tool.AllowedVariants)) || (recipe.Takes.Attributes != null) && recipe.Takes.Attributes.Token.HasValues && block.Attributes.Token != recipe.Takes.Attributes.Token) { continue; } if (block.WildCardMatch(recipe.Takes.Code)) { if (IsValid(byPlayer, recipe, slot)) { if (recipe.IsSwap) { var make = recipe.Makes[0].Clone(); make.Resolve(byPlayer.Entity.World, null); if (make.IsBlock()) { if (recipe.Remove) { byPlayer.Entity.World.BlockAccessor.SetBlock(0, Pos); } Block resolvedBlock = make.ResolvedItemstack.Block; byPlayer.Entity.World.BlockAccessor.SetBlock(resolvedBlock.BlockId, Pos); resolvedBlock.OnBlockPlaced(byPlayer.Entity.World, Pos); TakeOrDamage(recipe, slot, byPlayer); shouldbreak = true; } } else if (recipe.IsCreate) { foreach (var make in recipe.Makes) { var makeClone = make.Clone(); makeClone.Resolve(byPlayer.Entity.World, null); byPlayer.Entity.World.SpawnItemEntity(makeClone.ResolvedItemstack, Pos.MidPoint(), new Vec3d(0.0, 0.1, 0.0)); } TakeOrDamage(recipe, slot, byPlayer); if (recipe.Remove) { byPlayer.Entity.World.BlockAccessor.SetBlock(0, Pos); } shouldbreak = true; } if (byPlayer.Entity.World.Side.IsServer()) { byPlayer.Entity.World.PlaySoundAt(recipe.CraftSound, Pos); } } else { continue; } slot.MarkDirty(); break; } } if (shouldbreak) { return(true); } } return(false); }
public override void OnBlockInteractStop(float secondsUsed, IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handled) { handled = EnumHandling.PreventDefault; base.OnBlockInteractStop(secondsUsed, world, byPlayer, blockSel, ref handled); if (createBlocks == null) { world.Logger.Notification("CreateBlocks error in " + block.Code.ToString()); return; } var active = byPlayer?.InventoryManager; BlockPos Pos = blockSel?.Position; if (active?.ActiveHotbarSlot?.Itemstack?.Collectible?.Code != null && Pos != null) { foreach (var val in createBlocks) { //if (Api.Side.IsClient()) Api.ModLoader.GetModSystem<ShaderTest>().progressBar = 0; if (secondsUsed > val.MakeTime && active.ActiveHotbarSlot.Itemstack.Collectible.WildCardMatch(val.Takes.Code) && active.ActiveHotbarSlot.StackSize >= val.Takes.StackSize) { if (world.Side.IsServer()) { world.PlaySoundAt(block.Sounds.Place, Pos.X, Pos.Y, Pos.Z); if (active?.ActiveHotbarSlot?.Itemstack?.Item?.Tool != null) { active.ActiveHotbarSlot.Itemstack.Collectible.DamageItem(world, byPlayer.Entity, active.ActiveHotbarSlot); } } if (val.IntoInv) { if (!active.TryGiveItemstack(val.Makes)) { world.SpawnItemEntity(val.Makes, Pos.MidPoint(), new Vec3d(0.0, 0.1, 0.0)); } } else { world.SpawnItemEntity(val.Makes, Pos.MidPoint(), new Vec3d(0.0, 0.1, 0.0)); } active.ActiveHotbarSlot.TakeOut(val.Takes.StackSize); try { if (world.Side.IsClient()) { world.SpawnCubeParticles(Pos.MidPoint(), active.ActiveHotbarSlot.Itemstack, 2, 16); } } catch (Exception) { world.Logger.Error("Could not create particles, missing itemstack?"); } active.ActiveHotbarSlot.MarkDirty(); if (val.RemoveOnFinish) { world.BlockAccessor.SetBlock(0, Pos); } handled = EnumHandling.PreventDefault; return; } } } ItemStack stack = byPlayer?.InventoryManager?.ActiveHotbarSlot?.Itemstack; if (stack != null && allowPlaceOn) { string r = ""; BlockSelection newsel = blockSel.Clone(); newsel.Position = blockSel.Position.Offset(blockSel.Face); Block block = stack.Block; if (block != null && block.TryPlaceBlock(world, byPlayer, stack, newsel, ref r)) { world.PlaySoundAt(stack.Block?.Sounds.Place, newsel.Position); } } }