summaryrefslogtreecommitdiff
path: root/damnspam.py
blob: 7df9cb68ea073ef688bbf76eec2471cbd0868dfa (plain)
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
#pylint: disable = F0401
from helpers import *
from time import time as now
import org.bukkit.event.block.BlockBreakEvent as BlockBreakEvent

inputs          = open_json_file("damnspam", {}) # format "x;y;z;World"
accepted_inputs = ["WOOD_BUTTON", "STONE_BUTTON", "LEVER"]
changing_input  = False
removing_input  = False


def save_inputs():
  save_json_file("damnspam", inputs)


def location_str(block):
  return ";".join([block.getWorld().getName(), str(block.getX()), str(block.getY()), str(block.getZ())])


def add_input(creator, block, timeout_off, timeout_on):
  inputs[location_str(block)] = {
    "creator"     : uid(creator),
    "timeout_off" : timeout_off,
    "timeout_on"  : timeout_on,
    "last_time"   : 0
  }


@hook.command("damnspam")
def on_dammnspam_command(sender, args):
  global changing_input

  plugin_header(sender, "DamnSpam")
  if len(args) in [1,2]:

    if not is_creative(sender):
      msg(sender, "&cYou can only do this in Creative mode.")
      return True

    # /damnspam <secs>
    if len(args) == 1:
      timeout_on = args[0]
      try:
        timeout_on  = round(float(timeout_on), 2)
        timeout_off = timeout_on
        if not 0 <= timeout_on <= 60:
          msg(sender, "&cThe timeout must be within 0-60.")
          return True
      except ValueError:
        msg(sender, "&cThe timeout must be a number")
        return True

    # /damnspam <off> <on>
    elif len(args) == 2:
      timeout_on  = args[0]
      timeout_off = args[1]
      try:
        timeout_on  = round(float(timeout_on), 2)
        timeout_off = round(float(timeout_off), 2)
        if not 0 <= timeout_on <= 60 or not 0 <= timeout_off <= 60:
          msg(sender, "&cThe timeout must be within 0-60.")
          return True
      except ValueError:
        msg(sender, "&cThe timeout must be a number")
        return True

    # get the block we're looking at
    target = sender.getTargetBlock(None, 10)
    ttype  = str(target.getType())
    if ttype not in accepted_inputs:
      msg(sender, "&cPlease look at a button or lever while executing this command!")
      return True

    if location_str(target) in inputs:
      changing_input = True # this input already has a timeout
    # test if player is allowed to build here
    test_event = BlockBreakEvent(target, sender)
    server.getPluginManager().callEvent(test_event)
    changing_input = False
    if test_event.isCancelled():
      msg(sender, "&cYou are not allowed to modify this input")
      return True

    # add block to inputs
    add_input(sender, target, timeout_off, timeout_on)
    save_inputs()
    msg(sender, "&aSuccessfully set a timeout for this %s." % ttype.lower())
    return True

  else:
    msg(sender, "&c/damnspam <seconds> &e(Buttons/Levers)")
    msg(sender, "&c/damnspam <seconds after off> <seconds after on> &e(Levers only)")


@hook.event("block.BlockBreakEvent", "normal")
def on_block_break(event):
  global removing_input

  if removing_input:
    return True
  sender = event.getPlayer()
  block = event.getBlock()
  if str(block.getType()) in accepted_inputs and not event.isCancelled():
    pos_str = location_str(block)
    if inputs.get(pos_str):
      plugin_header(sender, "DamnSpam")
      if sender.isSneaking():
        # test if player is allowed to build here
        removing_input = True
        test_event     = BlockBreakEvent(block, sender)
        server.getPluginManager().callEvent(test_event)
        removing_input = False
        if test_event.isCancelled():
          event.setCancelled(True)
          msg(sender, "&cYou are not allowed to remove this input")
          return True
        inputs.pop(pos_str) # remove
        save_inputs()
        msg(sender, "&eSuccessfully removed this input!")
        return True
      elif not changing_input:
        event.setCancelled(True)
        msg(sender, "&cYou cannot destroy this input!")
        msg(sender, "&c&nSneak&c and break if you want to remove it.")
        return True


@hook.event("player.PlayerInteractEvent", "normal")
def on_interact(event):
  if (str(event.getAction()) == "RIGHT_CLICK_BLOCK") and not event.isCancelled():
    sender  = event.getPlayer()
    block   = event.getClickedBlock()
    btype   = str(block.getType()).lower()
    powered = (block.getData() & 0x8) == 0x8 if btype == "lever" else False # data > 7, but this is how bukkit does it
    pos_str = location_str(block)
    data    = inputs.get(pos_str)
    if data:
      checktime = data["timeout_on"] if powered else data["timeout_off"]
      if data["last_time"] + checktime > now():
        event.setCancelled(True)
        plugin_header(sender, "DamnSpam")
        msg(sender, "&cThis %s has a timeout of %ss." % (btype, checktime))
      else:
        inputs[pos_str]["last_time"] = round(now(), 2)