DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
DreamBlockDisplay.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.*;
27import org.bukkit.block.data.BlockData;
28import org.bukkit.entity.BlockDisplay;
29import org.bukkit.entity.Display;
30import org.bukkit.util.Transformation;
31import org.joml.Vector3f;
32
33import java.util.function.Consumer;
34
54public final class DreamBlockDisplay {
55
59 public static class BlockDisplayBuilder {
60 private World world;
61 private Location location;
62 private double itemScale = 1.0;
63 private Vector3f leftRotation = new Vector3f(0f, 0f, 0f);
64 private float viewRange = 0.1f;
65 private float shadowRadius = 0.3f;
66 private float shadowStrength = 5f;
67 private float displayWidth = 50f;
68 private float displayHeight = 50f;
69 private Display.Billboard billboard = Display.Billboard.CENTER;
70 private Color itemGlowColor = Color.RED;
71 private Display.Brightness itemBrightness = new Display.Brightness(15, 15);
72 private Consumer<Transformation> customTransformation;
73
79 this.world = Bukkit.getWorld("world");
80 if (this.world == null) throw new IllegalStateException("Default world 'world' is not available.");
81 this.location = this.world.getSpawnLocation();
82 }
83
87 public BlockDisplayBuilder world(World world) {
88 if (world == null) throw new IllegalArgumentException("World cannot be null");
89 this.world = world;
90 return this;
91 }
92
96 public BlockDisplayBuilder location(Location location) {
97 if (location == null) throw new IllegalArgumentException("Location cannot be null");
98 this.location = location;
99 return this;
100 }
101
105 public BlockDisplayBuilder itemScale(double itemScale) {
106 if (itemScale <= 0) throw new IllegalArgumentException("Item scale must be greater than zero");
107 this.itemScale = itemScale;
108 return this;
109 }
110
114 public BlockDisplayBuilder leftRotation(Vector3f leftRotation) {
115 if (leftRotation == null) throw new IllegalArgumentException("Rotation vector cannot be null");
116 this.leftRotation = leftRotation;
117 return this;
118 }
119
123 public BlockDisplayBuilder customTransformation(Consumer<Transformation> customTransformation) {
124 if (customTransformation == null) throw new IllegalArgumentException("Custom transformation cannot be null");
125 this.customTransformation = customTransformation;
126 return this;
127 }
128
132 public BlockDisplayBuilder viewRange(float viewRange) {
133 if (viewRange < 0) throw new IllegalArgumentException("View range must be non-negative");
134 this.viewRange = viewRange;
135 return this;
136 }
137
141 public BlockDisplayBuilder shadowRadius(float shadowRadius) {
142 if (shadowRadius < 0) throw new IllegalArgumentException("Shadow radius must be non-negative");
143 this.shadowRadius = shadowRadius;
144 return this;
145 }
146
150 public BlockDisplayBuilder shadowStrength(float shadowStrength) {
151 if (shadowStrength < 0) throw new IllegalArgumentException("Shadow strength must be non-negative");
152 this.shadowStrength = shadowStrength;
153 return this;
154 }
155
159 public BlockDisplayBuilder displayHeight(float displayHeight) {
160 if (displayHeight <= 0) throw new IllegalArgumentException("Display height must be greater than zero");
161 this.displayHeight = displayHeight;
162 return this;
163 }
164
168 public BlockDisplayBuilder displayWidth(float displayWidth) {
169 if (displayWidth <= 0) throw new IllegalArgumentException("Display width must be greater than zero");
170 this.displayWidth = displayWidth;
171 return this;
172 }
173
177 public BlockDisplayBuilder billboard(Display.Billboard billboard) {
178 this.billboard = billboard;
179 return this;
180 }
181
185 public BlockDisplayBuilder itemGlowColor(Color itemGlowColor) {
186 if (itemGlowColor == null) throw new IllegalArgumentException("Glow color cannot be null");
187 this.itemGlowColor = itemGlowColor;
188 return this;
189 }
190
194 public BlockDisplayBuilder itemBrightness(Display.Brightness itemBrightness) {
195 if (itemBrightness == null) throw new IllegalArgumentException("Brightness cannot be null");
196 this.itemBrightness = itemBrightness;
197 return this;
198 }
199
203 public BlockDisplay spawn(Material material) {
204 if (material == null) throw new IllegalArgumentException("Material cannot be null");
205 BlockData blockData = Bukkit.createBlockData(material);
206 return spawn(blockData);
207 }
208
212 public BlockDisplay spawn(BlockData blockData) {
213 if (blockData == null) throw new IllegalArgumentException("BlockData cannot be null");
214 BlockDisplay blockDisplay = world.spawn(location, BlockDisplay.class);
215 blockDisplay.setBlock(blockData);
216 return configureBlockDisplay(blockDisplay);
217 }
218
224 private BlockDisplay configureBlockDisplay(BlockDisplay blockDisplay) {
225 Transformation transformation = blockDisplay.getTransformation();
226 transformation.getScale().set((float) itemScale);
227 transformation.getLeftRotation().x = leftRotation.x;
228 transformation.getLeftRotation().y = leftRotation.y;
229 transformation.getLeftRotation().z = leftRotation.z;
230
231 if (customTransformation != null) {
232 customTransformation.accept(transformation);
233 }
234
235 blockDisplay.setTransformation(transformation);
236 blockDisplay.setViewRange(viewRange);
237 blockDisplay.setShadowRadius(shadowRadius);
238 blockDisplay.setShadowStrength(shadowStrength);
239 blockDisplay.setDisplayHeight(displayHeight);
240 blockDisplay.setDisplayWidth(displayWidth);
241 blockDisplay.setBillboard(billboard);
242 blockDisplay.setGlowColorOverride(itemGlowColor);
243 blockDisplay.setBrightness(itemBrightness);
244
245 BlockDisplayCreatedEvent event = new BlockDisplayCreatedEvent(blockDisplay);
246 Bukkit.getPluginManager().callEvent(event);
247 return blockDisplay;
248 }
249 }
250}
Fluent builder for configuring and spawning BlockDisplay entities.
BlockDisplayBuilder itemGlowColor(Color itemGlowColor)
Sets glow color override for the display.
BlockDisplayBuilder displayHeight(float displayHeight)
Sets the logical display height.
BlockDisplayBuilder viewRange(float viewRange)
Sets the render view range.
BlockDisplayBuilder displayWidth(float displayWidth)
Sets the logical display width.
BlockDisplay spawn(Material material)
Spawns a BlockDisplay using a Material.
BlockDisplayBuilder billboard(Display.Billboard billboard)
Sets billboard mode.
BlockDisplayBuilder world(World world)
Sets the world for the display.
BlockDisplayBuilder location(Location location)
Sets the spawn location for the display.
BlockDisplayBuilder shadowRadius(float shadowRadius)
Sets the shadow radius.
BlockDisplayBuilder leftRotation(Vector3f leftRotation)
Sets the left rotation (applied in transformation matrix).
BlockDisplayBuilder itemBrightness(Display.Brightness itemBrightness)
Sets emissive brightness.
BlockDisplayBuilder customTransformation(Consumer< Transformation > customTransformation)
Provides a custom per-spawn transformation mutator.
BlockDisplayBuilder itemScale(double itemScale)
Sets a uniform item scale (applied to X/Y/Z).
BlockDisplayBuilder()
Initializes a builder using the default world "world" and its spawn location.
BlockDisplay spawn(BlockData blockData)
Spawns a BlockDisplay using a BlockData.
BlockDisplayBuilder shadowStrength(float shadowStrength)
Sets the shadow strength.
Utility wrapper for building and spawning BlockDisplay entities with a fluent API.