Skip to content

Commit

Permalink
spotless
Browse files Browse the repository at this point in the history
Signed-off-by: Glease <[email protected]>
  • Loading branch information
Glease committed Jul 16, 2023
1 parent 26862cd commit 6f1899f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package crazypants.enderio.api.teleport;

import crazypants.enderio.config.Config;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;

import cofh.api.energy.IEnergyContainerItem;
import crazypants.enderio.config.Config;

public interface IItemOfTravel extends IEnergyContainerItem {

Expand All @@ -13,10 +13,11 @@ public interface IItemOfTravel extends IEnergyContainerItem {
void extractInternal(ItemStack equipped, int power);

/**
* @param equipped player who is currently holding this item. caller will ensure the item held has your Item as item type
* @param equipped player who is currently holding this item. caller will ensure the item held has your Item as item
* type
* @return -1 if method not supported, or max(can extract, power), e.g. 0 if item is empty.
*/
default int canExtractInternal(ItemStack equipped, int power) {
return Config.strictTPItemChecking ? 0 : -1;
return Config.strictTPItemChecking ? 0 : -1;
}
}
2 changes: 1 addition & 1 deletion src/main/java/crazypants/enderio/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -2891,7 +2891,7 @@ public static void processConfig(Configuration config) {
"strictTPItemChecking",
strictTPItemChecking,
"If true, turn on strict checking of item energy checking. This might cause traveling tools from third party mods to fire fake security warnings on server side and not function correctly.")
.getBoolean(strictTPItemChecking);
.getBoolean(strictTPItemChecking);
}

public static void checkYetaAccess() {
Expand Down
36 changes: 12 additions & 24 deletions src/main/java/crazypants/enderio/teleport/TravelController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import javax.annotation.Nullable;

import crazypants.enderio.Log;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityClientPlayerMP;
Expand Down Expand Up @@ -112,15 +111,13 @@ private TravelController() {
}
}

public boolean validatePacketTravelEvent(EntityPlayerMP toTp, int x, int y, int z, int powerUse, boolean conserveMotion,
TravelSource source) {
public boolean validatePacketTravelEvent(EntityPlayerMP toTp, int x, int y, int z, int powerUse,
boolean conserveMotion, TravelSource source) {
BlockCoord target = new BlockCoord(x, y, z);
double dist = getDistanceSquared(toTp, target);
// allow 15% overshoot to account for rounding
if (dist * 100 > source.getMaxDistanceTravelledSq() * 115)
return false;
if (powerUse < getRequiredPower(toTp, source, target))
return false;
if (dist * 100 > source.getMaxDistanceTravelledSq() * 115) return false;
if (powerUse < getRequiredPower(toTp, source, target)) return false;
ItemStack equippedItem = toTp.getCurrentEquippedItem();
switch (source) {
case TELEPAD:
Expand All @@ -129,24 +126,19 @@ public boolean validatePacketTravelEvent(EntityPlayerMP toTp, int x, int y, int
case BLOCK:
// this source is only triggered when player is on any active travel anchor
BlockCoord on = getActiveTravelBlock(toTp);
if (on == null)
return false;
if (on == null) return false;
// target must be a valid selectedCoord
// selectedCoord can either be a block right above/below when the player is on anchor...
if (on.x == x && on.z == z)
return true;
if (on.x == x && on.z == z) return true;
// or another anchor
TileEntity maybeAnchor = target.getTileEntity(toTp.worldObj);
if (!(maybeAnchor instanceof ITravelAccessable))
return false;
if (!(maybeAnchor instanceof ITravelAccessable)) return false;
ITravelAccessable anchor = (ITravelAccessable) maybeAnchor;
return anchor.canBlockBeAccessed(toTp) || !isValidTarget(toTp, target, TravelSource.BLOCK);
case STAFF:
case STAFF_BLINK:
if (equippedItem == null || !(equippedItem.getItem() instanceof IItemOfTravel))
return false;
if (!((IItemOfTravel) equippedItem.getItem()).isActive(toTp, equippedItem))
return false;
if (equippedItem == null || !(equippedItem.getItem() instanceof IItemOfTravel)) return false;
if (!((IItemOfTravel) equippedItem.getItem()).isActive(toTp, equippedItem)) return false;
int energy = ((IItemOfTravel) equippedItem.getItem()).canExtractInternal(equippedItem, powerUse);
return energy == -1 || energy == powerUse;
case TELEPORT_STAFF_BLINK:
Expand Down Expand Up @@ -807,9 +799,7 @@ private BlockCoord getValidVerticalTarget(EntityPlayer player, BlockCoord curren
showMessage(player, new ChatComponentTranslation("enderio.gui.travelAccessable.skipPrivate"));
}
if (!isValidTarget(player, targetBlock, TravelSource.BLOCK)) {
showMessage(
player,
new ChatComponentTranslation("enderio.gui.travelAccessable.skipObstructed"));
showMessage(player, new ChatComponentTranslation("enderio.gui.travelAccessable.skipObstructed"));
}
}
if (travelBlock.canBlockBeAccessed(player) && isValidTarget(player, targetBlock, TravelSource.BLOCK)) {
Expand Down Expand Up @@ -981,11 +971,9 @@ public boolean doClientTeleport(Entity entity, BlockCoord bc, TravelSource sourc
}

private BlockCoord getActiveTravelBlock(EntityPlayer player) {
if (player == null)
return null;
if (player == null) return null;
World world = player.worldObj;
if (world == null)
return null;
if (world == null) return null;
int x = MathHelper.floor_double(player.posX);
int y = MathHelper.floor_double(player.boundingBox.minY) - 1;
int z = MathHelper.floor_double(player.posZ);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.util.Optional;

import crazypants.enderio.Log;
import crazypants.enderio.teleport.ItemTeleportStaff;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
Expand All @@ -18,9 +16,11 @@
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import crazypants.enderio.Log;
import crazypants.enderio.api.teleport.IItemOfTravel;
import crazypants.enderio.api.teleport.TeleportEntityEvent;
import crazypants.enderio.api.teleport.TravelSource;
import crazypants.enderio.teleport.ItemTeleportStaff;
import crazypants.enderio.teleport.TravelController;
import io.netty.buffer.ByteBuf;

Expand Down Expand Up @@ -58,15 +58,22 @@ public IMessage onMessage(PacketLongDistanceTravelEvent message, MessageContext
if (message.entityId != -1) {
// after checking the code base, this type of packet won't be sent at all,
// so we can assume this to be an attempt to hack
Log.LOGGER.warn(Log.securityMarker, "Player {} tried to illegally tp other entity {}.", ctx.getServerHandler().playerEntity.getGameProfile(), message.entityId);
Log.LOGGER.warn(
Log.securityMarker,
"Player {} tried to illegally tp other entity {}.",
ctx.getServerHandler().playerEntity.getGameProfile(),
message.entityId);
return null;
}
EntityPlayerMP toTp = ctx.getServerHandler().playerEntity;

TravelSource source = TravelSource.values()[message.source];

if (!validate(toTp, source)) {
Log.LOGGER.warn(Log.securityMarker, "Player {} tried to tp without valid prereq.", ctx.getServerHandler().playerEntity.getGameProfile());
Log.LOGGER.warn(
Log.securityMarker,
"Player {} tried to tp without valid prereq.",
ctx.getServerHandler().playerEntity.getGameProfile());
return null;
}

Expand All @@ -79,8 +86,8 @@ private static boolean validate(EntityPlayerMP toTp, TravelSource source) {
ItemStack equippedItem = toTp.getCurrentEquippedItem();
switch (source) {
case STAFF:
return equippedItem != null && equippedItem.getItem() instanceof IItemOfTravel &&
((IItemOfTravel) equippedItem.getItem()).isActive(toTp, equippedItem);
return equippedItem != null && equippedItem.getItem() instanceof IItemOfTravel
&& ((IItemOfTravel) equippedItem.getItem()).isActive(toTp, equippedItem);
case TELEPORT_STAFF:
// tp staff is creative version of traveling staff
// no energy check or anything else needed
Expand All @@ -106,8 +113,10 @@ public static boolean doServerTeleport(Entity toTp, boolean conserveMotion, Trav
if (powerUse < 0) {
return false;
}
if (player != null && player.getCurrentEquippedItem() != null && player.getCurrentEquippedItem().getItem() instanceof IItemOfTravel) {
int used = ((IItemOfTravel) player.getCurrentEquippedItem().getItem()).canExtractInternal(player.getCurrentEquippedItem(), powerUse);
if (player != null && player.getCurrentEquippedItem() != null
&& player.getCurrentEquippedItem().getItem() instanceof IItemOfTravel) {
int used = ((IItemOfTravel) player.getCurrentEquippedItem().getItem())
.canExtractInternal(player.getCurrentEquippedItem(), powerUse);
if (used != -1 && used != powerUse) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package crazypants.enderio.teleport.packet;

import crazypants.enderio.Log;
import crazypants.enderio.teleport.TravelController;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
Expand All @@ -15,9 +13,11 @@
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import crazypants.enderio.Log;
import crazypants.enderio.api.teleport.IItemOfTravel;
import crazypants.enderio.api.teleport.TeleportEntityEvent;
import crazypants.enderio.api.teleport.TravelSource;
import crazypants.enderio.teleport.TravelController;
import io.netty.buffer.ByteBuf;

public class PacketTravelEvent implements IMessage, IMessageHandler<PacketTravelEvent, IMessage> {
Expand Down Expand Up @@ -70,7 +70,11 @@ public IMessage onMessage(PacketTravelEvent message, MessageContext ctx) {
if (message.entityId != -1) {
// after checking the code base, this type of packet won't be sent at all,
// so we can assume this to be an attempt to hack
Log.LOGGER.warn(Log.securityMarker, "Player {} tried to illegally tp other entity {}.", ctx.getServerHandler().playerEntity.getGameProfile(), message.entityId);
Log.LOGGER.warn(
Log.securityMarker,
"Player {} tried to illegally tp other entity {}.",
ctx.getServerHandler().playerEntity.getGameProfile(),
message.entityId);
return null;
}
EntityPlayerMP toTp = ctx.getServerHandler().playerEntity;
Expand All @@ -79,8 +83,12 @@ public IMessage onMessage(PacketTravelEvent message, MessageContext ctx) {

TravelSource source = TravelSource.values()[message.source];

if (!TravelController.instance.validatePacketTravelEvent(toTp, x, y, z, message.powerUse, message.conserveMotion, source)) {
Log.LOGGER.warn(Log.securityMarker, "Player {} tried to tp without valid prereq.", ctx.getServerHandler().playerEntity.getGameProfile());
if (!TravelController.instance
.validatePacketTravelEvent(toTp, x, y, z, message.powerUse, message.conserveMotion, source)) {
Log.LOGGER.warn(
Log.securityMarker,
"Player {} tried to tp without valid prereq.",
ctx.getServerHandler().playerEntity.getGameProfile());
return null;
}

Expand Down

0 comments on commit 6f1899f

Please sign in to comment.