public override BlockDropItemStack[] GetDropsForHandbook(ItemStack handbookStack, IPlayer forPlayer) { LootList list = lootLists[LastCodePart()]; List <BlockDropItemStack> drops = new List <BlockDropItemStack>(); foreach (var val in list.lootItems) { for (int i = 0; i < val.codes.Length; i++) { BlockDropItemStack stack = new BlockDropItemStack(val.GetItemStack(api.World, i, 1)); if (stack == null) { continue; } stack.Quantity.avg = val.chance / list.TotalChance / val.codes.Length; // Prevent duplicates if (drops.FirstOrDefault(dstack => dstack.ResolvedItemstack.Equals(api.World, stack.ResolvedItemstack, GlobalConstants.IgnoredStackAttributes)) == null) { drops.Add(stack); } } } return(drops.ToArray()); }
public override bool OnBlockInteractStart(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling) { if (!world.Claims.TryAccess(byPlayer, blockSel.Position, EnumBlockAccessFlags.Use)) { return(false); } if (!block.Code.Path.Contains("empty")) { handling = EnumHandling.PreventDefault; //handles the collection of items, and the transformation of the block. world.Logger.VerboseDebug("Collecting item(s) from target block at {0}.", blockSel.Position); if (block.Drops != null && block.Drops.Length > 1) { BlockDropItemStack drop = block.Drops[0]; ItemStack stack = drop.GetNextItemStack(); if (!byPlayer.InventoryManager.TryGiveItemstack(stack)) { world.SpawnItemEntity(drop.GetNextItemStack(), blockSel.Position.ToVec3d().Add(0.5, 0.5, 0.5)); } AssetLocation loc = block.Code.CopyWithPath(block.Code.Path.Replace(block.Code.Path.Split('-').Last(), "empty")); world.BlockAccessor.SetBlock(world.GetBlock(loc).BlockId, blockSel.Position); world.PlaySoundAt(new AssetLocation("sounds/player/collect"), blockSel.Position.X, blockSel.Position.Y, blockSel.Position.Z, byPlayer); } return(true); } return(false); }
/// <summary> /// Creates a copy of this object. /// </summary> /// <returns></returns> public EntityProperties Clone() { BlockDropItemStack[] DropsCopy; if (Drops == null) { DropsCopy = null; } else { DropsCopy = new BlockDropItemStack[Drops.Length]; for (int i = 0; i < DropsCopy.Length; i++) { DropsCopy[i] = Drops[i].Clone(); } } Dictionary <string, AssetLocation> csounds = new Dictionary <string, AssetLocation>(); foreach (var val in Sounds) { csounds[val.Key] = val.Value.Clone(); } Dictionary <string, AssetLocation[]> cresolvedsounds = new Dictionary <string, AssetLocation[]>(); foreach (var val in ResolvedSounds) { AssetLocation[] locs = val.Value; cresolvedsounds[val.Key] = new AssetLocation[locs.Length]; for (int i = 0; i < locs.Length; i++) { cresolvedsounds[val.Key][i] = locs[i].Clone(); } } return(new EntityProperties() { Code = Code.Clone(), Class = Class, Habitat = Habitat, HitBoxSize = HitBoxSize.Clone(), DeadHitBoxSize = DeadHitBoxSize.Clone(), CanClimb = CanClimb, CanClimbAnywhere = CanClimbAnywhere, FallDamage = FallDamage, ClimbTouchDistance = ClimbTouchDistance, RotateModelOnClimb = RotateModelOnClimb, KnockbackResistance = KnockbackResistance, Attributes = Attributes?.Clone(), Sounds = new Dictionary <string, AssetLocation>(Sounds), IdleSoundChance = IdleSoundChance, IdleSoundRange = IdleSoundRange, Drops = DropsCopy, EyeHeight = EyeHeight, Client = Client?.Clone() as EntityClientProperties, Server = Server?.Clone() as EntityServerProperties }); }
public override BlockDropItemStack[] GetDropsForHandbook(IWorldAccessor world, BlockPos pos, IPlayer byPlayer) { LootList list = lootLists[LastCodePart()]; List <BlockDropItemStack> drops = new List <BlockDropItemStack>(); foreach (var val in list.lootItems) { for (int i = 0; i < val.codes.Length; i++) { BlockDropItemStack stack = new BlockDropItemStack(val.GetItemStack(world, i)); stack.Quantity.avg = val.chance / list.TotalChance / val.codes.Length; drops.Add(stack); } } return(drops.ToArray()); }
public override void OnBlockInteractStop(float secondsUsed, IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handled) { handled = EnumHandling.PreventDefault; if (secondsUsed > harvestTime - 0.05f && block.Code.Path.Contains("ripe") && block.Drops != null && block.Drops.Length >= 1) { BlockDropItemStack drop = block.Drops.Length == 1 ? block.Drops[0] : block.Drops[1]; ItemStack stack = drop.GetNextItemStack(); if (!byPlayer.InventoryManager.TryGiveItemstack(stack)) { world.SpawnItemEntity(drop.GetNextItemStack(), blockSel.Position.ToVec3d().Add(0.5, 0.5, 0.5)); } world.BlockAccessor.SetBlock(world.GetBlock(block.Code.CopyWithPath(block.Code.Path.Replace("ripe", "empty"))).BlockId, blockSel.Position); world.PlaySoundAt(new AssetLocation("sounds/block/plant"), blockSel.Position.X, blockSel.Position.Y, blockSel.Position.Z, byPlayer); } }
public override void Initialize(JsonObject properties) { base.Initialize(properties); interactionHelpCode = properties["harvestTime"].AsString("blockhelp-harvetable-harvest"); harvestTime = properties["harvestTime"].AsFloat(0); harvestedStack = properties["harvestedStack"].AsObject <BlockDropItemStack>(null); string code = properties["harvestingSound"].AsString("game:sounds/block/leafy-picking"); if (code != null) { harvestingSound = AssetLocation.Create(code, block.Code.Domain); } code = properties["harvestedBlockCode"].AsString(); if (code != null) { harvestedBlockCode = AssetLocation.Create(code, block.Code.Domain); } }
public override BlockDropItemStack[] GetDropsForHandbook(ItemStack handbookStack, IPlayer forPlayer) { LootList list = lootLists[LastCodePart()]; List <BlockDropItemStack> drops = new List <BlockDropItemStack>(); foreach (var val in list.lootItems) { for (int i = 0; i < val.codes.Length; i++) { BlockDropItemStack stack = new BlockDropItemStack(val.GetItemStack(api.World, i, 1)); if (stack == null) { continue; } stack.Quantity.avg = val.chance / list.TotalChance / val.codes.Length; drops.Add(stack); } } return(drops.ToArray()); }
public void SetHarvested(IPlayer byPlayer, float dropQuantityMultiplier = 1f) { //entity.World.Logger.Debug("setharvested begin " + entity.World.Side); if (entity.WatchedAttributes.GetBool("harvested", false)) { return; } entity.WatchedAttributes.SetBool("harvested", true); if (entity.World.Side == EnumAppSide.Client) { return; } if (!entity.Attributes.GetBool("isMechanical", false)) { dropQuantityMultiplier *= byPlayer.Entity.Stats.GetBlended("animalLootDropRate"); } List <ItemStack> todrop = new List <ItemStack>(); for (int i = 0; i < jsonDrops.Length; i++) { BlockDropItemStack dstack = jsonDrops[i]; if (dstack.Tool != null && (byPlayer == null || dstack.Tool != byPlayer.InventoryManager.ActiveTool)) { continue; } dstack.Resolve(entity.World, "BehaviorHarvestable"); float extraMul = 1f; if (dstack.DropModbyStat != null) { // If the stat does not exist, then GetBlended returns 1 \o/ extraMul = byPlayer.Entity.Stats.GetBlended(dstack.DropModbyStat); } ItemStack stack = dstack.GetNextItemStack(this.dropQuantityMultiplier * dropQuantityMultiplier * extraMul); if (stack == null) { continue; } if (stack.Collectible.NutritionProps != null || stack.Collectible.CombustibleProps?.SmeltedStack?.ResolvedItemstack?.Collectible?.NutritionProps != null) { float weightedStackSize = stack.StackSize * AnimalWeight; float fraction = weightedStackSize - (int)weightedStackSize; stack.StackSize = (int)weightedStackSize + (entity.World.Rand.NextDouble() > fraction ? 1 : 0); } if (stack.StackSize == 0) { continue; } todrop.Add(stack); if (dstack.LastDrop) { break; } } //entity.World.Logger.Debug("setharvested drops resolved"); ItemStack[] resolvedDrops = todrop.ToArray(); TreeAttribute tree = new TreeAttribute(); for (int i = 0; i < resolvedDrops.Length; i++) { inv[i].Itemstack = resolvedDrops[i]; //entity.World.Logger.Debug("drop {0} is {1}", i, resolvedDrops[i]?.GetName()); } inv.ToTreeAttributes(tree); entity.WatchedAttributes["harvestableInv"] = tree; entity.WatchedAttributes.MarkPathDirty("harvestableInv"); entity.WatchedAttributes.MarkPathDirty("harvested"); if (entity.World.Side == EnumAppSide.Server) { entity.World.BlockAccessor.GetChunkAtBlockPos(entity.ServerPos.AsBlockPos).MarkModified(); } //entity.World.Logger.Debug("setharvested done"); }
public EntityProperties CreateProperties() { BlockDropItemStack[] DropsCopy; if (Drops == null) { DropsCopy = null; } else { DropsCopy = new BlockDropItemStack[Drops.Length]; for (int i = 0; i < DropsCopy.Length; i++) { DropsCopy[i] = Drops[i].Clone(); } } EntityProperties properties = new EntityProperties() { Code = Code, Class = Class, Habitat = Habitat, HitBoxSize = HitBoxSize, CanClimb = CanClimb, CanClimbAnywhere = CanClimbAnywhere, FallDamage = FallDamage, ClimbTouchDistance = ClimbTouchDistance, RotateModelOnClimb = RotateModelOnClimb, KnockbackResistance = KnockbackResistance, Attributes = Attributes, Sounds = Sounds == null ? new Dictionary <string, AssetLocation>() : new Dictionary <string, AssetLocation>(Sounds), IdleSoundChance = IdleSoundChance, IdleSoundRange = IdleSoundRange, Drops = DropsCopy }; if (Client != null) { properties.Client = new EntityClientProperties(Client.Behaviors) { RendererName = Client.Renderer, Textures = new Dictionary <string, CompositeTexture>(Client.Textures), GlowLevel = Client.GlowLevel, Shape = Client.Shape, Size = Client.Size, Animations = Client.Animations, AnimationsByMetaCode = Client.AnimationsByMetaCode, }; } if (Server != null) { properties.Server = new EntityServerProperties(Server.Behaviors) { Attributes = Server.Attributes?.ToAttribute() as TreeAttribute, SpawnConditions = Server.SpawnConditions }; } properties.SetEyeHeight(EyeHeight); return(properties); }