Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Commit

Permalink
Fixed TileEntityPilotableImpl extending TileEntityNodePilotableImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
Tri0de committed Jul 10, 2020
1 parent b0ac801 commit ce25907
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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) {

}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit ce25907

Please sign in to comment.