DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
BlockDisplayPool.java
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 Dreamfire Studio
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24package com.dreamfirestudios.dreamcore.DreamBlockDisplay;
25
26import org.bukkit.Bukkit;
27import org.bukkit.Location;
28import org.bukkit.World;
29import org.bukkit.block.data.BlockData;
30import org.bukkit.entity.BlockDisplay;
31
32import java.util.ArrayList;
33import java.util.List;
34
44public final class BlockDisplayPool {
45
49 private static final List<BlockDisplay> pool = new ArrayList<>();
50
54 private static final Location HIDDEN_LOCATION =
55 new Location(Bukkit.getWorlds().isEmpty() ? null : Bukkit.getWorlds().get(0), 0, -1000, 0);
56
65 public static BlockDisplay acquire(World world, Location location, BlockData blockData) {
66 if (world == null || location == null || blockData == null) {
67 throw new IllegalArgumentException("World, location, and blockData cannot be null");
68 }
69
70 BlockDisplay display;
71 if (!pool.isEmpty()) {
72 display = pool.remove(pool.size() - 1);
73 display.teleport(location);
74 display.setBlock(blockData);
75 } else {
76 display = world.spawn(location, BlockDisplay.class, bd -> bd.setBlock(blockData));
77 }
78 return display;
79 }
80
88 public static void release(BlockDisplay display) {
89 if (display == null || !display.isValid()) return;
90
91 display.setBlock(Bukkit.createBlockData("minecraft:air"));
92 if (HIDDEN_LOCATION.getWorld() != null) {
93 display.teleport(HIDDEN_LOCATION);
94 }
95 pool.add(display);
96 }
97
104 public static void clear() {
105 for (BlockDisplay display : pool) {
106 if (display.isValid()) {
107 display.remove();
108 }
109 }
110 pool.clear();
111 }
112}
A simple pool for BlockDisplay entities to reduce the overhead of frequent spawning and removal.
static void release(BlockDisplay display)
Releases a BlockDisplay back into the pool for later reuse.
static BlockDisplay acquire(World world, Location location, BlockData blockData)
Acquires a BlockDisplay from the pool if available, or spawns a new one.
static void clear()
Clears the entire pool and removes all pooled displays from the world.