Skip to content
This repository was archived by the owner on Apr 27, 2019. It is now read-only.

Commit 428a7c1

Browse files
committed
Added projectile blacklist, closing #56.
1 parent aaa7bad commit 428a7c1

5 files changed

Lines changed: 153 additions & 19 deletions

File tree

config.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
import json
33
import logging
44
import inspect
5+
import sys
6+
from utility_functions import recursive_dictionary_update
7+
58

69
class Singleton(type):
710
_instances = {}
@@ -21,16 +24,25 @@ def __init__(self):
2124
try:
2225
with open("config/config.json.default", "r") as default_config:
2326
self.config = json.load(default_config)
24-
except:
25-
self.logger.exception("Could not load the default configuration file.", exc_info=True)
26-
raise
27+
except IOError:
28+
self.logger.critical("The configuration defaults file (config.json.default) doesn't exist! Shutting down.")
29+
sys.exit()
30+
except ValueError:
31+
self.logger.critical("The configuration defaults file (config.json.default) contains invalid JSON. Please run it against a JSON linter, such as http://jsonlint.com. Shutting down." )
32+
sys.exit()
2733
try:
2834
with open("config/config.json", "r+") as config:
29-
self.config.update(json.load(config))
30-
except:
31-
self.logger.exception("Could not load the default configuration file.", exc_info=True)
32-
raise
35+
self.config = recursive_dictionary_update(self.config, json.load(config))
36+
except IOError:
37+
self.logger.warning("The configuration file (config.json) doesn't exist! Creating one from defaults.")
38+
with open("config/config.json", "w") as f:
39+
json.dump(self.config, f)
40+
except ValueError:
41+
self.logger.critical("The configuration file (config.json) contains invalid JSON. Please run it against a JSON linter, such as http://jsonlint.com. Shutting down.")
42+
sys.exit()
43+
3344
self.logger.debug("Created configuration manager.")
45+
self.save()
3446

3547
def save(self):
3648
try:

config/config.json.default

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
"command_prefix": "/",
1313
"core_plugin_path": "core_plugins",
1414
"debug_file": "debug.log",
15-
"logging_format_console": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
15+
"logging_format_console": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
1616
"logging_format_debugfile": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
1717
"logging_format_logfile": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
18-
"owner_uuid": "!!! SET THIS !!!",
18+
"owner_uuid": "!!--REPLACE THIS--!!",
1919
"passthrough": false,
2020
"player_db": "config/player.db",
2121
"plugin_config": {
@@ -33,8 +33,88 @@
3333
],
3434
"message": "Welcome to the server! Here are some starter items to get you off to a good start."
3535
},
36+
"planet_protect": {
37+
"blacklist": [
38+
"bomb",
39+
"bombblockexplosion",
40+
"bouldersmashexplosion",
41+
"bouncycluster",
42+
"bouncyclustergrenade",
43+
"cluster",
44+
"clustergoo",
45+
"clustergrenade",
46+
"defensiveexplosion",
47+
"dungeonpodexplosion",
48+
"electricexplosion",
49+
"electrogrenade",
50+
"explosivebullet",
51+
"explosivegoo",
52+
"fireexplosion",
53+
"friendlyboneexplosion",
54+
"gas",
55+
"gas2",
56+
"gasgrenade",
57+
"glowbomb",
58+
"glowgas",
59+
"gravitybomb",
60+
"grenade",
61+
"grenade",
62+
"grenadeexplosion",
63+
"icestorm",
64+
"impactgrenade",
65+
"invisibleprojectile",
66+
"jellybossexplode",
67+
"jellybossgoo",
68+
"jumpbomb",
69+
"jumpgas",
70+
"largemeteor",
71+
"lavaballoon",
72+
"lavaprojectile",
73+
"magicmolotov",
74+
"megabeam",
75+
"meteor",
76+
"meteorblockprojectile",
77+
"meteorblockspawner",
78+
"meteorexplosion",
79+
"molotov",
80+
"molotovflame",
81+
"nail",
82+
"nailbomb",
83+
"plasmabullet",
84+
"plasmaexplosion2",
85+
"plasmagrenade",
86+
"plasmatorpedo",
87+
"poisonsmoke",
88+
"poisonstatusprojectile",
89+
"poopbreath",
90+
"pulsecannon",
91+
"pulsecannonexplosion",
92+
"purpleplasma",
93+
"regularexplosion",
94+
"regularexplosion2",
95+
"regularexplosionnospark",
96+
"rocket",
97+
"rocketexplosion",
98+
"runbomb",
99+
"rungas",
100+
"shockbomb",
101+
"smallmeteor",
102+
"smallregularexplosion",
103+
"targetexplosion",
104+
"vsmallregularexplosion",
105+
"vsmallregularexplosionnodamage",
106+
"water",
107+
"zbomb",
108+
"spinningrocket",
109+
"stationaryrocket"
110+
],
111+
"protected_planets": []
112+
},
36113
"player_manager": {
37-
"name_removal_regexes": ["[\\s]"]
114+
"name_removal_regexes": [
115+
"\\^#[\\w]+;",
116+
"[^ \\w]+"
117+
]
38118
}
39119
},
40120
"plugin_path": "plugins",

packets/packet_types.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from construct import *
22
from enum import IntEnum
3-
from data_types import SignedVLQ, VLQ, Variant, star_string
3+
from data_types import SignedVLQ, VLQ, Variant, star_string, DictVariant
44

55

66
class Direction(IntEnum):
@@ -58,6 +58,18 @@ class Packets(IntEnum):
5858
HEARTBEAT = 46
5959

6060

61+
class EntityType(IntEnum):
62+
END = -1
63+
PLAYER = 0
64+
MONSTER = 1
65+
OBJECT = 2
66+
ITEMDROP = 3
67+
PROJECTILE = 4
68+
PLANT = 5
69+
PLANTDROP = 6
70+
EFFECT = 7
71+
72+
6173
class PacketOutOfOrder(Exception):
6274
pass
6375

@@ -164,6 +176,7 @@ def _decode(self, obj, context):
164176
),
165177
player=player))
166178

179+
167180
world_start = lambda name="world_start": Struct(name,
168181
VLQ("planet_size"),
169182
Bytes("planet", lambda ctx: ctx.planet_size),
@@ -205,4 +218,14 @@ def _decode(self, obj, context):
205218
update_world_properties_write = lambda dictionary: update_world_properties().build(
206219
Container(
207220
count=len(dictionary),
208-
properties=[Container(key=k, value=Container(type="SVLQ", data=v)) for k, v in dictionary.items()]))
221+
properties=[Container(key=k, value=Container(type="SVLQ", data=v)) for k, v in dictionary.items()]))
222+
223+
entity_create = Struct("entity_create",
224+
GreedyRange(
225+
Struct("entity",
226+
Byte("entity_type"),
227+
VLQ("entity_size"),
228+
String("entity", lambda ctx: ctx.entity_size),
229+
SignedVLQ("entity_id")
230+
)))
231+
projectile = DictVariant("projectile")

plugins/planet_protect/planet_protect_plugin.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from base_plugin import SimpleCommandPlugin
22
from core_plugins.player_manager import UserLevels, permissions
3+
from packets import entity_create, EntityType, star_string
34

45

56
class PlanetProtectPlugin(SimpleCommandPlugin):
@@ -27,12 +28,9 @@ def activate(self):
2728
"MODIFY_TILE_LIST"]
2829
for n in ["on_" + n.lower() for n in bad_packets]:
2930
setattr(self, n, (lambda x: self.planet_check()))
30-
if self.config.plugin_config == {}:
31-
self.protected_planets = []
32-
else:
33-
self.protected_planets = self.config.plugin_config
34-
35-
self.player_manager = self.plugins['player_manager']
31+
self.protected_planets = self.config.plugin_config.get("protected_planets", [])
32+
self.blacklist = self.config.plugin_config.get("blacklist", [])
33+
self.player_manager = self.plugins.get("player_manager", [])
3634

3735
def planet_check(self):
3836
if self.protocol.player.planet in self.protected_planets and self.protocol.player.access_level < UserLevels.REGISTERED:
@@ -73,5 +71,15 @@ def unprotect(self, data):
7371
self.save()
7472

7573
def save(self):
76-
self.config.plugin_config = self.protected_planets
74+
self.config.plugin_config['protected_planets'] = self.protected_planets
75+
self.config.plugin_config['blacklist'] = self.blacklist
7776

77+
def on_entity_create(self, data):
78+
if self.protocol.player.planet in self.protected_planets and self.protocol.player.access_level <= UserLevels.MODERATOR:
79+
entities = entity_create.parse(data.data)
80+
for entity in entities.entity:
81+
if entity.entity_type == EntityType.PROJECTILE:
82+
p_type = star_string("").parse(entity.entity)
83+
if p_type in self.blacklist:
84+
self.logger.info("Player %s attempted to use a prohibited projectile, %s, on a protected planet.", self.protocol.player.name, p_type)
85+
return False

utility_functions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import collections
12
import logging
23

34
from construct import Container
@@ -21,6 +22,16 @@ def give_item_to_player(player_protocol, item, count=1):
2122
item_count -= x
2223

2324

25+
def recursive_dictionary_update(d, u):
26+
for k, v in u.iteritems():
27+
if isinstance(v, collections.Mapping):
28+
r = recursive_dictionary_update(d.get(k, {}), v)
29+
d[k] = r
30+
else:
31+
d[k] = u[k]
32+
return d
33+
34+
2435
def build_packet(packet_type, data):
2536
"""
2637
Convenience method to build packets for sending.

0 commit comments

Comments
 (0)