PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/entity/BukkitPainting.java

https://gitlab.com/Skull3x/WorldEdit
Java | 110 lines | 71 code | 14 blank | 25 comment | 6 complexity | b44ba7bba564c8aac15fa287a0e63ccf MD5 | raw file
  1. /*
  2. * WorldEdit, a Minecraft world manipulation toolkit
  3. * Copyright (C) sk89q <http://www.sk89q.com>
  4. * Copyright (C) WorldEdit team and contributors
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU Lesser General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  14. * for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.sk89q.worldedit.bukkit.entity;
  20. import com.sk89q.worldedit.Location;
  21. import com.sk89q.worldedit.bukkit.BukkitUtil;
  22. import org.bukkit.Art;
  23. import org.bukkit.Bukkit;
  24. import org.bukkit.block.BlockFace;
  25. import org.bukkit.entity.EntityType;
  26. import org.bukkit.entity.Painting;
  27. import java.util.ArrayDeque;
  28. import java.util.Deque;
  29. import java.util.UUID;
  30. import java.util.logging.Level;
  31. import java.util.logging.Logger;
  32. public class BukkitPainting extends BukkitEntity {
  33. private static final Logger log = Logger.getLogger(BukkitPainting.class.getCanonicalName());
  34. private static int spawnTask = -1;
  35. private static final Deque<QueuedPaintingSpawn> spawnQueue = new ArrayDeque<QueuedPaintingSpawn>();
  36. private class QueuedPaintingSpawn {
  37. private final Location weLoc;
  38. private QueuedPaintingSpawn(Location weLoc) {
  39. this.weLoc = weLoc;
  40. }
  41. public void spawn() {
  42. spawnRaw(weLoc);
  43. }
  44. }
  45. private static class PaintingSpawnRunnable implements Runnable {
  46. @Override
  47. public void run() {
  48. synchronized (spawnQueue) {
  49. QueuedPaintingSpawn spawn;
  50. while ((spawn = spawnQueue.poll()) != null) {
  51. try {
  52. spawn.spawn();
  53. } catch (Throwable t) {
  54. log.log(Level.WARNING, "Failed to spawn painting", t);
  55. continue;
  56. }
  57. }
  58. spawnTask = -1;
  59. }
  60. }
  61. }
  62. private final Art art;
  63. private final BlockFace facingDirection;
  64. public BukkitPainting(Location loc, Art art, BlockFace facingDirection, UUID entityId) {
  65. super(loc, EntityType.PAINTING, entityId);
  66. this.art = art;
  67. this.facingDirection = facingDirection;
  68. }
  69. /**
  70. * Queue the painting to be spawned at the specified location.
  71. * This operation is delayed so that the block changes that may be applied can be applied before the painting spawn is attempted.
  72. *
  73. * @param weLoc The WorldEdit location
  74. * @return Whether the spawn as successful
  75. */
  76. @Override
  77. public boolean spawn(Location weLoc) {
  78. synchronized (spawnQueue) {
  79. spawnQueue.add(new QueuedPaintingSpawn(weLoc));
  80. if (spawnTask == -1) {
  81. spawnTask = Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Bukkit.getServer().getPluginManager().getPlugin("WorldEdit"), new PaintingSpawnRunnable(), 1L);
  82. }
  83. }
  84. return true;
  85. }
  86. public boolean spawnRaw(Location weLoc) {
  87. org.bukkit.Location loc = BukkitUtil.toLocation(weLoc);
  88. Painting paint = loc.getWorld().spawn(loc, Painting.class);
  89. if (paint != null) {
  90. paint.setFacingDirection(facingDirection, true);
  91. paint.setArt(art, true);
  92. return true;
  93. }
  94. return false;
  95. }
  96. }