Skip to content

Commit 92ffc89

Browse files
authored
Merge pull request MelonCode#12 from LoneDev6/master
Optimizations
2 parents e82dc9b + 6e8db9f commit 92ffc89

6 files changed

Lines changed: 117 additions & 23 deletions

File tree

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@
8181
<artifactId>commons-lang3</artifactId>
8282
<version>3.0</version>
8383
</dependency>
84+
<dependency>
85+
<groupId>org.jetbrains</groupId>
86+
<artifactId>annotations</artifactId>
87+
<version>17.0.0</version>
88+
<scope>compile</scope>
89+
</dependency>
8490
</dependencies>
8591

8692
</project>

src/main/java/ru/meloncode/xmas/Events.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
import org.bukkit.event.entity.EntityExplodeEvent;
1414
import org.bukkit.event.entity.ItemSpawnEvent;
1515
import org.bukkit.event.player.PlayerInteractEvent;
16+
import org.bukkit.event.world.ChunkLoadEvent;
1617
import org.bukkit.event.world.StructureGrowEvent;
1718
import org.bukkit.inventory.EquipmentSlot;
1819
import org.bukkit.inventory.ItemStack;
1920
import org.bukkit.inventory.meta.ItemMeta;
2021
import org.bukkit.inventory.meta.SkullMeta;
22+
import org.jetbrains.annotations.Nullable;
2123
import ru.meloncode.xmas.utils.TextUtils;
2224

25+
import java.util.Collection;
2326
import java.util.HashMap;
2427
import java.util.Map;
2528
import java.util.UUID;
@@ -293,4 +296,17 @@ private void disableFireworkDamage(EntityDamageByEntityEvent e)
293296
}
294297
}
295298
}
299+
300+
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
301+
private void chunkLoad(ChunkLoadEvent e)
302+
{
303+
Collection<MagicTree> trees = XMas.getAllTreesInChunk(e.getChunk());
304+
if(trees == null)
305+
return;
306+
for(MagicTree tree : trees)
307+
{
308+
if(tree.hasScheduledPresents())
309+
tree.spawnScheduledPresents();
310+
}
311+
}
296312
}

src/main/java/ru/meloncode/xmas/MagicTree.java

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/main/java/ru/meloncode/xmas/TreeSerializer.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public static void loadTrees(JavaPlugin plugin, World world) {
2626
TreeLevel level;
2727
int x, y, z;
2828
Location loc;
29+
long presentCounter;
30+
int scheduledPresents;
2931
if (trees.getConfigurationSection("trees") != null && trees.getConfigurationSection("trees").getKeys(false).size() > 0) {
3032

3133
for (String cKey : trees.getConfigurationSection("trees").getKeys(false)) {
@@ -44,7 +46,10 @@ public static void loadTrees(JavaPlugin plugin, World world) {
4446
} else {
4547
requirements = new HashMap<>();
4648
}
47-
XMas.addMagicTree(new MagicTree(owner, treeUID, level, loc, requirements));
49+
presentCounter = trees.getLong("trees." + cKey + ".present_counter", 0);
50+
scheduledPresents = trees.getInt("trees." + cKey + ".scheduled_presents", 0);
51+
52+
XMas.addMagicTree(new MagicTree(owner, treeUID, level, loc, requirements, presentCounter, scheduledPresents));
4853
} catch (Exception e) {
4954
plugin.getLogger().severe(String.format("Error while loading tree `%s`", cKey));
5055
e.printStackTrace();
@@ -76,6 +81,8 @@ public static void saveTree(MagicTree tree) {
7681
} catch (IOException e) {
7782
e.printStackTrace();
7883
}
84+
trees.set("trees." + cKey + ".present_counter", tree.getPresentCounter());
85+
trees.set("trees." + cKey + ".scheduled_presents", tree.getScheduledPresents());
7986
}
8087

8188
public static void removeTree(MagicTree tree) {

src/main/java/ru/meloncode/xmas/XMas.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package ru.meloncode.xmas;
22

3+
import org.bukkit.Chunk;
34
import org.bukkit.Location;
45
import org.bukkit.Material;
56
import org.bukkit.World;
67
import org.bukkit.block.Block;
78
import org.bukkit.block.Skull;
89
import org.bukkit.entity.Player;
910
import org.bukkit.inventory.ItemStack;
11+
import org.jetbrains.annotations.Nullable;
12+
import ru.meloncode.xmas.utils.LocationUtils;
1013
import ru.meloncode.xmas.utils.TextUtils;
1114

1215
import java.util.ArrayList;
@@ -20,11 +23,13 @@
2023
class XMas {
2124

2225
private static final ConcurrentHashMap<UUID, MagicTree> trees = new ConcurrentHashMap<>();
26+
private static final ConcurrentHashMap<Long, List<MagicTree>> trees_byChunk = new ConcurrentHashMap<>();
2327
public static ItemStack XMAS_CRYSTAL;
2428

2529
public static void createMagicTree(Player player, Location loc) {
2630
MagicTree tree = new MagicTree(player.getUniqueId(), TreeLevel.SAPLING, loc);
2731
trees.put(tree.getTreeUID(), tree);
32+
trees_byChunk.computeIfAbsent(LocationUtils.getChunkKey(tree.getLocation()), aLong -> new ArrayList<>()).add(tree);
2833
tree.save();
2934
}
3035

@@ -37,10 +42,16 @@ public static Collection<MagicTree> getAllTrees() {
3742
return trees.values();
3843
}
3944

45+
@Nullable
46+
public static Collection<MagicTree> getAllTreesInChunk(Chunk chunk) {
47+
return trees_byChunk.get(LocationUtils.getChunkKey(chunk));
48+
}
49+
4050
public static void removeTree(MagicTree tree) {
4151
tree.unbuild();
4252
TreeSerializer.removeTree(tree);
4353
trees.remove(tree.getTreeUID());
54+
trees_byChunk.remove(LocationUtils.getChunkKey(tree.getLocation()));
4455
}
4556

4657
public static void processPresent(Block block, Player player) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package ru.meloncode.xmas.utils;
2+
3+
import org.bukkit.Chunk;
4+
import org.bukkit.Location;
5+
6+
public class LocationUtils {
7+
public static long getChunkKey(Chunk chunk) {
8+
return getChunkKey(chunk.getX(), chunk.getZ());
9+
}
10+
public static long getChunkKey(Location location) {
11+
return getChunkKey(location.getBlockX(), location.getBlockZ());
12+
}
13+
14+
public static long getChunkKey(int x, int z) {
15+
x = floor(x) >> 4;
16+
z = floor(z) >> 4;
17+
return (((long) x) << 32) | (z & 0xFFFFFFFFL);
18+
}
19+
20+
public static int floor(double num) {
21+
int floor = (int) num;
22+
return floor == num ? floor : floor - (int) (Double.doubleToRawLongBits(num) >>> 63);
23+
}
24+
}

0 commit comments

Comments
 (0)