Skip to content

Commit b5093a1

Browse files
Refactor IndianHempPlant to implement IHarvestableObject interface and update HarvestableEntities to include IndianHempType
1 parent b1bd95a commit b5093a1

6 files changed

Lines changed: 95 additions & 74 deletions

File tree

src/OpenRP.Framework/Features/Harvestables/Components/IndianHempPlant.cs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,19 @@
1313

1414
namespace OpenRP.Framework.Features.Harvestables.Components
1515
{
16-
public class IndianHempPlant : Component, IHarvestableEntity
16+
public class IndianHempPlant : Component, IHarvestableObject
1717
{
1818
private BiomeObject _generatedObject;
1919
private int _remainingLeaves;
2020

21-
public IndianHempPlant(BiomeObject generatedObject)
21+
public IndianHempPlant()
2222
{
23-
_generatedObject = generatedObject;
2423
_remainingLeaves = 10;
25-
26-
CreateDynamicObject();
27-
//CreateDynamicTextLabel();
2824
}
2925

3026
public string GetTextLabelString()
3127
{
32-
return $"{ChatColor.Highlight}Indian Hemp (Apocynum Cannabinum)\n{ChatColor.White}{_remainingLeaves} leaves remaining\n\nUse {ChatColor.Highlight}/harvest hemp";
33-
}
34-
35-
public Vector3 GetTextLabelPosition()
36-
{
37-
Vector3 textLabelPosition = new Vector3(_generatedObject.GamePosition.XY, _generatedObject.GamePosition.Z + 0.25f);
38-
return textLabelPosition;
39-
}
40-
41-
public void CreateDynamicTextLabel()
42-
{
43-
Vector3 textLabelPosition = GetTextLabelPosition();
28+
return $"{ChatColor.Highlight}Indian Hemp (Apocynum Cannabinum)\n{ChatColor.White}{_remainingLeaves} leaves remaining\n\nUse {ChatColor.Highlight}/harvest indian hemp";
4429
}
4530

4631
public DynamicTextLabel GetDynamicTextLabel()
@@ -54,27 +39,27 @@ public void UpdateDynamicTextLabel()
5439
dynamicTextLabel.Text = GetTextLabelString();
5540
}
5641

57-
public void CreateDynamicObject()
58-
{
59-
BiomeManager.CreateGeneratorObject(_generatedObject, Entity, _colAndreasService, _streamerService);
60-
}
61-
6242
public DynamicObject GetDynamicObject()
6343
{
6444
return GetComponentInChildren<DynamicObject>();
6545
}
6646

6747
public bool IsPlayerNearby(Player player)
6848
{
69-
Vector3 textLabelPosition = GetTextLabelPosition();
49+
Vector3 textLabelPosition = GetDynamicTextLabel().Position;
7050
if (player.IsInRangeOfPoint(3.0f, textLabelPosition))
7151
{
7252
return true;
7353
}
7454
return false;
7555
}
7656

77-
public void Harvest()
57+
public void BeginHarvest()
58+
{
59+
60+
}
61+
62+
public void EndHarvest()
7863
{
7964
_remainingLeaves -= 1;
8065
UpdateDynamicTextLabel();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using SampSharp.Entities;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Reflection.Metadata.Ecma335;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace OpenRP.Framework.Features.Harvestables.Entities
10+
{
11+
public static class HarvestableEntities
12+
{
13+
// Id Registry
14+
private static int IndianHempId = int.MinValue;
15+
16+
// Entity Types
17+
[EntityType]
18+
public static readonly Guid IndianHempType = new Guid("F7AC9D99-47F4-4C10-939D-C0A2183F2417");
19+
20+
// Generate Methods
21+
public static EntityId GenerateIndianHempId()
22+
{
23+
EntityId newEntityId = new EntityId(IndianHempType, IndianHempId);
24+
25+
// Atomically decrement the counter to ensure thread-safety.
26+
int newId = Interlocked.Increment(ref IndianHempId);
27+
28+
return newEntityId;
29+
}
30+
}
31+
}

src/OpenRP.Framework/Features/Harvestables/Entities/IHarvestableEntity.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using SampSharp.Entities.SAMP;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace OpenRP.Framework.Features.Harvestables.Entities
9+
{
10+
public interface IHarvestableModel
11+
{
12+
/// <summary>
13+
/// The name of the resource (e.g. "hemp"). Used for the /harvest command.
14+
/// </summary>
15+
string ResourceName { get; }
16+
17+
/// <summary>
18+
/// The object model id of the resource.
19+
/// </summary>
20+
int ResourceObjectModelId { get; }
21+
22+
/// <summary>
23+
/// Create the harvestable in the world.
24+
/// </summary>
25+
/// <param name="position">The positions of the harvestable.</param>
26+
/// <param name="rotation">The rotations of the harvestable.</param>
27+
void CreateHarvestable(Vector3 position, Vector3 rotation);
28+
29+
/// <summary>
30+
/// Performs code at the begin of a player harvesting the resource
31+
/// </summary>
32+
/// <param name="player">The player harvesting.</param>
33+
void BeginHarvest(Player player);
34+
35+
/// <summary>
36+
/// Performs code at the end of a player harvesting the resource
37+
/// </summary>
38+
/// <param name="player">The player harvesting.</param>
39+
void EndHarvest(Player player);
40+
}
41+
}

src/OpenRP.Framework/Features/Harvestables/Entities/IHarvestableObject.cs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,8 @@ namespace OpenRP.Framework.Features.Harvestables.Entities
99
{
1010
public interface IHarvestableObject
1111
{
12-
/// <summary>
13-
/// The name of the resource (e.g. "hemp"). Used for the /harvest command.
14-
/// </summary>
15-
string ResourceName { get; }
16-
17-
/// <summary>
18-
/// The object model id of the resource.
19-
/// </summary>
20-
int ResourceObjectModelId { get; }
21-
22-
/// <summary>
23-
/// Create the harvestable in the world.
24-
/// </summary>
25-
/// <param name="position">The positions of the harvestable.</param>
26-
/// <param name="rotation">The rotations of the harvestable.</param>
27-
void CreateHarvestable(Vector3 position, Vector3 rotation);
28-
29-
/// <summary>
30-
/// Performs code at the begin of a player harvesting the resource
31-
/// </summary>
32-
/// <param name="player">The player harvesting.</param>
33-
void BeginHarvest(Player player);
34-
35-
/// <summary>
36-
/// Performs code at the end of a player harvesting the resource
37-
/// </summary>
38-
/// <param name="player">The player harvesting.</param>
39-
void EndHarvest(Player player);
12+
bool IsPlayerNearby(Player player);
13+
void BeginHarvest();
14+
void EndHarvest();
4015
}
4116
}

src/OpenRP.Framework/Features/Harvestables/Services/IndianHempHarvestable.cs renamed to src/OpenRP.Framework/Features/Harvestables/Services/IndianHempHarvestableModel.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,29 @@
1414

1515
namespace OpenRP.Framework.Features.Harvestables.Services
1616
{
17-
public class IndianHempHarvestable : IHarvestable
17+
public class IndianHempHarvestableModel : IHarvestableModel
1818
{
1919
private readonly IEntityManager _entityManager;
2020
private readonly IColAndreasService _colAndreasService;
2121
private readonly IStreamerService _streamerService;
22-
public IndianHempHarvestable(IEntityManager entityManager, IColAndreasService colAndreasService, IStreamerService streamerService)
22+
public IndianHempHarvestableModel(IEntityManager entityManager, IColAndreasService colAndreasService, IStreamerService streamerService)
2323
{
2424
_entityManager = entityManager;
2525
_streamerService = streamerService;
2626
_colAndreasService = colAndreasService;
2727
}
2828

29-
public string ResourceName => "industrial hemp";
29+
public string ResourceName => "indian hemp";
3030
public int ResourceObjectModelId => 19473;
3131

32-
public void CreateHarvestable(Vector3 positions, Vector3 rotations)
32+
public void CreateHarvestable(Vector3 position, Vector3 rotation)
3333
{
34+
EntityId harvestableId = HarvestableEntities.GenerateIndianHempId();
3435

36+
_entityManager.Create(harvestableId);
37+
38+
DynamicObject harvestableObject = _streamerService.CreateDynamicObject(ResourceObjectModelId, position, rotation, parent: harvestableId);
39+
DynamicTextLabel harvestableTextLabel = _streamerService.CreateDynamicTextLabel(String.Empty, Color.White, new Vector3(position.XY, position.Z + 0.25f), 1.5f, parent: harvestableId);
3540
}
3641

3742
public void BeginHarvest(Player player)
@@ -40,7 +45,7 @@ public void BeginHarvest(Player player)
4045
{
4146
if (indianHempPlant.IsPlayerNearby(player))
4247
{
43-
PlayerHarvesting.StartHarvesting(player, this, indianHempPlant, TimeSpan.FromSeconds(10));
48+
//PlayerHarvesting.StartHarvesting(player, this, indianHempPlant, TimeSpan.FromSeconds(10));
4449
return;
4550
}
4651
}

0 commit comments

Comments
 (0)