Archived
0

Added invalid strength values check

This commit is contained in:
Pepich
2018-01-01 18:13:38 +01:00
parent edf93f5272
commit 13f6a3babb

View File

@@ -1,10 +1,9 @@
package com.redstoner.modules.signalstrength; package com.redstoner.modules.signalstrength;
import com.nemez.cmdmgr.Command; import java.util.ArrayList;
import com.redstoner.annotations.Commands; import java.util.Arrays;
import com.redstoner.annotations.Version; import java.util.Set;
import com.redstoner.misc.CommandHolderType;
import com.redstoner.modules.Module;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.GameMode; import org.bukkit.GameMode;
@@ -22,42 +21,52 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.util.ArrayList; import com.nemez.cmdmgr.Command;
import java.util.Arrays; import com.redstoner.annotations.Commands;
import java.util.Set; import com.redstoner.annotations.Version;
import com.redstoner.misc.CommandHolderType;
import com.redstoner.modules.Module;
@Commands(CommandHolderType.File) @Commands(CommandHolderType.File)
@Version(major = 4, minor = 0, revision = 1, compatible = 4) @Version(major = 4, minor = 0, revision = 2, compatible = 4)
public class SignalStrength implements Module public class SignalStrength implements Module
{ {
private static final String namePrefix = ChatColor.GREEN.toString() + ChatColor.RESET + ChatColor.DARK_PURPLE + "Signal Strength: " + ChatColor.RED + ChatColor.BOLD; private static final String namePrefix = ChatColor.GREEN.toString() + ChatColor.RESET + ChatColor.DARK_PURPLE
+ "Signal Strength: " + ChatColor.RED + ChatColor.BOLD;
private static String nameForSignalStrength(int strength) private static String nameForSignalStrength(int strength)
{ {
return namePrefix + strength; return namePrefix + strength;
} }
private static boolean isSignalStrengthNameOrEmpty(String name) private static boolean isSignalStrengthNameOrEmpty(String name)
{ {
return name == null || name.isEmpty() || name.startsWith(namePrefix); return name == null || name.isEmpty() || name.startsWith(namePrefix);
} }
@Command(hook = "ss") @Command(hook = "ss")
public boolean ss(CommandSender sender, int strength) public boolean ss(CommandSender sender, int strength)
{ {
return ssm(sender, strength, Material.REDSTONE.toString()); return ssm(sender, strength, Material.REDSTONE.toString());
} }
@Command(hook = "ssm") @Command(hook = "ssm")
public boolean ssm(CommandSender sender, int strength, String material) public boolean ssm(CommandSender sender, int strength, String material)
{ {
if (strength < 0 || strength > 15)
{
getLogger().message(sender, true, "The strength must be between 0 and 15!");
return true;
}
Player player = (Player) sender; Player player = (Player) sender;
if (player.getGameMode() != GameMode.CREATIVE) { if (player.getGameMode() != GameMode.CREATIVE)
{
getLogger().message(sender, true, "You must be in creative mode to do that"); getLogger().message(sender, true, "You must be in creative mode to do that");
return true; return true;
} }
Material itemType = Material.matchMaterial(material); Material itemType = Material.matchMaterial(material);
if (itemType == null) if (itemType == null)
{ {
@@ -79,7 +88,7 @@ public class SignalStrength implements Module
getLogger().message(sender, true, "That command can only be used if a container is targeted!"); getLogger().message(sender, true, "That command can only be used if a container is targeted!");
return true; return true;
} }
// --------Get the stack size and required amount of items to achieve the desired signal strength--------- // --------Get the stack size and required amount of items to achieve the desired signal strength---------
int stackSize = itemType.getMaxStackSize(); int stackSize = itemType.getMaxStackSize();
int slotCount = inventory.getSize(); int slotCount = inventory.getSize();
@@ -93,16 +102,14 @@ public class SignalStrength implements Module
// #--------Add the other side of the chest if target is a double chest and check if player can build--------- // #--------Add the other side of the chest if target is a double chest and check if player can build---------
ArrayList<Block> containerBlocks = new ArrayList<>(); ArrayList<Block> containerBlocks = new ArrayList<>();
containerBlocks.add(targetBlock); containerBlocks.add(targetBlock);
Material blockType = targetBlock.getType(); Material blockType = targetBlock.getType();
if (inventory.getType() == InventoryType.CHEST) if (inventory.getType() == InventoryType.CHEST)
{ {
Arrays.stream(new BlockFace[]{BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST, BlockFace.NORTH}) Arrays.stream(new BlockFace[] {BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST, BlockFace.NORTH})
.map(targetBlock::getRelative) .map(targetBlock::getRelative).filter(b -> b.getType() == blockType).forEach(containerBlocks::add);
.filter(b -> b.getType() == blockType)
.forEach(containerBlocks::add);
} }
for (Block containerBlock : containerBlocks) for (Block containerBlock : containerBlocks)
{ {
if (!canBuild(player, containerBlock)) if (!canBuild(player, containerBlock))
@@ -118,29 +125,31 @@ public class SignalStrength implements Module
{ {
// Below checks should evaluate to false, but let's be safe. // Below checks should evaluate to false, but let's be safe.
BlockState blockState = containerBlock.getState(); BlockState blockState = containerBlock.getState();
if (!(blockState instanceof InventoryHolder)) continue; if (!(blockState instanceof InventoryHolder))
continue;
if (blockState instanceof Nameable && isSignalStrengthNameOrEmpty(((Nameable) blockState).getCustomName())) if (blockState instanceof Nameable && isSignalStrengthNameOrEmpty(((Nameable) blockState).getCustomName()))
{ {
((Nameable) blockState).setCustomName(nameForSignalStrength(strength)); ((Nameable) blockState).setCustomName(nameForSignalStrength(strength));
blockState.update(); blockState.update();
} }
Inventory inv = ((InventoryHolder) blockState).getInventory(); Inventory inv = ((InventoryHolder) blockState).getInventory();
if (inv == null) continue; if (inv == null)
continue;
inv.clear(); inv.clear();
for (int i = 0; i < fullStackCount; i++) for (int i = 0; i < fullStackCount; i++)
inv.setItem(i, new ItemStack(itemType, stackSize)); inv.setItem(i, new ItemStack(itemType, stackSize));
if (remaining > 0) if (remaining > 0)
inv.setItem(fullStackCount, new ItemStack(itemType, remaining)); inv.setItem(fullStackCount, new ItemStack(itemType, remaining));
} }
getLogger().message(sender, "Comparators attached to this " + enumNameToHumanName(blockType.name()) getLogger().message(sender, "Comparators attached to this " + enumNameToHumanName(blockType.name())
+ " will now put out a signal strength of " + strength); + " will now put out a signal strength of " + strength);
return true; return true;
} }
private static Inventory getInventory(Block b) private static Inventory getInventory(Block b)
{ {
BlockState state = b.getState(); BlockState state = b.getState();
@@ -148,7 +157,7 @@ public class SignalStrength implements Module
return ((InventoryHolder) state).getInventory(); return ((InventoryHolder) state).getInventory();
return null; return null;
} }
private static int computeRequiredItemCount(int strength, int stackSize, int slotCount) private static int computeRequiredItemCount(int strength, int stackSize, int slotCount)
{ {
int itemCount = -1; int itemCount = -1;
@@ -158,7 +167,7 @@ public class SignalStrength implements Module
itemCount = 1; itemCount = 1;
else else
itemCount = (int) Math.ceil(slotCount * stackSize / 14.0 * (strength - 1)); itemCount = (int) Math.ceil(slotCount * stackSize / 14.0 * (strength - 1));
// Reverse engineer the calculation to verify // Reverse engineer the calculation to verify
int resultingStrength = itemCount == 0 ? 0 : (int) Math.floor(1 + 14.0 * itemCount / stackSize / slotCount); int resultingStrength = itemCount == 0 ? 0 : (int) Math.floor(1 + 14.0 * itemCount / stackSize / slotCount);
if (resultingStrength != strength) if (resultingStrength != strength)
@@ -168,7 +177,7 @@ public class SignalStrength implements Module
// Clarification on these formulas at https://minecraft.gamepedia.com/Redstone_Comparator#Containers // Clarification on these formulas at https://minecraft.gamepedia.com/Redstone_Comparator#Containers
return itemCount; return itemCount;
} }
private static boolean canBuild(Player p, Block b) private static boolean canBuild(Player p, Block b)
{ {
BlockPlaceEvent event = new BlockPlaceEvent(b, b.getState(), b.getRelative(BlockFace.DOWN), BlockPlaceEvent event = new BlockPlaceEvent(b, b.getState(), b.getRelative(BlockFace.DOWN),
@@ -176,11 +185,10 @@ public class SignalStrength implements Module
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
return !event.isCancelled(); return !event.isCancelled();
} }
private static String enumNameToHumanName(String enumName) private static String enumNameToHumanName(String enumName)
{ {
return enumName.toLowerCase().replace('_', ' '); return enumName.toLowerCase().replace('_', ' ');
} }
} }