PageRenderTime 21ms CodeModel.GetById 16ms app.highlight 4ms 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
 20package com.sk89q.worldedit.bukkit.entity;
 21
 22import com.sk89q.worldedit.Location;
 23import com.sk89q.worldedit.bukkit.BukkitUtil;
 24import org.bukkit.Art;
 25import org.bukkit.Bukkit;
 26import org.bukkit.block.BlockFace;
 27import org.bukkit.entity.EntityType;
 28import org.bukkit.entity.Painting;
 29
 30import java.util.ArrayDeque;
 31import java.util.Deque;
 32import java.util.UUID;
 33import java.util.logging.Level;
 34import java.util.logging.Logger;
 35
 36public class BukkitPainting extends BukkitEntity {
 37
 38    private static final Logger log = Logger.getLogger(BukkitPainting.class.getCanonicalName());
 39
 40    private static int spawnTask = -1;
 41    private static final Deque<QueuedPaintingSpawn> spawnQueue = new ArrayDeque<QueuedPaintingSpawn>();
 42
 43    private class QueuedPaintingSpawn {
 44        private final Location weLoc;
 45
 46        private QueuedPaintingSpawn(Location weLoc) {
 47            this.weLoc = weLoc;
 48        }
 49
 50        public void spawn() {
 51            spawnRaw(weLoc);
 52        }
 53    }
 54
 55    private static class PaintingSpawnRunnable implements Runnable {
 56        @Override
 57        public void run() {
 58            synchronized (spawnQueue) {
 59                QueuedPaintingSpawn spawn;
 60                while ((spawn = spawnQueue.poll()) != null) {
 61                    try {
 62                        spawn.spawn();
 63                    } catch (Throwable t) {
 64                        log.log(Level.WARNING, "Failed to spawn painting", t);
 65                        continue;
 66                    }
 67                }
 68                spawnTask = -1;
 69            }
 70        }
 71    }
 72
 73    private final Art art;
 74    private final BlockFace facingDirection;
 75    public BukkitPainting(Location loc, Art art, BlockFace facingDirection, UUID entityId) {
 76        super(loc, EntityType.PAINTING, entityId);
 77        this.art = art;
 78        this.facingDirection = facingDirection;
 79    }
 80
 81    /**
 82     * Queue the painting to be spawned at the specified location.
 83     * This operation is delayed so that the block changes that may be applied can be applied before the painting spawn is attempted.
 84     *
 85     * @param weLoc The WorldEdit location
 86     * @return Whether the spawn as successful
 87     */
 88    @Override
 89    public boolean spawn(Location weLoc) {
 90        synchronized (spawnQueue) {
 91            spawnQueue.add(new QueuedPaintingSpawn(weLoc));
 92            if (spawnTask == -1) {
 93                spawnTask = Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Bukkit.getServer().getPluginManager().getPlugin("WorldEdit"), new PaintingSpawnRunnable(), 1L);
 94            }
 95        }
 96        return true;
 97    }
 98
 99    public boolean spawnRaw(Location weLoc) {
100        org.bukkit.Location loc = BukkitUtil.toLocation(weLoc);
101        Painting paint = loc.getWorld().spawn(loc, Painting.class);
102        if (paint != null) {
103            paint.setFacingDirection(facingDirection, true);
104            paint.setArt(art, true);
105            return true;
106        }
107        return false;
108    }
109
110}