-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClearRubbleCommand.cs
151 lines (136 loc) · 5.52 KB
/
ClearRubbleCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* Clear Rubble Command
Copyright (C) 2018 "Spoffy" - https://www.github.com/Spoffy
This program 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 <https://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Eco.Gameplay.Objects;
using Eco.Gameplay.Players;
using Eco.Gameplay.Systems.Chat;
using Eco.Mods.TechTree;
using Eco.Shared.Math;
using Eco.Shared.Networking;
using Eco.Shared.Utils;
using Eco.World.Blocks;
namespace Eco.Mods
{
public class ClearRubbleCommand : IChatCommandHandler
{
class MethodInvocationException : Exception
{
public MethodInvocationException(string message) : base(message)
{
}
}
private static int DestroyRubbleType<T>() where T : RubbleObject
{
MethodInfo destroyRubbleMethod = typeof(RubbleObject).GetMethod("Destroy", BindingFlags.NonPublic | BindingFlags.Instance);
if (destroyRubbleMethod == null)
{
throw new MethodInvocationException("Destroy method not found on Rubble type");
}
IEnumerable<T> objects = NetObjectManager.GetObjectsOfType<T>();
var count = 0;
objects.ForEach(rubbleObject =>
{
count += 1;
destroyRubbleMethod.Invoke(rubbleObject, new Object[0]);
});
return count;
}
private static int DestroyFallenTrees()
{
IEnumerable<TreeEntity> trees = NetObjectManager.GetObjectsOfType<TreeEntity>();
int count = 0;
trees.Where((tree) => tree.Fallen).ForEach((tree) =>
{
count++;
//Occasionally a tree may not have a PlantPack - this is a bug in Eco.
if (tree.PlantPack == null)
{
ChatManager.ServerMessageToAllAlreadyLocalized(
"Warning: Unable to clear a fallen tree, it has no PlantPack. Report this to a developer.", true);
}
tree.Destroy();
});
return count;
}
private static int DestroyWoodDebris()
{
var count = 0;
World.World.TopBlockCache.ForEach((Pos2d, block) =>
{
//We need to look one up, as the TopBlockCache contains the top SOLID block, and Wood debris isn't solid.
var positionAbove = World.World.GetTopPos(Pos2d) + Vector3i.Up;
var blockAbove = World.World.GetBlock(positionAbove);
if (blockAbove.Is<TreeDebris>())
{
count++;
World.World.DeleteBlock(positionAbove);
}
});
return count;
}
private static int DestroyRubbleByName(string rubbleName)
{
switch (rubbleName.ToLower())
{
case "stone":
return DestroyRubbleType<RubbleObject<StoneItem>>();
case "copper":
return DestroyRubbleType<RubbleObject<CopperOreItem>>();
case "gold":
return DestroyRubbleType<RubbleObject<GoldOreItem>>();
case "coal":
return DestroyRubbleType<RubbleObject<CoalItem>>();
case "iron":
return DestroyRubbleType<RubbleObject<IronOreItem>>();
case "fallentrees":
return DestroyFallenTrees();
case "wooddebris":
return DestroyWoodDebris();
}
return -1;
}
private static async void DestroyRubbleByNameAndSendResult(string rubbleName, User user)
{
var result = await Task.Run(() => DestroyRubbleByName(rubbleName));
if (result < 0)
{
ChatManager.ServerMessageToPlayerAlreadyLocalized("That is not a valid rubble type.", user);
}
else
{
ChatManager.ServerMessageToPlayerAlreadyLocalized("Destroyed " + result + " rubble", user);
}
}
[ChatCommand("Clears all of a type of debris from the server. Valid debris types are 'stone', 'copper', 'gold', 'coal', 'iron', 'fallentrees', 'wooddebris'", ChatAuthorizationLevel.Admin)]
public static void ClearRubble(User user, string rubbleName = "")
{
if (rubbleName.Length == 0)
{
ChatManager.ServerMessageToPlayerAlreadyLocalized("You need to specify an item type", user);
return;
}
try
{
DestroyRubbleByNameAndSendResult(rubbleName, user);
}
catch (MethodInvocationException ex)
{
ChatManager.ServerMessageToPlayerAlreadyLocalized("Error occured: " + ex.Message, user);
}
}
}
}