From ce259070b787f90efe5c8ebe9f9989a76ebd2eef Mon Sep 17 00:00:00 2001 From: Tri0de Date: Thu, 9 Jul 2020 21:35:06 -0700 Subject: [PATCH] Fixed TileEntityPilotableImpl extending TileEntityNodePilotableImpl --- .../addon/control/ControlEventsClient.java | 5 +- .../tileentity/TileEntityLiftLever.java | 2 +- .../TileEntityNodePilotableImpl.java | 157 ++++++++++++++++++ .../tileentity/TileEntityShipHelm.java | 2 +- .../tileentity/TileEntitySpeedTelegraph.java | 2 +- .../entity/MixinsAbstractClientPlayer.java | 1 - .../mod/client/VSKeyHandler.java | 1 - .../mod/common/ValkyrienSkiesMod.java | 1 - .../mod/common/block/BlockPassengerChair.java | 2 +- .../common/piloting/ITileEntityPilotable.java | 15 ++ .../tileentity/TileEntityCaptainsChair.java | 1 - .../tileentity/TileEntityPilotableImpl.java | 19 +-- .../valkyrienskies/mod/proxy/ClientProxy.java | 1 - 13 files changed, 182 insertions(+), 27 deletions(-) create mode 100644 src/main/java/org/valkyrienskies/addon/control/tileentity/TileEntityNodePilotableImpl.java diff --git a/src/main/java/org/valkyrienskies/addon/control/ControlEventsClient.java b/src/main/java/org/valkyrienskies/addon/control/ControlEventsClient.java index c2365ad7f..4437ae53b 100644 --- a/src/main/java/org/valkyrienskies/addon/control/ControlEventsClient.java +++ b/src/main/java/org/valkyrienskies/addon/control/ControlEventsClient.java @@ -9,6 +9,7 @@ import net.minecraftforge.client.event.RenderGameOverlayEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import org.valkyrienskies.mod.common.piloting.IShipPilot; +import org.valkyrienskies.mod.common.piloting.ITileEntityPilotable; import org.valkyrienskies.mod.common.tileentity.TileEntityPilotableImpl; public class ControlEventsClient { @@ -24,8 +25,8 @@ public void render(RenderGameOverlayEvent.Post event) { if (playerPilot.isPiloting()) { BlockPos tilePilotedPos = playerPilot.getPosBeingControlled(); TileEntity pilotedTile = player.getEntityWorld().getTileEntity(tilePilotedPos); - if (pilotedTile instanceof TileEntityPilotableImpl) { - TileEntityPilotableImpl pilotedControlEntity = (TileEntityPilotableImpl) pilotedTile; + if (pilotedTile instanceof ITileEntityPilotable) { + ITileEntityPilotable pilotedControlEntity = (ITileEntityPilotable) pilotedTile; ScaledResolution scaledresolution = new ScaledResolution( Minecraft.getMinecraft()); pilotedControlEntity.renderPilotText(fontRenderer, scaledresolution); diff --git a/src/main/java/org/valkyrienskies/addon/control/tileentity/TileEntityLiftLever.java b/src/main/java/org/valkyrienskies/addon/control/tileentity/TileEntityLiftLever.java index 340136cf9..53263ceed 100644 --- a/src/main/java/org/valkyrienskies/addon/control/tileentity/TileEntityLiftLever.java +++ b/src/main/java/org/valkyrienskies/addon/control/tileentity/TileEntityLiftLever.java @@ -24,7 +24,7 @@ import java.util.Optional; -public class TileEntityLiftLever extends TileEntityPilotableImpl { +public class TileEntityLiftLever extends TileEntityNodePilotableImpl { private static final double LEVER_PULL_RATE = .075D; // Between 0 and 1, where .5 is the middle. diff --git a/src/main/java/org/valkyrienskies/addon/control/tileentity/TileEntityNodePilotableImpl.java b/src/main/java/org/valkyrienskies/addon/control/tileentity/TileEntityNodePilotableImpl.java new file mode 100644 index 000000000..f8732466b --- /dev/null +++ b/src/main/java/org/valkyrienskies/addon/control/tileentity/TileEntityNodePilotableImpl.java @@ -0,0 +1,157 @@ +package org.valkyrienskies.addon.control.tileentity; + +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.gui.ScaledResolution; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.util.EnumFacing; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; +import org.joml.Vector3d; +import org.valkyrienskies.addon.control.nodenetwork.BasicNodeTileEntity; +import org.valkyrienskies.mod.common.ValkyrienSkiesMod; +import org.valkyrienskies.mod.common.network.MessageStartPiloting; +import org.valkyrienskies.mod.common.network.MessageStopPiloting; +import org.valkyrienskies.mod.common.piloting.ControllerInputType; +import org.valkyrienskies.mod.common.piloting.ITileEntityPilotable; +import org.valkyrienskies.mod.common.piloting.PilotControlsMessage; +import org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject; +import org.valkyrienskies.mod.common.util.ValkyrienUtils; +import valkyrienwarfare.api.TransformType; + +import javax.annotation.Nullable; +import java.util.Optional; +import java.util.UUID; + +/** + * A basic implementation of the ITileEntityPilotable interface, other tile entities can extend this + * for easy controls. This version comes with a built in node as well. + */ +public abstract class TileEntityNodePilotableImpl extends BasicNodeTileEntity implements + ITileEntityPilotable { + + // Do NOT make this a reference to pilotPlayerEntity. + @Nullable + private UUID pilotPlayerEntity; + + public TileEntityNodePilotableImpl() { + super(); + this.pilotPlayerEntity = null; + } + + @Override + public final void onPilotControlsMessage(PilotControlsMessage message, EntityPlayerMP sender) { + if (sender.getUniqueID().equals(pilotPlayerEntity)) { + processControlMessage(message, sender); + } + } + + @Override + public final EntityPlayer getPilotEntity() { + if (pilotPlayerEntity != null) { + return getWorld().getPlayerEntityByUUID(pilotPlayerEntity); + } + return null; + } + + @Override + public final void setPilotEntity(EntityPlayer toSet) { + if (!getWorld().isRemote) { + EntityPlayer oldPlayer = getPilotEntity(); + sendPilotUpdatePackets((EntityPlayerMP) toSet, (EntityPlayerMP) oldPlayer); + } + if (toSet != null) { + pilotPlayerEntity = toSet.getUniqueID(); + onStartTileUsage(); + } else { + pilotPlayerEntity = null; + onStopTileUsage(); + } + } + + @Override + public final void playerWantsToStopPiloting(EntityPlayer player) { + if (player == getPilotEntity()) { + setPilotEntity(null); + } + } + + @Override + public final PhysicsObject getParentPhysicsEntity() { + Optional physicsObject = ValkyrienUtils.getPhysoManagingBlock(world, pos); + if (physicsObject.isPresent()) { + return physicsObject.get(); + } else { + return null; + } + } + + // Always call this before setting the pilotPlayerEntity to equal newPilot + private void sendPilotUpdatePackets(EntityPlayerMP newPilot, EntityPlayerMP oldPilot) { + // If old pilot equals new pilot, then don't send the stop piloting message + if (oldPilot != null && oldPilot != newPilot) { + MessageStopPiloting stopMessage = new MessageStopPiloting(getPos()); + ValkyrienSkiesMod.controlNetwork.sendTo(stopMessage, oldPilot); + } + if (newPilot != null) { + MessageStartPiloting startMessage = new MessageStartPiloting(getPos(), + setClientPilotingEntireShip(), + getControlInputType()); + ValkyrienSkiesMod.controlNetwork.sendTo(startMessage, newPilot); + } + } + + /** + * Unique for each tileentity type + * + * @return + */ + public abstract ControllerInputType getControlInputType(); + + /** + * Returns true if this control type is piloting the ship. + * + * @return + */ + @Deprecated + public boolean setClientPilotingEntireShip() { + return false; + } + + /** + * Unique for each tileentity type, only called if the sender player is the same as the + * pilotPlayerEntity + * + * @return + */ + public abstract void processControlMessage(PilotControlsMessage message, EntityPlayerMP sender); + + /** + * @param player + * @param blockFacing + * @return true if the passed player is in front of the given blockFacing, false if not. + */ + protected boolean isPlayerInFront(EntityPlayer player, EnumFacing blockFacing) { + Vector3d tileRelativePos = new Vector3d(this.getPos().getX() + .5, this.getPos().getY() + .5, + this.getPos().getZ() + .5); + if (this.getParentPhysicsEntity() != null) { + this.getParentPhysicsEntity().getShipTransformationManager() + .getCurrentTickTransform() + .transformPosition(tileRelativePos, TransformType.SUBSPACE_TO_GLOBAL); + } + tileRelativePos.sub(player.posX, player.posY, player.posZ); + Vector3d normal = new Vector3d(blockFacing.getDirectionVec().getX() * -1, + blockFacing.getDirectionVec().getY(), + blockFacing.getDirectionVec().getZ()); + + if (this.getParentPhysicsEntity() != null) { + this.getParentPhysicsEntity().getShipTransformationManager() + .getCurrentTickTransform() + .transformDirection(normal, TransformType.SUBSPACE_TO_GLOBAL); + } + + double dotProduct = tileRelativePos.dot(normal); + return dotProduct > 0; + } + +} diff --git a/src/main/java/org/valkyrienskies/addon/control/tileentity/TileEntityShipHelm.java b/src/main/java/org/valkyrienskies/addon/control/tileentity/TileEntityShipHelm.java index ad5d76f47..cef9387db 100644 --- a/src/main/java/org/valkyrienskies/addon/control/tileentity/TileEntityShipHelm.java +++ b/src/main/java/org/valkyrienskies/addon/control/tileentity/TileEntityShipHelm.java @@ -29,7 +29,7 @@ import java.util.Optional; -public class TileEntityShipHelm extends TileEntityPilotableImpl implements ITickable { +public class TileEntityShipHelm extends TileEntityNodePilotableImpl implements ITickable { public double compassAngle = 0; public double lastCompassAngle = 0; diff --git a/src/main/java/org/valkyrienskies/addon/control/tileentity/TileEntitySpeedTelegraph.java b/src/main/java/org/valkyrienskies/addon/control/tileentity/TileEntitySpeedTelegraph.java index ac3987742..20dcb8d65 100644 --- a/src/main/java/org/valkyrienskies/addon/control/tileentity/TileEntitySpeedTelegraph.java +++ b/src/main/java/org/valkyrienskies/addon/control/tileentity/TileEntitySpeedTelegraph.java @@ -21,7 +21,7 @@ import java.util.Collection; import java.util.Optional; -public class TileEntitySpeedTelegraph extends TileEntityPilotableImpl implements ITickable { +public class TileEntitySpeedTelegraph extends TileEntityNodePilotableImpl implements ITickable { private ShipChadburnState telegraphState; // The following fields are only used by the client for smooth interpolation diff --git a/src/main/java/org/valkyrienskies/mixin/client/entity/MixinsAbstractClientPlayer.java b/src/main/java/org/valkyrienskies/mixin/client/entity/MixinsAbstractClientPlayer.java index e1acdd0b6..7ed98a596 100644 --- a/src/main/java/org/valkyrienskies/mixin/client/entity/MixinsAbstractClientPlayer.java +++ b/src/main/java/org/valkyrienskies/mixin/client/entity/MixinsAbstractClientPlayer.java @@ -3,7 +3,6 @@ import net.minecraft.client.entity.AbstractClientPlayer; import net.minecraft.util.math.BlockPos; import org.spongepowered.asm.mixin.Mixin; -import org.valkyrienskies.addon.control.ValkyrienSkiesControl; import org.valkyrienskies.mod.common.ValkyrienSkiesMod; import org.valkyrienskies.mod.common.piloting.ControllerInputType; import org.valkyrienskies.mod.common.piloting.IShipPilotClient; diff --git a/src/main/java/org/valkyrienskies/mod/client/VSKeyHandler.java b/src/main/java/org/valkyrienskies/mod/client/VSKeyHandler.java index 136ba5374..878fbcb0b 100644 --- a/src/main/java/org/valkyrienskies/mod/client/VSKeyHandler.java +++ b/src/main/java/org/valkyrienskies/mod/client/VSKeyHandler.java @@ -9,7 +9,6 @@ import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import org.lwjgl.input.Keyboard; -import org.valkyrienskies.addon.control.ValkyrienSkiesControl; import org.valkyrienskies.mod.common.ValkyrienSkiesMod; import org.valkyrienskies.mod.common.network.MessagePlayerStoppedPiloting; import org.valkyrienskies.mod.common.piloting.IShipPilotClient; diff --git a/src/main/java/org/valkyrienskies/mod/common/ValkyrienSkiesMod.java b/src/main/java/org/valkyrienskies/mod/common/ValkyrienSkiesMod.java index a54b0ae2e..a8b641f1a 100644 --- a/src/main/java/org/valkyrienskies/mod/common/ValkyrienSkiesMod.java +++ b/src/main/java/org/valkyrienskies/mod/common/ValkyrienSkiesMod.java @@ -25,7 +25,6 @@ import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper; import net.minecraftforge.fml.common.registry.GameRegistry; import net.minecraftforge.fml.relauncher.Side; -import org.valkyrienskies.addon.world.ValkyrienSkiesWorld; import org.valkyrienskies.mixin.MixinLoaderForge; import org.valkyrienskies.mod.client.gui.TabValkyrienSkies; import org.valkyrienskies.mod.common.block.*; diff --git a/src/main/java/org/valkyrienskies/mod/common/block/BlockPassengerChair.java b/src/main/java/org/valkyrienskies/mod/common/block/BlockPassengerChair.java index cb074161a..9ff986b8c 100644 --- a/src/main/java/org/valkyrienskies/mod/common/block/BlockPassengerChair.java +++ b/src/main/java/org/valkyrienskies/mod/common/block/BlockPassengerChair.java @@ -5,7 +5,6 @@ import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; -import org.valkyrienskies.addon.control.util.BaseBlock; import org.valkyrienskies.mod.common.tileentity.TileEntityPassengerChair; import mcp.MethodsReturnNonnullByDefault; @@ -27,6 +26,7 @@ import net.minecraft.util.math.Vec3d; import net.minecraft.util.text.TextFormatting; import net.minecraft.world.World; +import org.valkyrienskies.mod.common.util.BaseBlock; @MethodsReturnNonnullByDefault @ParametersAreNonnullByDefault diff --git a/src/main/java/org/valkyrienskies/mod/common/piloting/ITileEntityPilotable.java b/src/main/java/org/valkyrienskies/mod/common/piloting/ITileEntityPilotable.java index 2779bde2a..952b97ba4 100644 --- a/src/main/java/org/valkyrienskies/mod/common/piloting/ITileEntityPilotable.java +++ b/src/main/java/org/valkyrienskies/mod/common/piloting/ITileEntityPilotable.java @@ -1,7 +1,11 @@ package org.valkyrienskies.mod.common.piloting; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.gui.ScaledResolution; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; import org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject; public interface ITileEntityPilotable { @@ -22,4 +26,15 @@ default void onStartTileUsage() { default void onStopTileUsage() { } + /** + * This is called during the post render of every frame in Minecraft. Override this to allow a + * pilot tileentity to display info as text on the screen. + * + * @param renderer + * @param gameResolution + */ + @SideOnly(Side.CLIENT) + default void renderPilotText(FontRenderer renderer, ScaledResolution gameResolution) { + + } } diff --git a/src/main/java/org/valkyrienskies/mod/common/tileentity/TileEntityCaptainsChair.java b/src/main/java/org/valkyrienskies/mod/common/tileentity/TileEntityCaptainsChair.java index 64f191fa3..9f8c4f028 100644 --- a/src/main/java/org/valkyrienskies/mod/common/tileentity/TileEntityCaptainsChair.java +++ b/src/main/java/org/valkyrienskies/mod/common/tileentity/TileEntityCaptainsChair.java @@ -6,7 +6,6 @@ import org.joml.AxisAngle4d; import org.joml.Matrix3d; import org.joml.Vector3d; -import org.valkyrienskies.addon.control.ValkyrienSkiesControl; import org.valkyrienskies.mod.common.ValkyrienSkiesMod; import org.valkyrienskies.mod.common.block.BlockCaptainsChair; import org.valkyrienskies.mod.common.piloting.ControllerInputType; diff --git a/src/main/java/org/valkyrienskies/mod/common/tileentity/TileEntityPilotableImpl.java b/src/main/java/org/valkyrienskies/mod/common/tileentity/TileEntityPilotableImpl.java index bfb2510f4..9b29aa9f5 100644 --- a/src/main/java/org/valkyrienskies/mod/common/tileentity/TileEntityPilotableImpl.java +++ b/src/main/java/org/valkyrienskies/mod/common/tileentity/TileEntityPilotableImpl.java @@ -7,6 +7,7 @@ import net.minecraft.client.gui.ScaledResolution; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @@ -27,11 +28,9 @@ /** * A basic implementation of the ITileEntityPilotable interface, other tile entities can extend this - * for easy controls - * - * @author thebest108 + * for easy controls. */ -public abstract class TileEntityPilotableImpl extends BasicNodeTileEntity implements +public abstract class TileEntityPilotableImpl extends TileEntity implements ITileEntityPilotable { // Do NOT make this a reference to pilotPlayerEntity. @@ -157,16 +156,4 @@ protected boolean isPlayerInFront(EntityPlayer player, EnumFacing blockFacing) { double dotProduct = tileRelativePos.dot(normal); return dotProduct > 0; } - - /** - * This is called during the post render of every frame in Minecraft. Override this to allow a - * pilot tileentity to display info as text on the screen. - * - * @param renderer - * @param gameResolution - */ - @SideOnly(Side.CLIENT) - public void renderPilotText(FontRenderer renderer, ScaledResolution gameResolution) { - - } } diff --git a/src/main/java/org/valkyrienskies/mod/proxy/ClientProxy.java b/src/main/java/org/valkyrienskies/mod/proxy/ClientProxy.java index f6b90eab2..9469c8f16 100644 --- a/src/main/java/org/valkyrienskies/mod/proxy/ClientProxy.java +++ b/src/main/java/org/valkyrienskies/mod/proxy/ClientProxy.java @@ -1,7 +1,6 @@ package org.valkyrienskies.mod.proxy; import net.minecraft.block.Block; -import org.valkyrienskies.addon.control.ValkyrienSkiesControl; import org.valkyrienskies.mod.client.EventsClient; import org.valkyrienskies.mod.client.VSKeyHandler; import org.valkyrienskies.mod.client.render.GibsAnimationRegistry;