PageRenderTime 66ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java

https://github.com/BodyshotzVG/Essentials
Java | 489 lines | 425 code | 61 blank | 3 comment | 31 complexity | 88342657e7f36e9b41cfd797285983f0 MD5 | raw file
  1. package com.earth2me.essentials.signs;
  2. import com.earth2me.essentials.Trade;
  3. import com.earth2me.essentials.ChargeException;
  4. import com.earth2me.essentials.IEssentials;
  5. import com.earth2me.essentials.User;
  6. import com.earth2me.essentials.Util;
  7. import java.util.HashSet;
  8. import java.util.Set;
  9. import org.bukkit.Material;
  10. import org.bukkit.block.Block;
  11. import org.bukkit.block.BlockFace;
  12. import org.bukkit.block.Sign;
  13. import org.bukkit.entity.Player;
  14. import org.bukkit.event.block.SignChangeEvent;
  15. import org.bukkit.inventory.ItemStack;
  16. public class EssentialsSign
  17. {
  18. private static final Set<Material> EMPTY_SET = new HashSet<Material>();
  19. protected transient final String signName;
  20. public EssentialsSign(final String signName)
  21. {
  22. this.signName = signName;
  23. }
  24. public final boolean onSignCreate(final SignChangeEvent event, final IEssentials ess)
  25. {
  26. final ISign sign = new EventSign(event);
  27. final User user = ess.getUser(event.getPlayer());
  28. if (!(user.isAuthorized("essentials.signs." + signName.toLowerCase() + ".create")
  29. || user.isAuthorized("essentials.signs.create." + signName.toLowerCase())))
  30. {
  31. // Return true, so other plugins can use the same sign title, just hope
  32. // they won't change it to §1[Signname]
  33. return true;
  34. }
  35. sign.setLine(0, Util.format("signFormatFail", this.signName));
  36. try
  37. {
  38. final boolean ret = onSignCreate(sign, user, getUsername(user), ess);
  39. if (ret)
  40. {
  41. sign.setLine(0, getSuccessName());
  42. }
  43. return ret;
  44. }
  45. catch (ChargeException ex)
  46. {
  47. ess.showError(user, ex, signName);
  48. }
  49. catch (SignException ex)
  50. {
  51. ess.showError(user, ex, signName);
  52. }
  53. // Return true, so the player sees the wrong sign.
  54. return true;
  55. }
  56. public String getSuccessName()
  57. {
  58. return Util.format("signFormatSuccess", this.signName);
  59. }
  60. public String getTemplateName()
  61. {
  62. return Util.format("signFormatTemplate", this.signName);
  63. }
  64. private String getUsername(final User user)
  65. {
  66. return user.getName().substring(0, user.getName().length() > 13 ? 13 : user.getName().length());
  67. }
  68. public final boolean onSignInteract(final Block block, final Player player, final IEssentials ess)
  69. {
  70. final ISign sign = new BlockSign(block);
  71. final User user = ess.getUser(player);
  72. try
  73. {
  74. return (user.isAuthorized("essentials.signs." + signName.toLowerCase() + ".use")
  75. || user.isAuthorized("essentials.signs.use." + signName.toLowerCase()))
  76. && onSignInteract(sign, user, getUsername(user), ess);
  77. }
  78. catch (ChargeException ex)
  79. {
  80. ess.showError(user, ex, signName);
  81. return false;
  82. }
  83. catch (SignException ex)
  84. {
  85. ess.showError(user, ex, signName);
  86. return false;
  87. }
  88. }
  89. public final boolean onSignBreak(final Block block, final Player player, final IEssentials ess)
  90. {
  91. final ISign sign = new BlockSign(block);
  92. final User user = ess.getUser(player);
  93. try
  94. {
  95. return (user.isAuthorized("essentials.signs." + signName.toLowerCase() + ".break")
  96. || user.isAuthorized("essentials.signs.break." + signName.toLowerCase()))
  97. && onSignBreak(sign, user, getUsername(user), ess);
  98. }
  99. catch (SignException ex)
  100. {
  101. ess.showError(user, ex, signName);
  102. return false;
  103. }
  104. }
  105. protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
  106. {
  107. return true;
  108. }
  109. protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
  110. {
  111. return true;
  112. }
  113. protected boolean onSignBreak(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
  114. {
  115. return true;
  116. }
  117. public final boolean onBlockPlace(final Block block, final Player player, final IEssentials ess)
  118. {
  119. User user = ess.getUser(player);
  120. try
  121. {
  122. return onBlockPlace(block, user, getUsername(user), ess);
  123. }
  124. catch (ChargeException ex)
  125. {
  126. ess.showError(user, ex, signName);
  127. }
  128. catch (SignException ex)
  129. {
  130. ess.showError(user, ex, signName);
  131. }
  132. return false;
  133. }
  134. public final boolean onBlockInteract(final Block block, final Player player, final IEssentials ess)
  135. {
  136. User user = ess.getUser(player);
  137. try
  138. {
  139. return onBlockInteract(block, user, getUsername(user), ess);
  140. }
  141. catch (ChargeException ex)
  142. {
  143. ess.showError(user, ex, signName);
  144. }
  145. catch (SignException ex)
  146. {
  147. ess.showError(user, ex, signName);
  148. }
  149. return false;
  150. }
  151. public final boolean onBlockBreak(final Block block, final Player player, final IEssentials ess)
  152. {
  153. User user = ess.getUser(player);
  154. try
  155. {
  156. return onBlockBreak(block, user, getUsername(user), ess);
  157. }
  158. catch (SignException ex)
  159. {
  160. ess.showError(user, ex, signName);
  161. }
  162. return false;
  163. }
  164. public boolean onBlockExplode(final Block block, final IEssentials ess)
  165. {
  166. return true;
  167. }
  168. public boolean onBlockBurn(final Block block, final IEssentials ess)
  169. {
  170. return true;
  171. }
  172. public boolean onBlockIgnite(final Block block, final IEssentials ess)
  173. {
  174. return true;
  175. }
  176. public boolean onBlockPush(final Block block, final IEssentials ess)
  177. {
  178. return true;
  179. }
  180. public static boolean checkIfBlockBreaksSigns(final Block block)
  181. {
  182. final Block sign = block.getRelative(BlockFace.UP);
  183. if (sign.getType() == Material.SIGN_POST && isValidSign(new BlockSign(sign)))
  184. {
  185. return true;
  186. }
  187. final BlockFace[] directions = new BlockFace[]
  188. {
  189. BlockFace.NORTH,
  190. BlockFace.EAST,
  191. BlockFace.SOUTH,
  192. BlockFace.WEST
  193. };
  194. for (BlockFace blockFace : directions)
  195. {
  196. final Block signblock = block.getRelative(blockFace);
  197. if (signblock.getType() == Material.WALL_SIGN)
  198. {
  199. final org.bukkit.material.Sign signMat = (org.bukkit.material.Sign)signblock.getState().getData();
  200. if (signMat.getFacing() == blockFace && isValidSign(new BlockSign(signblock)))
  201. {
  202. return true;
  203. }
  204. }
  205. }
  206. return false;
  207. }
  208. public static boolean isValidSign(final ISign sign)
  209. {
  210. return sign.getLine(0).matches("§1\\[.*\\]");
  211. }
  212. protected boolean onBlockPlace(final Block block, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
  213. {
  214. return true;
  215. }
  216. protected boolean onBlockInteract(final Block block, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
  217. {
  218. return true;
  219. }
  220. protected boolean onBlockBreak(final Block block, final User player, final String username, final IEssentials ess) throws SignException
  221. {
  222. return true;
  223. }
  224. public Set<Material> getBlocks()
  225. {
  226. return EMPTY_SET;
  227. }
  228. protected final void validateTrade(final ISign sign, final int index, final IEssentials ess) throws SignException
  229. {
  230. final String line = sign.getLine(index).trim();
  231. if (line.isEmpty())
  232. {
  233. return;
  234. }
  235. final Trade trade = getTrade(sign, index, 0, ess);
  236. final Double money = trade.getMoney();
  237. if (money != null)
  238. {
  239. sign.setLine(index, Util.formatCurrency(money, ess));
  240. }
  241. }
  242. protected final void validateTrade(final ISign sign, final int amountIndex, final int itemIndex,
  243. final User player, final IEssentials ess) throws SignException
  244. {
  245. final Trade trade = getTrade(sign, amountIndex, itemIndex, player, ess);
  246. final ItemStack item = trade.getItemStack();
  247. sign.setLine(amountIndex, Integer.toString(item.getAmount()));
  248. sign.setLine(itemIndex, sign.getLine(itemIndex).trim());
  249. }
  250. protected final Trade getTrade(final ISign sign, final int amountIndex, final int itemIndex,
  251. final User player, final IEssentials ess) throws SignException
  252. {
  253. final ItemStack item = getItemStack(sign.getLine(itemIndex), 1, ess);
  254. final int amount = Math.min(getIntegerPositive(sign.getLine(amountIndex)), item.getType().getMaxStackSize() * player.getInventory().getSize());
  255. if (item.getTypeId() == 0 || amount < 1)
  256. {
  257. throw new SignException(Util.i18n("moreThanZero"));
  258. }
  259. item.setAmount(amount);
  260. return new Trade(item, ess);
  261. }
  262. protected final void validateInteger(final ISign sign, final int index) throws SignException
  263. {
  264. final String line = sign.getLine(index).trim();
  265. if (line.isEmpty())
  266. {
  267. throw new SignException("Empty line " + index);
  268. }
  269. final int quantity = getIntegerPositive(line);
  270. sign.setLine(index, Integer.toString(quantity));
  271. }
  272. protected final int getIntegerPositive(final String line) throws SignException
  273. {
  274. final int quantity = getInteger(line);
  275. if (quantity < 1)
  276. {
  277. throw new SignException(Util.i18n("moreThanZero"));
  278. }
  279. return quantity;
  280. }
  281. protected final int getInteger(final String line) throws SignException
  282. {
  283. try
  284. {
  285. final int quantity = Integer.parseInt(line);
  286. return quantity;
  287. }
  288. catch (NumberFormatException ex)
  289. {
  290. throw new SignException("Invalid sign", ex);
  291. }
  292. }
  293. protected final ItemStack getItemStack(final String itemName, final int quantity, final IEssentials ess) throws SignException
  294. {
  295. try
  296. {
  297. final ItemStack item = ess.getItemDb().get(itemName);
  298. item.setAmount(quantity);
  299. return item;
  300. }
  301. catch (Exception ex)
  302. {
  303. throw new SignException(ex.getMessage(), ex);
  304. }
  305. }
  306. protected final Double getMoney(final String line) throws SignException
  307. {
  308. final boolean isMoney = line.matches("^[^0-9-\\.][\\.0-9]+$");
  309. return isMoney ? getDoublePositive(line.substring(1)) : null;
  310. }
  311. protected final Double getDoublePositive(final String line) throws SignException
  312. {
  313. final double quantity = getDouble(line);
  314. if (Math.round(quantity * 100.0) < 1.0)
  315. {
  316. throw new SignException(Util.i18n("moreThanZero"));
  317. }
  318. return quantity;
  319. }
  320. protected final Double getDouble(final String line) throws SignException
  321. {
  322. try
  323. {
  324. return Double.parseDouble(line);
  325. }
  326. catch (NumberFormatException ex)
  327. {
  328. throw new SignException(ex.getMessage(), ex);
  329. }
  330. }
  331. protected final Trade getTrade(final ISign sign, final int index, final IEssentials ess) throws SignException
  332. {
  333. return getTrade(sign, index, 1, ess);
  334. }
  335. protected final Trade getTrade(final ISign sign, final int index, final int decrement, final IEssentials ess) throws SignException
  336. {
  337. final String line = sign.getLine(index).trim();
  338. if (line.isEmpty())
  339. {
  340. return new Trade(signName.toLowerCase() + "sign", ess);
  341. }
  342. final Double money = getMoney(line);
  343. if (money == null)
  344. {
  345. final String[] split = line.split("[ :]+", 2);
  346. if (split.length != 2)
  347. {
  348. throw new SignException(Util.i18n("invalidCharge"));
  349. }
  350. final int quantity = getIntegerPositive(split[0]);
  351. final String item = split[1].toLowerCase();
  352. if (item.equalsIgnoreCase("times"))
  353. {
  354. sign.setLine(index, (quantity - decrement) + " times");
  355. return new Trade(signName.toLowerCase() + "sign", ess);
  356. }
  357. else
  358. {
  359. final ItemStack stack = getItemStack(item, quantity, ess);
  360. sign.setLine(index, quantity + " " + item);
  361. return new Trade(stack, ess);
  362. }
  363. }
  364. else
  365. {
  366. return new Trade(money, ess);
  367. }
  368. }
  369. static class EventSign implements ISign
  370. {
  371. private final transient SignChangeEvent event;
  372. private final transient Block block;
  373. public EventSign(final SignChangeEvent event)
  374. {
  375. this.event = event;
  376. this.block = event.getBlock();
  377. }
  378. public final String getLine(final int index)
  379. {
  380. return event.getLine(index);
  381. }
  382. public final void setLine(final int index, final String text)
  383. {
  384. event.setLine(index, text);
  385. }
  386. public Block getBlock()
  387. {
  388. return block;
  389. }
  390. public void updateSign()
  391. {
  392. return;
  393. }
  394. }
  395. static class BlockSign implements ISign
  396. {
  397. private final transient Sign sign;
  398. private final transient Block block;
  399. public BlockSign(final Block block)
  400. {
  401. this.block = block;
  402. this.sign = (Sign)block.getState();
  403. }
  404. public final String getLine(final int index)
  405. {
  406. return sign.getLine(index);
  407. }
  408. public final void setLine(final int index, final String text)
  409. {
  410. sign.setLine(index, text);
  411. }
  412. public final Block getBlock()
  413. {
  414. return block;
  415. }
  416. public final void updateSign()
  417. {
  418. sign.update();
  419. }
  420. }
  421. public interface ISign
  422. {
  423. String getLine(final int index);
  424. void setLine(final int index, final String text);
  425. public Block getBlock();
  426. void updateSign();
  427. }
  428. }