diff options
author | Dico200 <dico.karssiens@gmail.com> | 2015-10-20 01:01:53 +0200 |
---|---|---|
committer | Dico200 <dico.karssiens@gmail.com> | 2015-10-20 01:01:53 +0200 |
commit | 0676c535ad34373740c7ca7e88874965ff9b4883 (patch) | |
tree | ea86e17e4d5fab7e30309a708906c6bb26dc22a3 | |
parent | a637954ef7ae8fe2028144a641bd7efeaa6f5aa7 (diff) |
Finished signal strength command
-rw-r--r-- | misc.py | 137 |
1 files changed, 78 insertions, 59 deletions
@@ -4,9 +4,13 @@ from time import time as now from time import sleep from sys import exc_info import thread + import org.bukkit.inventory.ItemStack as ItemStack +import org.bukkit.Material as Material +from math import ceil + import org.bukkit.Bukkit as Bukkit -from basecommands import simplecommand +from basecommands import simplecommand, Validate @@ -70,7 +74,6 @@ def cmd_event2(event): - """ Disabled while builder can't access Trusted @hook.event("player.PlayerGameModeChangeEvent", "low") def on_gamemode(event): @@ -142,17 +145,24 @@ def on_sudo_command(sender, command, label, args): return "&cPlayer %s not found!" % target -container_levels = dict( - FURNACE = [], - HOPPER = [], - CHEST = [], -) - - """ Suggestion by Armadillo28, see thread: http://redstoner.com/forums/threads/2213-putting-the-correct-amount-of-items-in-a-container?page=1#reply-14507 + +Clarification on these formulas on http://minecraft.gamepedia.com/Redstone_Comparator#Containers """ -""" + +def required_item_count(strength, slots, stack): + if strength == 0: + count = 0 + elif strength == 1: + count = 1 + else: + count = int(ceil(slots * stack / 14.0 * (strength - 1))) + + resulting_strength = int(1 + 14.0 * count / stack / slots) + + return count if resulting_strength == strength else None + @simplecommand("signalstrength", usage = "<signal strength> [item] [data]", aliases = ["ss", "level"], @@ -163,58 +173,67 @@ Suggestion by Armadillo28, see thread: http://redstoner.com/forums/threads/2213- helpSubcmd = True, senderLimit = 0) def on_signalstrength_command(sender, command, label, args): - values = [] - try: - for i in range(len(args)): - values[i] = int(args[i]) - except ValueError: - return "&cYou have to enter a number for the signal strength" - target_block = sender.getTargetBlock(None, 5) - Validate.notNone(target_block) - target_type = str(target_block.getType()) - Validate.isTrue(target_type in container_levels) - - levels = container_levels[target_type] - - item_value = 1 - item_type = Material.REDSTONE - item_data = 0 - if len(values) > 1: - item_type = Material.getMaterial(values[1]) - if item_type == None: - return "&cThat is not an item ID" - if len(values) == 3: - item_data = values[2] - if not (0 <= item_data <= 15): - return "&cThe data must be a number from 0 to a maximum of 15" - if values[1] in (items that stack up to 16): - item_value = 4 - elif values[1] in (items that stack up to 1): - item_value = 64 - - target_strength = values[0] - - item_count = levels[target_strength] / item_value - - if int(item_count) * item_value < levels[target_strength] and (int(item_count) + 1) * item_value >= levels[target_strength + 1]: #if target_strength = 15, first check will always be false, so no IndexError - return "&cThe desired signal strength could not be achieved with the requested item type" - - item_count = int(item_count) - if item_count % 1 != 0: - item_count += 1 - - def stack(count): - return ItemStack(values[1]) - - full_stacks = int(item_count / item_value) - for i in range(0, full_stacks) #amount of full stacks - target_block.setItem(i, ItemStack()) -""" - - + Validate.notNone(target_block, "&cThat command can only be used when a container is targeted") + try: + inv = target_block.getState().getInventory() + except AttributeError: + return "&cThat command can only be used when a container is targeted" + + #--------Check if all arguments are an int >= 0 and reassign args--------- + for i in range(len(args)): + if not args[i].isdigit(): + return "&cThe %s has to be a number >= 0" % ("signal strength", "item", "data")[i] + args = [int(arg) for arg in args] + + #---------Define the requested strength, item type and item data---------- + strength = args[0] + Validate.isTrue(0 <= strength <= 15, "&cThe signal strength has to be a value from 0 to 15") + + item_type = Material.REDSTONE if len(args) < 2 else Material.getMaterial(args[1]) + Validate.notNone(item_type, "&cThat item id does not exist") + + item_data = 0 if len(args) < 3 else args[2] + Validate.isTrue(0 <= item_data <= 15, "&cThe data has to be a value from 0 to 15") + + #--------Get the stack size and required amount of items to achieve the desired signal strength--------- + stack_size = item_type.getMaxStackSize() + item_count = required_item_count(strength, inv.getSize(), stack_size) + Validate.notNone(item_count, "&cThe desired signal strength could not be achieved with the requested item type") + + #------------Add the other side of the chest if target is a double chest-------------- + target_blocks = [target_block] + target_type = target_block.getType() + if target_type in (Material.CHEST, Material.TRAPPED_CHEST): + loc = target_block.getLocation() + x = loc.getBlockX() + y = loc.getBlockY() + z = loc.getBlockZ() + world = loc.getWorld() + + target_blocks += [ + block for block in ( + world.getBlockAt(x + 1, y, z), + world.getBlockAt(x - 1, y, z), + world.getBlockAt(x, y, z + 1), + world.getBlockAt(x, y, z - 1), + ) if block.getType() == target_type + ] + + #----------------Insert items------------- + full_stack_count, remaining = divmod(item_count, stack_size) + + inv.clear() + for block in target_blocks: + for i in range(full_stack_count): + inv.setItem(i, ItemStack(item_type, stack_size, item_data)) + if remaining > 0: + inv.setItem(full_stack_count, ItemStack(item_type, remaining, item_data)) + + + return "&aSuccesfully edited the targeted %s to give out a signal strenth of %s to comparators" % (str(target_type).lower().replace("_", " "), strength) |