Skip to content

Commit f22970e

Browse files
committed
Move SpawnerAPI to its own class and just make the constructor package-private instead of having it as an inner-class inside of SpawnerPlugin. Achieves the same purpose of preventing construction of new SpawnerAPI instances and is a better approach than having an inner class
1 parent d9c41b7 commit f22970e

File tree

2 files changed

+90
-79
lines changed

2 files changed

+90
-79
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.github.mlefeb01.spawners;
2+
3+
import com.github.mlefeb01.spawners.config.ConfigYml;
4+
import com.github.mlefeb01.spawners.util.Constants;
5+
import com.github.mlefeb01.spigotutils.api.utils.TextUtils;
6+
import de.tr7zw.changeme.nbtapi.NBTItem;
7+
import org.bukkit.Location;
8+
import org.bukkit.Material;
9+
import org.bukkit.block.Block;
10+
import org.bukkit.entity.EntityType;
11+
import org.bukkit.inventory.ItemStack;
12+
import org.bukkit.inventory.meta.ItemMeta;
13+
14+
import java.util.Date;
15+
import java.util.Map;
16+
import java.util.stream.Collectors;
17+
18+
public final class SpawnerAPI {
19+
private final ConfigYml configYml;
20+
private final Map<Location, Long> spawnerLifetime;
21+
22+
SpawnerAPI(ConfigYml configYml, Map<Location, Long> spawnerLifetime) {
23+
this.configYml = configYml;
24+
this.spawnerLifetime = spawnerLifetime;
25+
}
26+
27+
public long getSpawnerLifetime(Location location) {
28+
return getSpawnerLifetime(location.getBlock());
29+
}
30+
31+
public long getSpawnerLifetime(Block block) {
32+
// Check if the lifetime feature is enabled
33+
if (!configYml.isExpireEnabled()) {
34+
return -1;
35+
}
36+
37+
// Check for blocks that are not a spawner
38+
final Long time = spawnerLifetime.get(block.getLocation());
39+
if (block.getType() != Material.MOB_SPAWNER) {
40+
// Remove the location from the cache if for some reason the block is no longer a spawner (e.g. - worldedit)
41+
if (time != null) {
42+
spawnerLifetime.remove(block.getLocation());
43+
}
44+
return -1;
45+
}
46+
47+
// By now it is known that the block is a spawner, so if it does not have a lifetime cached give it one (e.g. - worldedit)
48+
if (time == null) {
49+
spawnerLifetime.put(block.getLocation(), System.currentTimeMillis());
50+
return 0;
51+
} else {
52+
return System.currentTimeMillis() - time;
53+
}
54+
}
55+
56+
private long calculateExpireTime(long startTime) {
57+
final long expireTime = configYml.isExpireEnabled() ? startTime + (configYml.getExpireTimeLimit() * 1000) : -1;
58+
if (expireTime != -1 && configYml.isRoundNearestHour()) {
59+
return expireTime - (expireTime % 3600000);
60+
}
61+
return expireTime;
62+
}
63+
64+
public ItemStack createSpawner(EntityType spawned, long expireStartTime) {
65+
final ItemStack spawner = new ItemStack(Material.MOB_SPAWNER, 1);
66+
final ItemMeta meta = spawner.getItemMeta();
67+
68+
final long expireTime = calculateExpireTime(expireStartTime);
69+
70+
meta.setDisplayName(configYml.getSpawnerItemName().replace("%type%", TextUtils.formatEnumAsString(spawned)));
71+
final String expirePlaceholder = configYml.getExpireTimeFormat().format(new Date(expireTime));
72+
meta.setLore(configYml.getSpawnerItemLore().stream().map(str -> str.replace("%time%", expireTime == -1 ? "N/A" : expirePlaceholder)).collect(Collectors.toList()));
73+
spawner.setItemMeta(meta);
74+
75+
final NBTItem finalSpawner = new NBTItem(spawner);
76+
finalSpawner.setString(Constants.NBT_SPAWNER_TYPE, spawned.name());
77+
finalSpawner.setLong(Constants.NBT_SPAWNER_EXPIRE, expireTime);
78+
return finalSpawner.getItem();
79+
}
80+
81+
public ItemStack createSpawner(EntityType spawned) {
82+
return createSpawner(spawned, System.currentTimeMillis());
83+
}
84+
85+
public boolean isCustomSpawner(ItemStack item) {
86+
return item != null && new NBTItem(item).hasKey(Constants.NBT_SPAWNER_TYPE);
87+
}
88+
89+
}

src/main/java/com/github/mlefeb01/spawners/SpawnerPlugin.java

Lines changed: 1 addition & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,17 @@
33
import com.github.mlefeb01.spawners.command.SpawnerCommand;
44
import com.github.mlefeb01.spawners.config.ConfigYml;
55
import com.github.mlefeb01.spawners.listener.SpawnerListener;
6-
import com.github.mlefeb01.spawners.util.Constants;
76
import com.github.mlefeb01.spigotutils.api.adapters.LocationAdapter;
87
import com.github.mlefeb01.spigotutils.api.utils.FileUtils;
9-
import com.github.mlefeb01.spigotutils.api.utils.TextUtils;
108
import com.google.common.reflect.TypeToken;
119
import com.google.gson.Gson;
1210
import com.google.gson.GsonBuilder;
13-
import de.tr7zw.changeme.nbtapi.NBTItem;
1411
import net.milkbowl.vault.economy.Economy;
1512
import org.bukkit.Location;
16-
import org.bukkit.Material;
17-
import org.bukkit.block.Block;
18-
import org.bukkit.entity.EntityType;
19-
import org.bukkit.inventory.ItemStack;
20-
import org.bukkit.inventory.meta.ItemMeta;
2113
import org.bukkit.plugin.java.JavaPlugin;
2214

23-
import java.util.Date;
2415
import java.util.HashMap;
2516
import java.util.Map;
26-
import java.util.stream.Collectors;
2717

2818
public final class SpawnerPlugin extends JavaPlugin {
2919
private final Gson gson = new GsonBuilder().registerTypeAdapter(Location.class, new LocationAdapter()).enableComplexMapKeySerialization().setPrettyPrinting().create();
@@ -43,7 +33,7 @@ public void onEnable() {
4333

4434
getCommand("spawner").setExecutor(new SpawnerCommand(configYml));
4535

46-
spawnerAPI = new SpawnerAPI();
36+
spawnerAPI = new SpawnerAPI(configYml, spawnerLifetime);
4737

4838
getLogger().info("SpawnerPlugin has been enabled!");
4939
}
@@ -59,72 +49,4 @@ public static SpawnerAPI getSpawnerAPI() {
5949
return spawnerAPI;
6050
}
6151

62-
public class SpawnerAPI {
63-
64-
private SpawnerAPI() {}
65-
66-
public long getSpawnerLifetime(Location location) {
67-
return getSpawnerLifetime(location.getBlock());
68-
}
69-
70-
public long getSpawnerLifetime(Block block) {
71-
// Check if the lifetime feature is enabled
72-
if (!configYml.isExpireEnabled()) {
73-
return -1;
74-
}
75-
76-
// Check for blocks that are not a spawner
77-
final Long time = spawnerLifetime.get(block.getLocation());
78-
if (block.getType() != Material.MOB_SPAWNER) {
79-
// Remove the location from the cache if for some reason the block is no longer a spawner (e.g. - worldedit)
80-
if (time != null) {
81-
spawnerLifetime.remove(block.getLocation());
82-
}
83-
return -1;
84-
}
85-
86-
// By now it is known that the block is a spawner, so if it does not have a lifetime cached give it one (e.g. - worldedit)
87-
if (time == null) {
88-
spawnerLifetime.put(block.getLocation(), System.currentTimeMillis());
89-
return 0;
90-
} else {
91-
return System.currentTimeMillis() - time;
92-
}
93-
}
94-
95-
private long calculateExpireTime(long startTime) {
96-
final long expireTime = configYml.isExpireEnabled() ? startTime + (configYml.getExpireTimeLimit() * 1000) : -1;
97-
if (expireTime != -1 && configYml.isRoundNearestHour()) {
98-
return expireTime - (expireTime % 3600000);
99-
}
100-
return expireTime;
101-
}
102-
103-
public ItemStack createSpawner(EntityType spawned, long expireStartTime) {
104-
final ItemStack spawner = new ItemStack(Material.MOB_SPAWNER, 1);
105-
final ItemMeta meta = spawner.getItemMeta();
106-
107-
final long expireTime = calculateExpireTime(expireStartTime);
108-
109-
meta.setDisplayName(configYml.getSpawnerItemName().replace("%type%", TextUtils.formatEnumAsString(spawned)));
110-
final String expirePlaceholder = configYml.getExpireTimeFormat().format(new Date(expireTime));
111-
meta.setLore(configYml.getSpawnerItemLore().stream().map(str -> str.replace("%time%", expireTime == -1 ? "N/A" : expirePlaceholder)).collect(Collectors.toList()));
112-
spawner.setItemMeta(meta);
113-
114-
final NBTItem finalSpawner = new NBTItem(spawner);
115-
finalSpawner.setString(Constants.NBT_SPAWNER_TYPE, spawned.name());
116-
finalSpawner.setLong(Constants.NBT_SPAWNER_EXPIRE, expireTime);
117-
return finalSpawner.getItem();
118-
}
119-
120-
public ItemStack createSpawner(EntityType spawned) {
121-
return createSpawner(spawned, System.currentTimeMillis());
122-
}
123-
124-
public boolean isCustomSpawner(ItemStack item) {
125-
return item != null && new NBTItem(item).hasKey(Constants.NBT_SPAWNER_TYPE);
126-
}
127-
128-
}
129-
13052
}

0 commit comments

Comments
 (0)