33import com .github .mlefeb01 .spawners .command .SpawnerCommand ;
44import com .github .mlefeb01 .spawners .config .ConfigYml ;
55import com .github .mlefeb01 .spawners .listener .SpawnerListener ;
6- import com .github .mlefeb01 .spawners .util .Constants ;
76import com .github .mlefeb01 .spigotutils .api .adapters .LocationAdapter ;
87import com .github .mlefeb01 .spigotutils .api .utils .FileUtils ;
9- import com .github .mlefeb01 .spigotutils .api .utils .TextUtils ;
108import com .google .common .reflect .TypeToken ;
119import com .google .gson .Gson ;
1210import com .google .gson .GsonBuilder ;
13- import de .tr7zw .changeme .nbtapi .NBTItem ;
1411import net .milkbowl .vault .economy .Economy ;
1512import 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 ;
2113import org .bukkit .plugin .java .JavaPlugin ;
2214
23- import java .util .Date ;
2415import java .util .HashMap ;
2516import java .util .Map ;
26- import java .util .stream .Collectors ;
2717
2818public 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