summaryrefslogtreecommitdiff
path: root/blockplacemods.py
diff options
context:
space:
mode:
authorDico200 <dico.karssiens@gmail.com>2015-05-14 02:16:33 +0200
committerDico200 <dico.karssiens@gmail.com>2015-05-14 02:16:33 +0200
commit59816b58e2906318a79ecdf4a6d6b4b75f3aa6e8 (patch)
treeb3dc3f458495ac6e5d017544b3f2e314c0ffca67 /blockplacemods.py
parent0ef318d6b95c3f602c4409e6849bea8e29ac9367 (diff)
Almost complete redo of blockplacemods, small fixes
I've redone most of blockplacemods its code. It is now MUCH easier to expand in the future and I think the code is more elegant and stuff. It all works the same as before, except the command is now /toggle <setting> I've merged the setting for /autotakewater and /autofillcauldron into one setting named cauldron.
Diffstat (limited to 'blockplacemods.py')
-rw-r--r--blockplacemods.py258
1 files changed, 146 insertions, 112 deletions
diff --git a/blockplacemods.py b/blockplacemods.py
index 3882d4f..c167aca 100644
--- a/blockplacemods.py
+++ b/blockplacemods.py
@@ -5,122 +5,156 @@ import org.bukkit.block.Furnace as Furnace
import org.bukkit.inventory.ItemStack as ItemStack
import org.bukkit.Material as Material
-denyslabcorrection = open_json_file("denyslabcorrection", []) #Players that don't want slabs corrected
-denyautofill = open_json_file("denyautocauldronfill", [])
-denyautolevel = open_json_file("denyautocauldronlevel", [])
-
-def saveslabs():
- save_json_file("denyslabcorrection", denyslabcorrection)
-def savecauldrons():
- save_json_file("denyautocauldronfill", denyautofill)
-def savelevels():
- save_json_file("denyautocauldronlevel", denyautolevel)
-
-@simplecommand("autofillcauldron",
- aliases = ["fillcauldronautomatically"],
- usage = "on/off",
- helpNoargs = True,
- description = "Sets whether you want placed cauldrons to fill \nautomatically",
- amax = 1,
- senderLimit = 0)
-def autofillcauldron_command(sender, command, label, args):
- uuid = uid(server.getPlayer(sender.getName()))
- if args[0].lower() == "off":
- if uuid in denyautofill:
- return "&cAuto fillment of cauldrons is already disabled"
- denyautofill.append(uuid)
- savecauldrons()
- return "&aFilling cauldrons will no longer happen automatically"
- if args[0].lower() == "on":
- if uuid not in denyautofill:
- return "&cAuto fillment of cauldrons is already enabled"
- denyautofill.remove(uuid)
- savecauldrons()
- return "&aFilling cauldrons will happen automatically from now"
- return "HELP"
-
-
-@simplecommand("autoflipslab",
- aliases = ["autoflipstep", "flipslabautomatically", "flipstepautomatically"],
- usage = "on/off",
- helpNoargs = True,
- description = "Sets whether you want placed slabs to be turned \nupside-down",
- amax = 1,
- senderLimit = 0)
-def autoflipslab_command(sender, command, label, args):
- uuid = uid(server.getPlayer(sender.getName()))
- if args[0].lower() == "off":
- if uuid in denyslabcorrection:
- return "&cAuto flipping of slabs is already disabled"
- denyslabcorrection.append(uuid)
- saveslabs()
- return "&aFlipping slabs will no longer happen automatically"
- if args[0].lower() == "on":
- if uuid not in denyslabcorrection:
- return "&cAuto flipping of slabs is already enabled"
- denyslabcorrection.remove(uuid)
- saveslabs()
- return "&aFlipping slabs will happen automatically from now"
- return "HELP"
-
-
-@simplecommand("autotakewater",
- aliases = ["autocauldronlevel"],
- usage = "on/off",
- helpNoargs = True,
- description = "Sets whether you want right clicking cauldrons \nwith empty hand or redstone dust \nto lower water level",
- amax = 1,
- senderLimit = 0)
-def autoflipslab_command(sender, command, label, args):
- uuid = uid(server.getPlayer(sender.getName()))
- if args[0].lower() == "off":
- if uuid in denyautolevel:
- return "&cTaking water with hand/redstone is already disabled"
- denyautolevel.append(uuid)
- savelevels()
- return "&aYou can no longer take water with hand/redstone"
- if args[0].lower() == "on":
- if uuid not in denyautolevel:
- return "&cTaking water with hand/redstone is already enabled"
- denyautolevel.remove(uuid)
- savelevels()
- return "&aYou can take water with hand/redstone from now"
- return "HELP"
+settingInformation = {
+ "cauldron": [0,
+ "easy cauldron water level control",
+ "Toggles whether cauldrons auto-fill upon placement and whether right clicking them with redstone dust or empty hand will cycle their water level"
+ ],
+ "slab": [0,
+ "automatically flipping placed slabs upside-down",
+ "Toggles whether slabs/steps which you place should be automatically flipped upside-down"
+ ],
+ "furnace": [1,
+ "automatically filling furnaces upon placement",
+ "Sets your preferred default furnace contents to your currently held itemstack. Use an empty hand to disable this feature."
+ ]
+}
+
+defaultPlayerSettings = {
+ "cauldron": [],
+ "slab": [],
+ "furnace": {}
+}
+
+playerSettings = open_json_file("blockplacemods", defaultPlayerSettings)
+
+
+#for setting, default in enumerate(defaultPlayerSettings):
+# if playerSettings.get(setting) == None:
+# playerSettings[setting] = default
+
+def get(setting):
+ return playerSettings[setting]
+
+
+def saveSettings():
+ save_json_file("blockplacemods", playerSettings)
+
+
+
+@simplecommand("toggle",
+ aliases = ["set"],
+ usage = "<setting> [value|info]",
+ description = "Toggles or sets your preferences for our redstone utilities.\nThe following settings are available:\n" + ", ".join([x for x in settingInformation]),
+ senderLimit = 0,
+ helpNoargs = True,
+ helpSubcmd = True,
+ amax = 2)
+def toggle_command(sender, command, label, args):
+ setting = args[0].lower()
+ info = settingInformation.get(setting)
+ if info == None:
+ return "&cThat setting could not be found. For command help, use &o/toggle"
+
+ values = get(setting)
+ player = server.getPlayer(sender.getName())
+ uuid = uid(player)
+ arglen = len(args)
+
+ if info[0] == 0: # Toggle
+ enabled = uuid not in values
+ new = None
+ if arglen == 1:
+ new = not enabled
+ else:
+ arg2 = args[1].lower()
+ if arg2 == "info":
+ return " &aSetting %s:\n &9%s\n &6Accepted arguments: None or one of the following:\n &oon, enable, off, disable, toggle, switch" % (setting, info[2])
+ elif arg2 in ("toggle", "switch"):
+ new = not enabled
+ elif arg2 in ("on", "enable"):
+ new = True
+ elif arg2 in ("off", "disable"):
+ new = False
+ else:
+ return "&cArgument '%s' was not recognized. \nTry one of the following: &oon, off, toggle" % arg2
+ if enabled == new:
+ return "&cAlready %s: &a%s" % ("enabled" if enabled else "disabled", info[1])
+ if new:
+ values.remove(uuid)
+ else:
+ values.append(uuid)
+ saveSettings()
+ return ("&aEnabled " if new else "&aDisabled ") + info[1]
+
+ elif info[0] == 1: # Save ItemStack in hand
+ if arglen == 1:
+ item = fromStack(player.getItemInHand())
+ if 0 in (item[0], item[1]):
+ del values[uuid]
+ return "&aDisabled " + info[1]
+ values[uuid] = item
+ saveSettings()
+ return "&aEnabled %s, with currently held itemstack" % info[1]
+ if args[1].lower() == "info":
+ return "&aSetting %s:\n&9%s" % (setting, info[2])
+ return "&cArgument '%s' was not recognized. \nUse /toggle %s info for more information." % setting
+
+ return None #This shouldn't happen
+
+
+def fromStack(itemStack):
+ return [itemStack.getTypeId(), itemStack.getAmount(), itemStack.getData().getData()]
+def toStack(lst):
+ return ItemStack(lst[0], lst[1], lst[2])
+
+def isEnabled(toggleSetting, uuid):
+ return uuid not in get(toggleSetting)
+
@hook.event("block.BlockPlaceEvent", "monitor")
def on_block_place(event):
- if event.isCancelled():
- return
- player = event.getPlayer()
- if player.getWorld().getName() not in ("Creative", "Trusted", "world"):
- return
- uuid = uid(player)
- block = event.getBlockPlaced()
- material = str(block.getType())
- if uuid not in denyslabcorrection and material in ("WOOD_STEP", "STEP") and block.getData() < 8:
- block.setData(block.getData() + 8) # Flip upside down
- elif uuid not in denyautofill and material == "CAULDRON":
- block.setData(3) #3 layers of water, 3 signal strength
- elif material == "FURNACE":
- state = block.getState()
- state.getInventory().setSmelting(ItemStack(Material.REDSTONE))
- state.update()
+ try:
+ if event.isCancelled():
+ return
+ player = event.getPlayer()
+ if not is_creative(player):
+ return
+
+ uuid = uid(player)
+ block = event.getBlockPlaced()
+ material = str(block.getType())
+ if isEnabled("slab", uuid) and material in ("WOOD_STEP", "STEP") and block.getData() < 8:
+ block.setData(block.getData() + 8) # Flip upside down
+ elif isEnabled("cauldron", uuid) and material == "CAULDRON":
+ block.setData(3) #3 layers of water, 3 signal strength
+ elif material == "FURNACE":
+ stack = get("furnace").get(uuid)
+ if stack == None:
+ return
+ state = block.getState()
+ state.getInventory().setSmelting(toStack(stack))
+ state.update()
+ except:
+ error(trace())
+
@hook.event("player.PlayerInteractEvent", "monitor")
def on_interact(event):
- player = event.getPlayer()
- if uid(player) in denyautolevel or player.getWorld().getName() not in ("Creative", "Trusted", "world"):
- return
- if str(event.getAction()) != "RIGHT_CLICK_BLOCK":
- return
- if event.hasItem() and not str(event.getItem().getType()) == "REDSTONE":
- return
- block = event.getClickedBlock()
- if str(block.getType()) != "CAULDRON":
- return
- event2 = BlockBreakEvent(block, player)
- server.getPluginManager().callEvent(event2)
- if not event2.isCancelled():
- data = block.getData()
- block.setData(data - 1 if data > 0 else 3)
+ try:
+ player = event.getPlayer()
+ if (isEnabled("cauldron", uid(player))
+ and is_creative(player)
+ and str(event.getAction()) == "RIGHT_CLICK_BLOCK"
+ and (not event.hasItem() or str(event.getItem().getType()) == "REDSTONE")
+ and str(event.getClickedBlock().getType()) == "CAULDRON"
+ ):
+ block = event.getClickedBlock()
+ event2 = BlockBreakEvent(block, player)
+ server.getPluginManager().callEvent(event2)
+ if not event2.isCancelled():
+ block.setData(block.getData() - 1 if block.getData() > 0 else 3)
+ except:
+ error(trace())
+