diff options
Diffstat (limited to 'src')
3 files changed, 46 insertions, 25 deletions
diff --git a/src/main/java/de/cowtipper/cowlection/listener/PlayerListener.java b/src/main/java/de/cowtipper/cowlection/listener/PlayerListener.java index 0507766..d92dd6f 100644 --- a/src/main/java/de/cowtipper/cowlection/listener/PlayerListener.java +++ b/src/main/java/de/cowtipper/cowlection/listener/PlayerListener.java @@ -34,6 +34,7 @@ public class PlayerListener { private final Cowlection main; private static DungeonsListener dungeonsListener; private static SkyBlockListener skyBlockListener; + private boolean isPlayerJoiningServer; private boolean isOnSkyBlock; public PlayerListener(Cowlection main) { @@ -89,13 +90,14 @@ public class PlayerListener { @SubscribeEvent public void onServerJoin(FMLNetworkEvent.ClientConnectedToServerEvent e) { - main.getVersionChecker().runUpdateCheck(false); - new TickDelay(() -> main.getChatHelper().sendOfflineMessages(), 6 * 20); - if (MooConfig.doBestFriendsOnlineCheck && CredentialStorage.isMooValid && main.getFriendsHandler().getBestFriends().size() > 0) { - main.getFriendsHandler().runBestFriendsOnlineCheck(false); + if (!isPlayerJoiningServer) { + isOnSkyBlock = false; + isPlayerJoiningServer = true; + main.getVersionChecker().runUpdateCheck(false); + if (MooConfig.doBestFriendsOnlineCheck && CredentialStorage.isMooValid && main.getFriendsHandler().getBestFriends().size() > 0) { + main.getFriendsHandler().runBestFriendsOnlineCheck(false); + } } - isOnSkyBlock = false; - main.getLogger().info("Joined the server"); } @SubscribeEvent @@ -105,6 +107,7 @@ public class PlayerListener { @SubscribeEvent public void onWorldEnter(PlayerSetSpawnEvent e) { + isPlayerJoiningServer = false; // check if player is on SkyBlock or on another gamemode new TickDelay(() -> { ScoreObjective scoreboardSidebar = e.entityPlayer.worldObj.getScoreboard().getObjectiveInDisplaySlot(1); @@ -145,9 +148,11 @@ public class PlayerListener { @SubscribeEvent public void onServerLeave(FMLNetworkEvent.ClientDisconnectionFromServerEvent e) { - main.getFriendsHandler().saveBestFriends(); - main.getPlayerCache().clearAllCaches(); - unregisterSkyBlockListeners(); - main.getLogger().info("Left the server"); + // check if player actually was on the server + if (!isPlayerJoiningServer) { + main.getFriendsHandler().saveBestFriends(); + main.getPlayerCache().clearAllCaches(); + unregisterSkyBlockListeners(); + } } } diff --git a/src/main/java/de/cowtipper/cowlection/util/ChatHelper.java b/src/main/java/de/cowtipper/cowlection/util/ChatHelper.java index 42a5790..eef9ca7 100644 --- a/src/main/java/de/cowtipper/cowlection/util/ChatHelper.java +++ b/src/main/java/de/cowtipper/cowlection/util/ChatHelper.java @@ -8,9 +8,10 @@ import net.minecraft.util.EnumChatFormatting; import net.minecraft.util.IChatComponent; import net.minecraftforge.client.event.ClientChatReceivedEvent; import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.entity.EntityJoinWorldEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -30,27 +31,43 @@ public class ChatHelper { } public void sendMessage(IChatComponent chatComponent) { - ClientChatReceivedEvent event = new ClientChatReceivedEvent((byte) 1, chatComponent); - MinecraftForge.EVENT_BUS.post(event); - if (!event.isCanceled()) { - if (Minecraft.getMinecraft().thePlayer == null) { - offlineMessages.add(event.message); - } else { + if (Minecraft.getMinecraft().thePlayer == null) { + putOfflineMessage(chatComponent); + } else { + ClientChatReceivedEvent event = new ClientChatReceivedEvent((byte) 1, chatComponent); + MinecraftForge.EVENT_BUS.post(event); + if (!event.isCanceled()) { Minecraft.getMinecraft().thePlayer.addChatMessage(event.message); } } } - public void sendOfflineMessages() { + private void putOfflineMessage(IChatComponent chatComponent) { + if (offlineMessages.size() == 0) { + // had no offline messages before + MinecraftForge.EVENT_BUS.register(this); + } + offlineMessages.add(chatComponent); + } + + @SubscribeEvent + public void onPlayerWorldJoin(EntityJoinWorldEvent e) { + if (e.entity == Minecraft.getMinecraft().thePlayer) { + new TickDelay(this::sendOfflineMessages, 6 * 20); + } + } + + private void sendOfflineMessages() { if (Minecraft.getMinecraft().thePlayer != null) { - Iterator<IChatComponent> offlineMessages = this.offlineMessages.iterator(); - if (offlineMessages.hasNext()) { + if (offlineMessages.size() > 0) { Minecraft.getMinecraft().thePlayer.playSound("random.levelup", 0.4F, 0.8F); } - while (offlineMessages.hasNext()) { - Minecraft.getMinecraft().thePlayer.addChatMessage(offlineMessages.next()); - offlineMessages.remove(); + + for (IChatComponent offlineMessage : offlineMessages) { + sendMessage(offlineMessage); } + offlineMessages.clear(); + MinecraftForge.EVENT_BUS.unregister(this); } } diff --git a/src/main/java/de/cowtipper/cowlection/util/VersionChecker.java b/src/main/java/de/cowtipper/cowlection/util/VersionChecker.java index 7f859a6..4fd465e 100644 --- a/src/main/java/de/cowtipper/cowlection/util/VersionChecker.java +++ b/src/main/java/de/cowtipper/cowlection/util/VersionChecker.java @@ -124,8 +124,7 @@ public class VersionChecker { main.getChatHelper().sendMessage(statusMsg); } else { IChatComponent finalStatusMsg = statusMsg; - new TickDelay(() -> main.getChatHelper().sendMessage(finalStatusMsg) - , 6 * 20); + new TickDelay(() -> main.getChatHelper().sendMessage(finalStatusMsg), 6 * 20); } } } |