Cleaned up Gui overriding and replaced it with Forge Event Handlers
This commit is contained in:
@@ -1,48 +1,82 @@
|
||||
package eu.crushedpixel.replaymod.events;
|
||||
|
||||
import eu.crushedpixel.replaymod.gui.GuiCustomMainMenu;
|
||||
import eu.crushedpixel.replaymod.gui.GuiCustomOptions;
|
||||
import eu.crushedpixel.replaymod.gui.GuiExitReplay;
|
||||
import eu.crushedpixel.replaymod.replay.ReplayHandler;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.GuiButton;
|
||||
import net.minecraft.client.gui.GuiChat;
|
||||
import net.minecraft.client.gui.GuiDisconnected;
|
||||
import net.minecraft.client.gui.GuiIngameMenu;
|
||||
import net.minecraft.client.gui.GuiMainMenu;
|
||||
import net.minecraft.client.gui.GuiOptions;
|
||||
import net.minecraft.client.gui.inventory.GuiInventory;
|
||||
import net.minecraft.client.resources.I18n;
|
||||
import net.minecraftforge.client.event.GuiOpenEvent;
|
||||
import net.minecraftforge.client.event.GuiScreenEvent.ActionPerformedEvent;
|
||||
import net.minecraftforge.client.event.GuiScreenEvent.InitGuiEvent;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
import eu.crushedpixel.replaymod.gui.GuiCustomOptions;
|
||||
import eu.crushedpixel.replaymod.gui.GuiExitReplay;
|
||||
import eu.crushedpixel.replaymod.gui.GuiReplayManager;
|
||||
import eu.crushedpixel.replaymod.gui.GuiReplaySaving;
|
||||
import eu.crushedpixel.replaymod.gui.GuiReplaySettings;
|
||||
import eu.crushedpixel.replaymod.recording.ConnectionEventHandler;
|
||||
import eu.crushedpixel.replaymod.replay.ReplayHandler;
|
||||
|
||||
public class GuiEventHandler {
|
||||
|
||||
@SubscribeEvent
|
||||
public void onGui(GuiOpenEvent event) {
|
||||
if(event.gui instanceof GuiMainMenu) {
|
||||
event.gui = new GuiCustomMainMenu();
|
||||
} else if(event.gui instanceof GuiOptions) {
|
||||
GuiOptions go = (GuiOptions)event.gui;
|
||||
GuiCustomOptions gco = new GuiCustomOptions(GuiCustomOptions.getGuiScreen(go), GuiCustomOptions.getGameSettings(go));
|
||||
event.gui = gco;
|
||||
}
|
||||
|
||||
else if(event.gui instanceof GuiChat || event.gui instanceof GuiInventory) {
|
||||
if(event.gui instanceof GuiChat || event.gui instanceof GuiInventory) {
|
||||
if(ReplayHandler.replayActive()) {
|
||||
event.setCanceled(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
else if(event.gui instanceof GuiIngameMenu) {
|
||||
if(ReplayHandler.replayActive()) {
|
||||
event.gui = new GuiExitReplay();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
else if(event.gui instanceof GuiDisconnected) {
|
||||
if(!ReplayHandler.replayActive() && System.currentTimeMillis() - ReplayHandler.lastExit < 5000) {
|
||||
event.setCanceled(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static final int REPLAY_MANAGER_ID = 9001;
|
||||
private static final int REPLAY_OPTIONS_ID = 9001;
|
||||
|
||||
@SubscribeEvent
|
||||
public void onInit(InitGuiEvent event) {
|
||||
if(event.gui instanceof GuiMainMenu) {
|
||||
int i1 = event.gui.height / 4 + 48;
|
||||
|
||||
for(GuiButton b : (List<GuiButton>)event.buttonList) {
|
||||
if(b.id != 0 && b.id != 4 && b.id != 5) {
|
||||
b.yPosition = b.yPosition - 24;
|
||||
}
|
||||
}
|
||||
|
||||
event.buttonList.add(new GuiButton(REPLAY_MANAGER_ID, event.gui.width / 2 - 100, i1 + 2*24, I18n.format("Replay Manager", new Object[0])));
|
||||
} else if(event.gui instanceof GuiOptions) {
|
||||
event.buttonList.add(new GuiButton(9001, event.gui.width / 2 - 155, event.gui.height / 6 + 48 - 6 - 24, 310, 20, "Replay Mod Settings..."));
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onButton(ActionPerformedEvent event) {
|
||||
if(event.gui instanceof GuiMainMenu && event.button.id == REPLAY_MANAGER_ID) {
|
||||
if(ConnectionEventHandler.saving) {
|
||||
Minecraft.getMinecraft().displayGuiScreen(new GuiReplaySaving());
|
||||
} else {
|
||||
Minecraft.getMinecraft().displayGuiScreen(new GuiReplayManager());
|
||||
}
|
||||
} else if(event.gui instanceof GuiOptions && event.button.id == REPLAY_OPTIONS_ID) {
|
||||
Minecraft.getMinecraft().displayGuiScreen(new GuiReplaySettings(event.gui));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -121,6 +121,8 @@ public class RecordingHandler {
|
||||
playerItems = new ItemStack[5];
|
||||
}
|
||||
|
||||
private int ticksSinceLastCorrection = 0;
|
||||
|
||||
@SubscribeEvent
|
||||
public void onPlayerTick(PlayerTickEvent e) {
|
||||
try {
|
||||
@@ -135,6 +137,12 @@ public class RecordingHandler {
|
||||
lastZ = e.player.posZ;
|
||||
}
|
||||
|
||||
ticksSinceLastCorrection++;
|
||||
if(ticksSinceLastCorrection >= 100) {
|
||||
ticksSinceLastCorrection = 0;
|
||||
force = true;
|
||||
}
|
||||
|
||||
double dx = e.player.posX - lastX;
|
||||
double dy = e.player.posY - lastY;
|
||||
double dz = e.player.posZ - lastZ;
|
||||
@@ -151,7 +159,6 @@ public class RecordingHandler {
|
||||
byte yaw = (byte)((int)(e.player.rotationYaw * 256.0F / 360.0F));
|
||||
byte pitch = (byte)((int)(e.player.rotationPitch * 256.0F / 360.0F));
|
||||
packet = new S18PacketEntityTeleport(entityID, x, y, z, yaw, pitch, e.player.onGround);
|
||||
|
||||
} else {
|
||||
byte oldYaw = (byte)((int)(e.player.prevRotationYaw * 256.0F / 360.0F));
|
||||
byte newYaw = (byte)((int)(e.player.rotationYaw * 256.0F / 360.0F));
|
||||
@@ -259,7 +266,7 @@ public class RecordingHandler {
|
||||
} else {
|
||||
lastRiding = mc.thePlayer.ridingEntity.getEntityId();
|
||||
}
|
||||
|
||||
|
||||
S1BPacketEntityAttach pea = new S1BPacketEntityAttach();
|
||||
|
||||
ByteBuf buf = Unpooled.buffer();
|
||||
@@ -273,6 +280,23 @@ public class RecordingHandler {
|
||||
|
||||
ConnectionEventHandler.insertPacket(pea);
|
||||
}
|
||||
|
||||
//Sleeping
|
||||
if(!mc.thePlayer.isPlayerSleeping() && wasSleeping) {
|
||||
S0BPacketAnimation pac = new S0BPacketAnimation();
|
||||
|
||||
ByteBuf bb = Unpooled.buffer();
|
||||
PacketBuffer pb = new PacketBuffer(bb);
|
||||
|
||||
pb.writeVarIntToBuffer(entityID);
|
||||
pb.writeByte(2);
|
||||
|
||||
pac.readPacketData(pb);
|
||||
|
||||
ConnectionEventHandler.insertPacket(pac);
|
||||
|
||||
wasSleeping = false;
|
||||
}
|
||||
|
||||
} catch(Exception e1) {
|
||||
e1.printStackTrace();
|
||||
@@ -377,29 +401,32 @@ public class RecordingHandler {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean wasSleeping = false;
|
||||
|
||||
@SubscribeEvent
|
||||
public void onSleep(PlayerSleepInBedEvent event) {
|
||||
try {
|
||||
if(event.entity.getEntityId() != mc.thePlayer.getEntityId()) {
|
||||
|
||||
if(event.entityPlayer != mc.thePlayer) {
|
||||
return;
|
||||
};
|
||||
|
||||
if(event.result == EnumStatus.OK) {
|
||||
S0APacketUseBed pub = new S0APacketUseBed();
|
||||
System.out.println(event.getResult());
|
||||
S0APacketUseBed pub = new S0APacketUseBed();
|
||||
|
||||
ByteBuf buf = Unpooled.buffer();
|
||||
PacketBuffer pbuf = new PacketBuffer(buf);
|
||||
ByteBuf buf = Unpooled.buffer();
|
||||
PacketBuffer pbuf = new PacketBuffer(buf);
|
||||
|
||||
pbuf.writeVarIntToBuffer(entityID);
|
||||
pbuf.writeBlockPos(event.pos);
|
||||
pbuf.writeVarIntToBuffer(entityID);
|
||||
pbuf.writeBlockPos(event.pos);
|
||||
|
||||
pub.readPacketData(pbuf);
|
||||
pub.readPacketData(pbuf);
|
||||
|
||||
ConnectionEventHandler.insertPacket(pub);
|
||||
ConnectionEventHandler.insertPacket(pub);
|
||||
|
||||
wasSleeping = true;
|
||||
|
||||
System.out.println("SLEEP PACKET SENT");
|
||||
}
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user