DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
DreamFakeBlock.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.DreamFakeBlock;
25
26import lombok.Getter;
27import org.bukkit.Location;
28import org.bukkit.Material;
29import org.bukkit.entity.Player;
30
31import java.util.HashSet;
32import java.util.Set;
33
45public class DreamFakeBlock {
46 @Getter private final Location location;
47 private Material material;
48 private final Set<Player> observers = new HashSet<>();
49
51 public Material getMaterial(){ return material; }
52
60 public DreamFakeBlock(Location location, Material material) {
61 if (location == null || material == null) throw new IllegalArgumentException("Location and material cannot be null");
62 this.location = location;
63 this.material = material;
64 }
65
71 public void addObserver(Player player) {
72 if (player == null) throw new IllegalArgumentException("Player cannot be null");
73 player.sendBlockChange(location, material.createBlockData());
74 observers.add(player);
75 new FakeBlockObserverAddedEvent(this, player);
76 }
77
83 public void removeObserver(Player player) {
84 if (player == null) throw new IllegalArgumentException("Player cannot be null");
85 player.sendBlockChange(location, location.getBlock().getType().createBlockData());
86 observers.remove(player);
87 new FakeBlockObserverRemovedEvent(this, player);
88 }
89
93 public void removeAllObservers() {
94 for (Player player : new HashSet<>(observers)) {
95 removeObserver(player);
96 }
97 observers.clear();
98 new FakeBlockClearedEvent(this);
99 }
100
108 public boolean isPlayerObservingAtLocation(Player player, Location location) {
109 if (player == null || location == null) throw new IllegalArgumentException("Player and location cannot be null");
110 return observers.contains(player) && this.location.equals(location);
111 }
112
119 public boolean isLocation(Location location) {
120 return this.location.equals(location);
121 }
122
128 public void updateMaterialForAllObservers(Material newMaterial) {
129 if (newMaterial == null) throw new IllegalArgumentException("Material cannot be null");
130 this.material = newMaterial;
131 for (Player player : observers) {
132 player.sendBlockChange(location, newMaterial.createBlockData());
133 }
134 new FakeBlockUpdatedEvent(this, newMaterial);
135 }
136
140 public void displayNextFrame() {
141 for (Player player : observers) {
142 player.sendBlockChange(location, material.createBlockData());
143 }
144 }
145
147 public int getObserverCount() {
148 return observers.size();
149 }
150}
Represents a single fake block at a given location for one or more observers.
void addObserver(Player player)
Adds a player to the observer list and sends them the fake block.
boolean isLocation(Location location)
Checks if a given location matches this fake block’s location.
Material getMaterial()
The current fake block material.
void removeAllObservers()
Removes all observers, restoring the world block to them.
DreamFakeBlock(Location location, Material material)
Creates a new fake block at a location with a specific material.
void updateMaterialForAllObservers(Material newMaterial)
Updates the material of the fake block for all observers.
int getObserverCount()
The number of players observing this fake block.
void removeObserver(Player player)
Removes a player from observers and restores the real block to them.
boolean isPlayerObservingAtLocation(Player player, Location location)
Checks if a player is observing this block at a given location.
void displayNextFrame()
Re-sends the current block state to all observers (e.g.
Fired after all observers are removed from a DreamFakeBlock.
Fired when a player is added as an observer to a DreamFakeBlock.
Fired when a DreamFakeBlock's material is updated and pushed to all observers.