Skip to content

Commit

Permalink
input fixes
Browse files Browse the repository at this point in the history
- Various simplifications and optimizations made to input handling, shouldn't be noticeable outside of network traffic monitoring.
- Inputs now identify the player sending them for convenience, should fix some permissions bugs.
- Fixed steam trains ignoring player pressing forward/backwards.
- Trains now properly get the player facing direction from seats, and fallback to their own rotation if that fails.
- Fixed a couple crashes related to logic ticks happening between seat spawns.
  • Loading branch information
EternalBlueFlame committed Jan 13, 2025
1 parent a1c6953 commit afb11c1
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 118 deletions.
14 changes: 0 additions & 14 deletions src/main/java/ebf/tim/entities/EntitySeat.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,6 @@ public void onUpdate() {
}

}
if (riddenByEntity != null && riddenByEntity instanceof EntityPlayer && worldObj.isRemote && this.isControlSeat()) {
if (TCKeyHandler.inventory.isPressed()) {
if (this.parent instanceof Locomotive) {
Traincraft.keyChannel.sendToServer(new PacketKeyPress(7));
} else if (this.parent instanceof AbstractWorkCart) {
Traincraft.keyChannel.sendToServer(new PacketKeyPress(7));
}
} else if (TCKeyHandler.furnace.isPressed() && this.parent instanceof AbstractWorkCart) {
Traincraft.keyChannel.sendToServer(new PacketKeyPress(9));
}
}
if (this.parent instanceof Locomotive && this.isControlSeat() && worldObj.isRemote) {
((Locomotive)this.parent).keyHandling();
}
}

@Override
Expand Down
66 changes: 57 additions & 9 deletions src/main/java/train/client/core/handlers/TCKeyHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.InputEvent;
import ebf.tim.entities.EntitySeat;
import ebf.tim.utility.DebugUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ChatComponentText;
import org.lwjgl.input.Keyboard;
import train.client.gui.GuiMTCInfo;
import train.common.Traincraft;
import train.common.api.AbstractTrains;
import train.common.api.Locomotive;
import train.common.api.SteamTrain;
import train.common.core.handlers.ConfigHandler;
import train.common.core.network.PacketKeyPress;
import train.common.entity.zeppelin.AbstractZeppelin;

public class TCKeyHandler {
public static KeyBinding horn;
Expand Down Expand Up @@ -60,7 +65,15 @@ public TCKeyHandler() {

@SubscribeEvent
public void onKeyInput(InputEvent.KeyInputEvent event) {
if (!Minecraft.getMinecraft().ingameGUI.getChatGUI().getChatOpen()) {
if(Minecraft.getMinecraft().ingameGUI.getChatGUI().getChatOpen()){
return;
}

Entity riding =Minecraft.getMinecraft().thePlayer.ridingEntity;
if(riding instanceof EntitySeat){
riding=((EntitySeat) riding).parent;
}
if (riding instanceof AbstractTrains || riding instanceof AbstractZeppelin){
if (up.getIsKeyPressed()) {
sendKeyControlsPacket(0);
}
Expand All @@ -77,18 +90,57 @@ public void onKeyInput(InputEvent.KeyInputEvent event) {
sendKeyControlsPacket(7);
}

if (horn.getIsKeyPressed()) {
sendKeyControlsPacket(8);
}

if (furnace.getIsKeyPressed()) {
sendKeyControlsPacket(9);
}

if (FMLClientHandler.instance().getClient().gameSettings.keyBindSneak.getIsKeyPressed() && Keyboard.isKeyDown(Keyboard.KEY_F3)) {
sendKeyControlsPacket(404);
}
}

if (riding instanceof Locomotive) {

if (horn.getIsKeyPressed()) {
sendKeyControlsPacket(8);
}

if (bell.getIsKeyPressed()) {
sendKeyControlsPacket(10);
}



if (Keyboard.isKeyDown(FMLClientHandler.instance().getClient().gameSettings.keyBindForward.getKeyCode())
&& !((Locomotive) riding).forwardPressed) {
Traincraft.keyChannel.sendToServer(new PacketKeyPress(4));
((Locomotive) riding).forwardPressed = true;
} else if (!Keyboard
.isKeyDown(FMLClientHandler.instance().getClient().gameSettings.keyBindForward.getKeyCode())
&& ((Locomotive) riding).forwardPressed) {
Traincraft.keyChannel.sendToServer(new PacketKeyPress(13));
((Locomotive) riding).forwardPressed = false;
}
if (Keyboard.isKeyDown(FMLClientHandler.instance().getClient().gameSettings.keyBindBack.getKeyCode())
&& !((Locomotive) riding).backwardPressed) {
Traincraft.keyChannel.sendToServer(new PacketKeyPress(5));
((Locomotive) riding).backwardPressed = true;
} else if (!Keyboard
.isKeyDown(FMLClientHandler.instance().getClient().gameSettings.keyBindBack.getKeyCode())
&& ((Locomotive) riding).backwardPressed) {
Traincraft.keyChannel.sendToServer(new PacketKeyPress(14));
((Locomotive) riding).backwardPressed = false;
}
if (Keyboard.isKeyDown(FMLClientHandler.instance().getClient().gameSettings.keyBindJump.getKeyCode())
&& !((Locomotive) riding).brakePressed) {
Traincraft.keyChannel.sendToServer(new PacketKeyPress(12));
((Locomotive) riding).brakePressed = true;
} else if (!Keyboard
.isKeyDown(FMLClientHandler.instance().getClient().gameSettings.keyBindJump.getKeyCode())
&& ((Locomotive) riding).brakePressed) {
Traincraft.keyChannel.sendToServer(new PacketKeyPress(15));
((Locomotive) riding).brakePressed = false;
}
if (Traincraft.hasComputerCraft()) {
if (MTCScreen.getIsKeyPressed() && !FMLClientHandler.instance().isGUIOpen(GuiMTCInfo.class)) {
if (Minecraft.getMinecraft().thePlayer.ridingEntity != null) {
Expand Down Expand Up @@ -144,10 +196,6 @@ public void onKeyInput(InputEvent.KeyInputEvent event) {

}
}

if (FMLClientHandler.instance().getClient().gameSettings.keyBindSneak.getIsKeyPressed() && Keyboard.isKeyDown(Keyboard.KEY_F3)) {
sendKeyControlsPacket(404);
}
}


Expand Down
2 changes: 1 addition & 1 deletion src/main/java/train/common/api/AbstractControlCar.java
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ else if (brakePressed)
}

@Override
public void keyHandlerFromPacket(int i)
public void keyHandlerFromPacket(int i, int player)
{
if (this.getTrainLockedFromPacket())
{
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/train/common/api/EntityRollingStock.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.minecraft.entity.item.EntityMinecart;
import net.minecraft.entity.monster.EntityCreeper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemDye;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -539,20 +540,19 @@ public float getPlayerScale() {
*
* @param
*/
public boolean isLockedAndNotOwner() {
public boolean isLockedAndNotOwner(int player) {
Entity p = getWorld().getEntityByID(player);
if(!(p instanceof EntityPlayer)){
return false;
}
if (this.getTrainLockedFromPacket()) {
if (this.riddenByEntity instanceof EntityPlayer && !((EntityPlayer) this.riddenByEntity).getDisplayName().equalsIgnoreCase(this.getTrainOwner())) {
return true;
}
if (this.seats.size() > 0 && this.seats.get(0).getPassenger() instanceof EntityPlayer && !((EntityPlayer) this.seats.get(0).getPassenger()).getDisplayName().equalsIgnoreCase(this.getTrainOwner())) {
return true;
}
return !((EntityPlayer) p).getDisplayName().equalsIgnoreCase(this.getTrainOwner());
}
return false;
}
public void keyHandlerFromPacket(int i) {
public void keyHandlerFromPacket(int i, int player) {
if (this.getTrainLockedFromPacket()) {
if (isLockedAndNotOwner()) {
if (isLockedAndNotOwner(player)) {
return;
}
}
Expand Down Expand Up @@ -837,7 +837,7 @@ else if (seats.size() != 0 && worldObj.isRemote && Traincraft.proxy.getCurrentSc

}
if (seats.size() != 0) {
for (int i = 0; i < seats.size(); i++) {
for (int i = 0; i < seats.size() && i<getRiderOffsets().length; i++) {
if (seats.get(i) != null) {
TraincraftUtil.updateRider(this, getRiderOffsets()[i][0], getRiderOffsets()[i][1] + 2, getRiderOffsets()[i][2], seats.get(i));
}
Expand Down
123 changes: 41 additions & 82 deletions src/main/java/train/common/api/Locomotive.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import ebf.tim.entities.EntitySeat;
import ebf.tim.utility.DebugUtil;
import io.netty.buffer.ByteBuf;
import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity;
Expand Down Expand Up @@ -498,9 +499,9 @@ public void setCanBeAdjusted(boolean canBeAdj) {
* @param i
*/
@Override
public void keyHandlerFromPacket(int i) {
public void keyHandlerFromPacket(int i, int player) {
if (this.getTrainLockedFromPacket()) {
if (isLockedAndNotOwner()) {
if (isLockedAndNotOwner(player)) {
return;
}
}
Expand Down Expand Up @@ -647,40 +648,6 @@ public void soundWhistle() {
}
}

@SideOnly(Side.CLIENT)
public void keyHandling() {
if (Keyboard.isKeyDown(FMLClientHandler.instance().getClient().gameSettings.keyBindForward.getKeyCode())
&& !forwardPressed) {
Traincraft.keyChannel.sendToServer(new PacketKeyPress(4));
forwardPressed = true;
} else if (!Keyboard
.isKeyDown(FMLClientHandler.instance().getClient().gameSettings.keyBindForward.getKeyCode())
&& forwardPressed) {
Traincraft.keyChannel.sendToServer(new PacketKeyPress(13));
forwardPressed = false;
}
if (Keyboard.isKeyDown(FMLClientHandler.instance().getClient().gameSettings.keyBindBack.getKeyCode())
&& !backwardPressed) {
Traincraft.keyChannel.sendToServer(new PacketKeyPress(5));
backwardPressed = true;
} else if (!Keyboard
.isKeyDown(FMLClientHandler.instance().getClient().gameSettings.keyBindBack.getKeyCode())
&& backwardPressed) {
Traincraft.keyChannel.sendToServer(new PacketKeyPress(14));
backwardPressed = false;
}
if (Keyboard.isKeyDown(FMLClientHandler.instance().getClient().gameSettings.keyBindJump.getKeyCode())
&& !brakePressed) {
Traincraft.keyChannel.sendToServer(new PacketKeyPress(12));
brakePressed = true;
} else if (!Keyboard
.isKeyDown(FMLClientHandler.instance().getClient().gameSettings.keyBindJump.getKeyCode())
&& brakePressed) {
Traincraft.keyChannel.sendToServer(new PacketKeyPress(15));
brakePressed = false;
}
}

private void cycleBeaconIndex()
{
if (isBeaconEnabled && ticksExisted % 5 == 0)
Expand All @@ -697,60 +664,52 @@ private void cycleBeaconIndex()
public void onUpdate()
{
cycleBeaconIndex();

if (worldObj.isRemote && ticksExisted %2 ==0)
{
keyHandling();
}
if (!worldObj.isRemote) {
if (this.riddenByEntity instanceof EntityPlayer || (this.seats.size() > 0 && this.seats.get(0).getPassenger() != null)) {
EntityPlayer passenger = (EntityPlayer) this.riddenByEntity;
if (this.seats.size() != 0 && this.seats.get(0).getPassenger() != null) {
passenger = (EntityPlayer) this.seats.get(0).getPassenger();
}
if (forwardPressed || backwardPressed) {
if (getFuel() > 0 && this.isLocoTurnedOn() && rand.nextInt(4) == 0 && !worldObj.isRemote) {
if (isLockedAndNotOwner()) {
return;
if (forwardPressed || backwardPressed) {
if (getFuel() > 0 && this.isLocoTurnedOn() && rand.nextInt(4) == 0) {
if(this instanceof SteamTrain && !getState().equals("hot") && !getState().equals("too hot")){
return;
}
if(this instanceof DieselTrain && (getState().equals("broken") || getState().equals("cold"))){
return;
}
float y=getYaw();
for(EntitySeat s : seats){
if(s!=null && s.isControlSeat() && s.getPassenger()!=null){
y=s.getPassenger().rotationYaw;
}
if(this instanceof SteamTrain && (!getState().equals("hot") || !getState().equals("too hot"))){
return;
}
int dir = MathHelper
.floor_double((y * 4F) / 360F + 0.5D) & 3;
if (dir == 2){
if (forwardPressed) {
motionZ -= 0.0075 * this.accelerate;
} else {
motionZ += 0.0075 * this.accelerate;
}
if(this instanceof DieselTrain && (getState().equals("broken") || getState().equals("cold"))){
return;
} else if (dir == 0){
if (forwardPressed) {
motionZ += 0.0075 * this.accelerate;
} else {
motionZ -= 0.0075 * this.accelerate;
}
int dir = MathHelper
.floor_double((passenger.rotationYaw * 4F) / 360F + 0.5D) & 3;
if (dir == 2){
if (forwardPressed) {
motionZ -= 0.0075 * this.accelerate;
} else {
motionZ += 0.0075 * this.accelerate;
}
} else if (dir == 0){
if (forwardPressed) {
motionZ += 0.0075 * this.accelerate;
} else {
motionZ -= 0.0075 * this.accelerate;
}
} else if (dir == 1){
if (forwardPressed) {
motionX -= 0.0075 * this.accelerate;
} else {
motionX += 0.0075 * this.accelerate;
}
} else if (dir == 1){
if (forwardPressed) {
motionX -= 0.0075 * this.accelerate;
} else {
motionX += 0.0075 * this.accelerate;
}
} else {
if (forwardPressed) {
motionX += 0.0075 * this.accelerate;
} else {
if (forwardPressed) {
motionX += 0.0075 * this.accelerate;
} else {
motionX -= 0.0075 * this.accelerate;
}
motionX -= 0.0075 * this.accelerate;
}
}
} else if (brakePressed) {
motionX *= brake;
motionZ *= brake;
}
} else if (brakePressed) {
motionX *= brake;
motionZ *= brake;
}


Expand Down
5 changes: 3 additions & 2 deletions src/main/java/train/common/core/network/PacketKeyPress.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import ebf.tim.entities.EntitySeat;
import ebf.tim.utility.DebugUtil;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.Entity;
import train.common.api.EntityRollingStock;
Expand Down Expand Up @@ -61,9 +62,9 @@ public IMessage onMessage(PacketKeyPress message, MessageContext context) {
/* "instanceof" is null-safe, but we check to avoid four unnecessary instanceof checks for when the value is null anyways. */
if (ridingEntity != null) {
if (ridingEntity instanceof Locomotive) {
((Locomotive) ridingEntity).keyHandlerFromPacket(message.key);
((Locomotive) ridingEntity).keyHandlerFromPacket(message.key, context.getServerHandler().playerEntity.getEntityId());
} else if (ridingEntity instanceof EntityRollingStock) {
((EntityRollingStock) ridingEntity).keyHandlerFromPacket(message.key);
((EntityRollingStock) ridingEntity).keyHandlerFromPacket(message.key, context.getServerHandler().playerEntity.getEntityId());
} else if (ridingEntity instanceof AbstractZeppelin) {
((AbstractZeppelin) ridingEntity).pressKey(message.key);
} else if (ridingEntity instanceof EntityRotativeDigger) {
Expand Down

0 comments on commit afb11c1

Please sign in to comment.