-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes for experience and level systems, see #87
Add player experience and levels Add Experience Orb entity Add /givexp command
- Loading branch information
Showing
10 changed files
with
314 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#region C#raft License | ||
// This file is part of C#raft. Copyright C#raft Team | ||
// | ||
// C#raft is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
#endregion | ||
|
||
using System.Linq; | ||
using Chraft.PluginSystem; | ||
using Chraft.PluginSystem.Commands; | ||
using Chraft.PluginSystem.Net; | ||
using Chraft.Utilities.Misc; | ||
|
||
namespace Chraft.Plugins.Commands | ||
{ | ||
public class CmdGiveXP : IClientCommand | ||
{ | ||
public CmdGiveXP(IPlugin plugin) | ||
{ | ||
Iplugin = plugin; | ||
} | ||
|
||
public IClientCommandHandler ClientCommandHandler { get; set; } | ||
|
||
public void Use(IClient client, string commandName, string[] tokens) | ||
{ | ||
short amount; | ||
IClient target; | ||
|
||
if (tokens.Length == 1) | ||
{ | ||
if (short.TryParse(tokens[0], out amount)) | ||
{ | ||
client.GetOwner().AddExperience(amount); | ||
client.SendMessage(string.Format("{0}You has been granted with {1} exp", ChatColor.Red, amount)); | ||
return; | ||
} | ||
Help(client); | ||
return; | ||
} | ||
|
||
if (tokens.Length == 2) | ||
{ | ||
IClient[] matchedClients = client.GetServer().GetClients(tokens[0]).ToArray(); | ||
if (matchedClients.Length < 1) | ||
{ | ||
client.SendMessage("Unknown Player"); | ||
return; | ||
} | ||
if (matchedClients.Length == 1) | ||
{ | ||
target = matchedClients[0]; | ||
} | ||
else | ||
{ | ||
int exactMatchClient = -1; | ||
for (int i = 0; i < matchedClients.Length; i++) | ||
{ | ||
if (matchedClients[i].GetOwner().DisplayName.ToLower() == tokens[0].ToLower()) | ||
exactMatchClient = i; | ||
} | ||
|
||
// If we found the player with the exactly same name - he is our target | ||
if (exactMatchClient != -1) | ||
{ | ||
target = matchedClients[exactMatchClient]; | ||
} | ||
else | ||
{ | ||
// We do not found a proper target and aren't going to randomly punish anyone | ||
client.SendMessage("More than one player found. Provide the exact name."); | ||
return; | ||
} | ||
} | ||
|
||
if (short.TryParse(tokens[1], out amount)) | ||
{ | ||
target.GetOwner().AddExperience(amount); | ||
target.SendMessage(string.Format("{0}{1} has been granted with {2} exp", ChatColor.Red, target.GetOwner().DisplayName, amount)); | ||
} | ||
else | ||
{ | ||
Help(client); | ||
} | ||
return; | ||
} | ||
|
||
Help(client); | ||
} | ||
|
||
public void Help(IClient client) | ||
{ | ||
client.SendMessage("/givexp [Player] <amount> - gives the player an experience"); | ||
} | ||
|
||
public string Name | ||
{ | ||
get { return "givexp"; } | ||
set { } | ||
} | ||
|
||
public string Shortcut | ||
{ | ||
get { return ""; } | ||
} | ||
|
||
public CommandType Type | ||
{ | ||
get { return CommandType.Mod; } | ||
} | ||
|
||
public string Permission | ||
{ | ||
get { return "chraft.givexp"; } | ||
} | ||
|
||
public IPlugin Iplugin { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#region C#raft License | ||
// This file is part of C#raft. Copyright C#raft Team | ||
// | ||
// C#raft is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
#endregion | ||
|
||
namespace Chraft.Utilities.Misc | ||
{ | ||
public static class Experience | ||
{ | ||
public static short GetLevel(short experience) | ||
{ | ||
short level = 0; | ||
if (experience <= 16) | ||
return level; | ||
|
||
short toTheNext = ExpToNextLevel(level); | ||
while (experience >= toTheNext) | ||
{ | ||
level++; | ||
experience -= toTheNext; | ||
toTheNext = ExpToNextLevel(level); | ||
} | ||
|
||
return level; | ||
} | ||
|
||
public static short GetExperience(short level) | ||
{ | ||
if (level <= 0) | ||
return 0; | ||
|
||
if (level >= 1 && level <= 15) | ||
return (short)System.Math.Min(short.MaxValue, 17 * level); | ||
|
||
if (level >= 16 && level <= 30) | ||
return (short)System.Math.Min(short.MaxValue, 1.5 * (level * level) - (29.5 * level) + 360); | ||
|
||
// Levels 31+ | ||
return (short)System.Math.Min(short.MaxValue, 3.5 * (level * level) - (151.5 * level) + 2220); | ||
} | ||
|
||
public static short ExpToNextLevel(short currentLevel) | ||
{ | ||
// TODO: Proper handling of negative level | ||
if (currentLevel < 0) | ||
return 17; | ||
|
||
if (currentLevel >= 0 && currentLevel <= 14) | ||
return 17; | ||
|
||
if (currentLevel >= 15 && currentLevel <= 29) | ||
return (short)System.Math.Min(short.MaxValue, 3 * currentLevel - 28); | ||
|
||
// Levels 30+ | ||
return (short)System.Math.Min(short.MaxValue, 7 * currentLevel - 148); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#region C#raft License | ||
// This file is part of C#raft. Copyright C#raft Team | ||
// | ||
// C#raft is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
#endregion | ||
|
||
using Chraft.Utilities.Coords; | ||
using Chraft.World; | ||
|
||
namespace Chraft.Entity | ||
{ | ||
public class ExpOrbEntity : EntityBase | ||
{ | ||
public short Experience { get; protected set; } | ||
|
||
public ExpOrbEntity(Server server, int entityId, short exp) | ||
: base(server, entityId) | ||
{ | ||
Height = 0.5f; | ||
Width = 0.5f; | ||
Experience = exp; | ||
World = server.GetDefaultWorld() as WorldManager; | ||
} | ||
|
||
protected override void DoUpdate() | ||
{ | ||
base.DoUpdate(); | ||
|
||
PushOutOfBlocks(new AbsWorldCoords(Position.X, (BoundingBox.Minimum.Y + BoundingBox.Maximum.Y) / 2.0, Position.Z)); | ||
var chunk = World.GetChunk(UniversalCoords.FromAbsWorld(Position)); | ||
if (chunk == null) | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.