diff options
Diffstat (limited to 'src/main/java/io')
-rw-r--r-- | src/main/java/io/github/moulberry/notenoughupdates/mixins/MixinGuiScreen.java | 19 | ||||
-rw-r--r-- | src/main/java/io/github/moulberry/notenoughupdates/util/SBInfo.java | 30 |
2 files changed, 43 insertions, 6 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/mixins/MixinGuiScreen.java b/src/main/java/io/github/moulberry/notenoughupdates/mixins/MixinGuiScreen.java new file mode 100644 index 00000000..6b1065ea --- /dev/null +++ b/src/main/java/io/github/moulberry/notenoughupdates/mixins/MixinGuiScreen.java @@ -0,0 +1,19 @@ +package io.github.moulberry.notenoughupdates.mixins; + +import io.github.moulberry.notenoughupdates.util.SBInfo; +import net.minecraft.client.entity.EntityPlayerSP; +import net.minecraft.client.gui.GuiScreen; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(GuiScreen.class) +public class MixinGuiScreen { + + @Inject(method = "sendChatMessage(Ljava/lang/String;Z)V", at=@At("HEAD")) + public void onSendChatMessage(String message, boolean addToChat, CallbackInfo ci) { + SBInfo.getInstance().onSendChatMessage(message); + } + +} diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/SBInfo.java b/src/main/java/io/github/moulberry/notenoughupdates/util/SBInfo.java index 314096aa..a17c9ec9 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/SBInfo.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/SBInfo.java @@ -13,6 +13,7 @@ import net.minecraft.scoreboard.Scoreboard; import net.minecraftforge.client.event.ClientChatReceivedEvent; import net.minecraftforge.client.event.GuiOpenEvent; import net.minecraftforge.event.world.WorldEvent; +import net.minecraftforge.fml.common.eventhandler.EventPriority; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent; @@ -45,7 +46,9 @@ public class SBInfo { return INSTANCE; } + private long lastManualLocRaw = -1; private long lastLocRaw = -1; + private long joinedWorld = -1; private JsonObject locraw = null; @SubscribeEvent @@ -66,16 +69,26 @@ public class SBInfo { lastLocRaw = -1; locraw = null; mode = null; + joinedWorld = System.currentTimeMillis(); lastOpenContainerName = null; } - @SubscribeEvent + private static final Pattern JSON_BRACKET_PATTERN = Pattern.compile("\\{.+}"); + + public void onSendChatMessage(String msg) { + if(msg.trim().startsWith("/locraw") || msg.trim().startsWith("/locraw ")) { + lastManualLocRaw = System.currentTimeMillis(); + } + } + + @SubscribeEvent(priority = EventPriority.LOW, receiveCanceled = true) public void onChatMessage(ClientChatReceivedEvent event) { - if(event.message.getUnformattedText().startsWith("{")) { + Matcher matcher = JSON_BRACKET_PATTERN.matcher(event.message.getUnformattedText()); + if(matcher.find()) { try { - JsonObject obj = NotEnoughUpdates.INSTANCE.manager.gson.fromJson(event.message.getUnformattedText(), JsonObject.class); + JsonObject obj = NotEnoughUpdates.INSTANCE.manager.gson.fromJson(matcher.group(), JsonObject.class); if(obj.has("server")) { - if(true || System.currentTimeMillis() - lastLocRaw < 5000) event.setCanceled(true); + if(System.currentTimeMillis() - lastManualLocRaw > 5000) event.setCanceled(true); if(obj.has("gametype") && obj.has("mode") && obj.has("map")) { locraw = obj; mode = locraw.get("mode").getAsString(); @@ -95,8 +108,13 @@ public class SBInfo { } public void tick() { - if(Minecraft.getMinecraft().theWorld != null && - locraw == null && (System.currentTimeMillis() - lastLocRaw) > 20000) { + long currentTime = System.currentTimeMillis(); + + if(Minecraft.getMinecraft().thePlayer != null && + Minecraft.getMinecraft().theWorld != null && + locraw == null && + (currentTime - joinedWorld) > 1000 && + (currentTime - lastLocRaw) > 10000) { lastLocRaw = System.currentTimeMillis(); NotEnoughUpdates.INSTANCE.sendChatMessage("/locraw"); } |