Skip to content

Commit

Permalink
Merge v3.4
Browse files Browse the repository at this point in the history
Merge v3.4
  • Loading branch information
lokka30 committed Mar 31, 2022
2 parents c8d8dbd + 79bcc63 commit 9b4eda7
Show file tree
Hide file tree
Showing 46 changed files with 963 additions and 1,186 deletions.
28 changes: 28 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>me.lokka30</groupId>
<artifactId>LevelledMobs</artifactId>
<version>3.3.3 b604</version>
<version>3.4.0 b624</version>
<packaging>jar</packaging>

<name>LevelledMobs</name>
Expand Down Expand Up @@ -143,7 +143,7 @@
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.17.1-R0.1-SNAPSHOT</version>
<version>1.18.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand All @@ -161,13 +161,13 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.17.1-R0.1-SNAPSHOT</version>
<version>1.18.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>2.2.1</version>
<version>3.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand All @@ -185,7 +185,7 @@
<dependency>
<groupId>com.github.dmulloy2</groupId>
<artifactId>ProtocolLib</artifactId>
<version>4.7.0</version>
<version>4.8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand All @@ -197,13 +197,13 @@
<dependency>
<groupId>simplepets.brainsynder</groupId>
<artifactId>API</artifactId>
<version>5.0-BUILD-98</version>
<version>5.0-BUILD-146</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
<version>2.9.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
161 changes: 155 additions & 6 deletions src/main/java/me/lokka30/levelledmobs/Companion.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
import me.lokka30.levelledmobs.listeners.PlayerInteractEventListener;
import me.lokka30.levelledmobs.listeners.PlayerJoinListener;
import me.lokka30.levelledmobs.listeners.PlayerPortalEventListener;
import me.lokka30.levelledmobs.managers.ExternalCompatibilityManager;
import me.lokka30.levelledmobs.managers.LevelManager;
import me.lokka30.levelledmobs.managers.PlaceholderApiIntegration;

import me.lokka30.levelledmobs.misc.ChunkKillInfo;
import me.lokka30.levelledmobs.misc.DebugType;
import me.lokka30.levelledmobs.misc.FileLoader;
import me.lokka30.levelledmobs.misc.FileMigrator;
Expand All @@ -45,7 +46,10 @@
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -54,10 +58,13 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.InvalidObjectException;
import java.time.Duration;
import java.time.Instant;
import java.util.Collections;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -88,22 +95,31 @@ public class Companion {
this.spawner_CopyIds = new LinkedList<>();
this.spawner_InfoIds = new LinkedList<>();
this.debugsEnabled = new LinkedList<>();
this.entityDeathInChunkCounter = new HashMap<>();
this.chunkKillNoticationTracker = new HashMap<>();
}

final private WeakHashMap<Player, Instant> recentlyJoinedPlayers;
public HashSet<EntityType> groups_HostileMobs;
public HashSet<EntityType> groups_AquaticMobs;
public HashSet<EntityType> groups_PassiveMobs;
public List<String> updateResult;
private boolean hadRulesLoadError;
public boolean useAdventure;
final private HashMap<Long, Map<EntityType, ChunkKillInfo>> entityDeathInChunkCounter;
final private HashMap<Long, Map<UUID, Instant>> chunkKillNoticationTracker;
final public Map<Player, Location> playerNetherPortals;
final public Map<Player, Location> playerWorldPortals;
final public List<UUID> spawner_CopyIds;
final public List<UUID> spawner_InfoIds;
final public List<DebugType> debugsEnabled;
final private PluginManager pluginManager = Bukkit.getPluginManager();
final private MetricsInfo metricsInfo;
private BukkitTask hashMapCleanUp;
final static private Object playerLogonTimes_Lock = new Object();
final static private Object playerNetherPortals_Lock = new Object();
final static private Object entityDeathInChunkCounter_Lock = new Object();
final static private Object entityDeathInChunkNotifier_Lock = new Object();

//Checks if the server version is supported
public void checkCompatibility() {
Expand All @@ -118,8 +134,18 @@ public void checkCompatibility() {
"Compatible MC versions: &b" + String.join("&7,&b ", Utils.getSupportedServerVersions()) + "&7.");
}

if (!ExternalCompatibilityManager.hasProtocolLibInstalled()) {
incompatibilities.add("Your server does not have &bProtocolLib&7 installed! This means that no levelled nametags will appear on the mobs. If you wish to see custom nametags above levelled mobs, then you must install ProtocolLib.");
final Plugin protocolLibPlugin = Bukkit.getPluginManager().getPlugin("ProtocolLib");

if (protocolLibPlugin == null) {
incompatibilities.add("Your server does not have ProtocolLib installed! This means that you will" +
"not be able to see any custom nametags/labels above any levelled mobs' heads. To fix this, " +
"install the latest compatible version of ProtocolLib for your server.");
} else {
if(VersionUtils.isOneEighteen() && protocolLibPlugin.getDescription().getVersion().equals("4.7.0")) {
incompatibilities.add("You are running an outdated version of ProtocolLib! This version of " +
"ProtocolLib does not support 1.18+ servers, so you will receive lots of errors if " +
"you try to use it. Update to the latest ProtocolLib version as soon as possible.");
}
}

main.incompatibilitiesAmount = incompatibilities.size();
Expand All @@ -131,6 +157,10 @@ public void checkCompatibility() {
}
}

public boolean getHadRulesLoadError(){
return this.hadRulesLoadError;
}

private int getSettingsVersion(){
final File file = new File(main.getDataFolder(), "settings.yml");
if (!file.exists()) return 0;
Expand All @@ -146,7 +176,9 @@ boolean loadFiles(final boolean isReload) {
// save license.txt
FileLoader.saveResourceIfNotExists(main, new File(main.getDataFolder(), "license.txt"));

main.rulesParsingManager.parseRulesMain(FileLoader.loadFile(main, "rules", FileLoader.RULES_FILE_VERSION));
final YamlConfiguration rulesFile = FileLoader.loadFile(main, "rules", FileLoader.RULES_FILE_VERSION);
this.hadRulesLoadError = rulesFile == null;
main.rulesParsingManager.parseRulesMain(rulesFile);

main.configUtils.playerLevellingEnabled = main.rulesManager.isPlayerLevellingEnabled();

Expand Down Expand Up @@ -192,6 +224,7 @@ boolean loadFiles(final boolean isReload) {

main.configUtils.load();
main.playerLevellingMinRelevelTime = main.helperSettings.getInt(main.settingsCfg, "player-levelling-relevel-min-time", 5000);
this.useAdventure = main.helperSettings.getBoolean(main.settingsCfg, "use-adventure", true);

return true;
}
Expand Down Expand Up @@ -307,6 +340,121 @@ void setupMetrics() {
metrics.addCustomChart(new SimpleBarChart("enabled-compatibility", metricsInfo::enabledCompats));
}

void startCleanupTask(){
this.hashMapCleanUp = new BukkitRunnable() {
@Override
public void run() {
synchronized (entityDeathInChunkCounter_Lock) {
chunkKillLimitCleanup();
}
synchronized (entityDeathInChunkNotifier_Lock){
chunkKillNoticationCleanup();
}
}
}.runTaskTimerAsynchronously(main, 100, 40);
}

private void chunkKillLimitCleanup(){
final List<Long> chunkKeysToRemove = new LinkedList<>();

for (final long chunkKey : entityDeathInChunkCounter.keySet()){
// Cooldown time, entity counts
final Map<EntityType, ChunkKillInfo> pairList = entityDeathInChunkCounter.get(chunkKey);

if (pairList == null) continue;
final Instant now = Instant.now();

for (final EntityType entityType : pairList.keySet()) {
final ChunkKillInfo chunkKillInfo = pairList.get(entityType);

chunkKillInfo.getEntrySet().removeIf(
e -> e.getKey().compareTo(now.minusSeconds(e.getValue())) < 0
);
}

pairList.entrySet().removeIf(e -> e.getValue().isEmpty());

if(pairList.isEmpty()){
// Remove the object to prevent iterate over exceed amount of empty pairList
chunkKeysToRemove.add(chunkKey);
}
}

for (final long chunkKey : chunkKeysToRemove)
entityDeathInChunkCounter.remove(chunkKey);
}

private void chunkKillNoticationCleanup(){
final Iterator<Long> iterator = this.chunkKillNoticationTracker.keySet().iterator();

while (iterator.hasNext()){
final long chunkKey = iterator.next();
final Map<UUID, Instant> playerTimestamps = this.chunkKillNoticationTracker.get(chunkKey);
playerTimestamps.entrySet().removeIf(e -> Duration.between(e.getValue(), Instant.now()).toSeconds() > 30L);

if (playerTimestamps.isEmpty()) iterator.remove();
}
}

@NotNull
public Map<EntityType, ChunkKillInfo> getorAddPairForSpecifiedChunk(final long chunkKey){
synchronized (entityDeathInChunkCounter_Lock){
return this.entityDeathInChunkCounter.computeIfAbsent(chunkKey, k -> new HashMap<>());
}
}

@NotNull
public List<Map<EntityType, ChunkKillInfo>> getorAddPairForSpecifiedChunks(final @NotNull List<Long> chunkKeys){
final List<Map<EntityType, ChunkKillInfo>> results = new ArrayList<>(chunkKeys.size());

synchronized (entityDeathInChunkCounter_Lock){
for (final long chunkKey : chunkKeys)
results.add(this.entityDeathInChunkCounter.computeIfAbsent(chunkKey, k -> new HashMap<>()));
}

return results;
}

public boolean doesUserHaveCooldown(final @NotNull List<Long> chunkKeys, final @NotNull UUID userId){
final List<Map<UUID, Instant>> chunkInfos = new LinkedList<>();

synchronized (entityDeathInChunkNotifier_Lock){
for (final long chunkKey : chunkKeys) {
if (this.chunkKillNoticationTracker.containsKey(chunkKey))
chunkInfos.add(this.chunkKillNoticationTracker.get(chunkKey));
}
}

if (chunkInfos.isEmpty()) return false;

for (final Map<UUID, Instant> chunkInfo : chunkInfos){
if (chunkInfo == null || !chunkInfo.containsKey(userId)) continue;
final Instant instant = chunkInfo.get(userId);
if (Duration.between(instant, Instant.now()).toSeconds() <= 30L)
return true;
}

return false;
}

public void addUserCooldown(final @NotNull List<Long> chunkKeys, final @NotNull UUID userId){
synchronized (entityDeathInChunkNotifier_Lock){
for (final long chunkKey : chunkKeys){
final Map<UUID, Instant> entry = this.chunkKillNoticationTracker.computeIfAbsent(chunkKey, k -> new HashMap<>());
entry.put(userId, Instant.now());
}
}
}

public void clearChunkKillCache(){
synchronized (entityDeathInChunkCounter_Lock){
this.entityDeathInChunkCounter.clear();
}
synchronized (entityDeathInChunkNotifier_Lock){
this.chunkKillNoticationTracker.clear();
}
}

//Check for updates on the Spigot page.
void checkUpdates() {
if (main.helperSettings.getBoolean(main.settingsCfg,"use-update-checker", true)) {
Expand Down Expand Up @@ -334,7 +482,7 @@ void checkUpdates() {
}

if (isNewerVersion) {
updateResult = Collections.singletonList(
updateResult = List.of(
"&7Your &bLevelledMobs&7 version is &ba pre-release&7. Latest release version is &bv%latestVersion%&7. &8(&7You're running &bv%currentVersion%&8)");

updateResult = Utils.replaceAllInList(updateResult, "%currentVersion%", currentVersion);
Expand Down Expand Up @@ -385,6 +533,7 @@ void shutDownAsyncTasks() {
Utils.logger.info("&fTasks: &7Shutting down other async tasks...");
main._mobsQueueManager.stop();
main.nametagQueueManager_.stop();
if (hashMapCleanUp != null) hashMapCleanUp.cancel();
Bukkit.getScheduler().cancelTasks(main);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/me/lokka30/levelledmobs/LevelledMobs.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public void onEnable() {
levelManager.startNametagAutoUpdateTask();
levelManager.startNametagTimer();
}
companion.startCleanupTask();
companion.setupMetrics();
companion.checkUpdates();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public interface LivingEntityInterface {

void setSpawnedTimeOfDay(final int ticks);

Integer getSummonedLevel();

boolean isWasSummoned();

void clearEntityData();

void free();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ public List<String> onTabComplete(final @NotNull CommandSender sender, final @No
return summonSubcommand.parseTabCompletions(main, sender, args);
case "egg":
return spawnerEggCommand.parseTabCompletions(main, sender, args);
case "debug":
return debugSubcommand.parseTabCompletions(main, sender, args);
// the missing subcommands don't have tab completions, don't bother including them.
default:
return Collections.emptyList();
Expand Down
Loading

0 comments on commit 9b4eda7

Please sign in to comment.