@@ -21,11 +21,11 @@ public class MagicTree {
2121 private final UUID owner ;
2222 private final Location location ;
2323 private final UUID treeuid ;
24- private final Set <Block > presents = new HashSet <>();
2524 TreeLevel level ;
2625 private Map <Material , Integer > levelupRequirements ;
2726 private Set <Block > blocks ;
28- private long presentCounter = 0 ;
27+ private long presentCounter ;
28+ private int scheduledPresents ;
2929
3030 public MagicTree (UUID owner , TreeLevel level , Location location ) {
3131 this .treeuid = UUID .randomUUID ();
@@ -35,16 +35,22 @@ public MagicTree(UUID owner, TreeLevel level, Location location) {
3535 this .levelupRequirements = new HashMap <>(level .getLevelupRequirements ());
3636 if (Main .inProgress )
3737 build ();
38+ presentCounter = 0 ;
39+ scheduledPresents = 0 ;
3840 }
3941
40- public MagicTree (UUID owner , UUID uid , TreeLevel level , Location location , Map <Material , Integer > levelupRequirements ) {
42+ public MagicTree (UUID owner , UUID uid , TreeLevel level , Location location , Map <Material , Integer > levelupRequirements ,
43+ long presentCounter , int scheduledPresents ) {
4144 this .owner = owner ;
4245 this .treeuid = uid ;
4346 this .level = level ;
4447 this .location = location ;
4548 this .levelupRequirements = new HashMap <>(levelupRequirements );
49+ this .presentCounter = 0 ;
50+ this .presentCounter = presentCounter ;
4651 if (Main .inProgress )
4752 build ();
53+ this .scheduledPresents = scheduledPresents ;
4854 }
4955
5056 public static MagicTree getTreeByBlock (Block block ) {
@@ -186,29 +192,35 @@ public void build() {
186192
187193 @ SuppressWarnings ("deprecation" )
188194 public void spawnPresent () {
195+ if (!location .getWorld ().isChunkLoaded ((int )location .getX () / 16 , (int )location .getZ () / 16 ))
196+ {
197+ if (scheduledPresents + 1 <= 8 )
198+ scheduledPresents ++;
199+ return ;
200+ }
201+
189202 Location presentLoc = location .clone ().add (-1 + Main .RANDOM .nextInt (3 ), 0 , -1 + Main .RANDOM .nextInt (3 ));
190203
191204 Block pBlock = presentLoc .getBlock ();
192- if (presents .size () <= 3 ) {
193- if (!pBlock .getType ().isSolid () && pBlock .getType () != Material .SPRUCE_SAPLING ) {
194- pBlock .setType (Material .PLAYER_HEAD );
195- BlockState state = pBlock .getState ();
196- if (state instanceof Skull ) {
197- Skull skull = (Skull ) state ;
198- BlockFace face ;
199- do {
200- face = BlockFace .values ()[Main .RANDOM .nextInt (BlockFace .values ().length )];
201- }
202- while (face == BlockFace .DOWN || face == BlockFace .UP || face == BlockFace .SELF );
203- //skull.setRotation(face);
204- Rotatable skullRotatable = (Rotatable ) skull .getBlockData ();
205- skullRotatable .setRotation (face );
206- //skull.setSkullType(SkullType.PLAYER);
207- skull .setType (Material .PLAYER_HEAD );
208- //skull.setOwner();
209- skull .setOwningPlayer (Bukkit .getOfflinePlayer (Main .getHeads ().get (Main .RANDOM .nextInt (Main .getHeads ().size ()))));
210- skull .update (true );
205+ if (!pBlock .getType ().isSolid () && pBlock .getType () != Material .SPRUCE_SAPLING )
206+ {
207+ pBlock .setType (Material .PLAYER_HEAD );
208+ BlockState state = pBlock .getState ();
209+ if (state instanceof Skull ) {
210+ Skull skull = (Skull ) state ;
211+ BlockFace face ;
212+ do {
213+ face = BlockFace .values ()[Main .RANDOM .nextInt (BlockFace .values ().length )];
211214 }
215+ while (face == BlockFace .DOWN || face == BlockFace .UP || face == BlockFace .SELF );
216+ //skull.setRotation(face);
217+ Rotatable skullRotatable = (Rotatable ) skull .getBlockData ();
218+ skullRotatable .setRotation (face );
219+ //skull.setSkullType(SkullType.PLAYER);
220+ skull .setType (Material .PLAYER_HEAD );
221+ //skull.setOwner();
222+ skull .setOwningPlayer (Bukkit .getOfflinePlayer (Main .getHeads ().get (Main .RANDOM .nextInt (Main .getHeads ().size ()))));
223+ skull .update (true );
212224 }
213225 }
214226 }
@@ -279,4 +291,22 @@ public void end() {
279291 }
280292 XMas .removeTree (this );
281293 }
294+
295+ public long getPresentCounter () {
296+ return presentCounter ;
297+ }
298+
299+ public int getScheduledPresents () {
300+ return scheduledPresents ;
301+ }
302+
303+ public boolean hasScheduledPresents () {
304+ return scheduledPresents > 0 ;
305+ }
306+
307+ public void spawnScheduledPresents () {
308+ for (int i = scheduledPresents ; i > 0 ; i --)
309+ spawnPresent ();
310+ scheduledPresents = 0 ;
311+ }
282312}
0 commit comments