Skip to content

Commit

Permalink
use openContainer instead of sent coords (#3644)
Browse files Browse the repository at this point in the history
  • Loading branch information
Glease authored Aug 3, 2023
1 parent 1562b9e commit 050a0ae
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/li/cil/oc/common/container/Drone.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import li.cil.oc.common.entity
import net.minecraft.entity.player.InventoryPlayer
import net.minecraft.inventory.IInventory

class Drone(playerInventory: InventoryPlayer, drone: entity.Drone) extends Player(playerInventory, drone.mainInventory) {
class Drone(playerInventory: InventoryPlayer, val drone: entity.Drone) extends Player(playerInventory, drone.mainInventory) {
val deltaY = 0

for (i <- 0 to 1) {
Expand Down
69 changes: 45 additions & 24 deletions src/main/scala/li/cil/oc/server/PacketHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package li.cil.oc.server
import cpw.mods.fml.common.eventhandler.SubscribeEvent
import cpw.mods.fml.common.network.FMLNetworkEvent.ServerCustomPacketEvent
import li.cil.oc.Localization
import li.cil.oc.OpenComputers
import li.cil.oc.api
import li.cil.oc.api.internal.Server
import li.cil.oc.api.machine.Machine
import li.cil.oc.common.Achievement
import li.cil.oc.common.PacketType
import li.cil.oc.common.component.TextBuffer
import li.cil.oc.common.container
import li.cil.oc.common.entity.Drone
import li.cil.oc.common.item.Delegator
import li.cil.oc.common.item.data.DriveData
Expand All @@ -22,8 +24,14 @@ import net.minecraft.entity.player.EntityPlayerMP
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.network.NetHandlerPlayServer
import net.minecraftforge.common.DimensionManager
import org.apache.logging.log4j.MarkerManager

object PacketHandler extends CommonPacketHandler {
private val securityMarker = MarkerManager.getMarker("SuspiciousPackets")

private def logForgedPacket(player: EntityPlayerMP) =
OpenComputers.log.warn(securityMarker, "Player {} tried to send GUI packets without opening them", player.getGameProfile)

@SubscribeEvent
def onPacket(e: ServerCustomPacketEvent) =
onPacketData(e.packet.payload, e.handler.asInstanceOf[NetHandlerPlayServer].playerEntity)
Expand Down Expand Up @@ -60,10 +68,14 @@ object PacketHandler extends CommonPacketHandler {
def onComputerPower(p: PacketParser): Unit = {
val entity = p.readTileEntity[Computer]()
val setPower = p.readBoolean()
entity match {
case Some(t) => p.player match {
case player: EntityPlayerMP => trySetComputerPower(t.machine, setPower, player)
case _ =>
p.player match {
case player: EntityPlayerMP => player.openContainer match {
case container: container.Player => (container.otherInventory, entity) match {
case (computer: Computer, Some(c2)) if c2.position == computer.position =>
trySetComputerPower(computer.machine, setPower, player)
case _ => logForgedPacket(player)
}
case _ => logForgedPacket(player)
}
case _ => // Invalid packet.
}
Expand All @@ -72,17 +84,24 @@ object PacketHandler extends CommonPacketHandler {
def onServerPower(p: PacketParser): Unit = {
val entity = p.readTileEntity[Rack]()
val index = p.readInt()
val setPower = p.readBoolean()
entity match {
val readServer = entity match {
case Some(t) =>
val mountableIndex = index
t.getMountable(mountableIndex) match {
case server: Server => p.player match {
case player: EntityPlayerMP => trySetComputerPower(server.machine, setPower, player)
case _ => // Invalid packet.
}
case _ => // Invalid packet.
t.getMountable(index) match {
case server: Server => server
case _ => return // probably just lag, not invalid packet
}
case _ => return
}
val setPower = p.readBoolean()
p.player match {
case player: EntityPlayerMP => player.openContainer match {
case container: container.Server => container.server match {
case Some(server) if server == readServer =>
trySetComputerPower(server.machine, setPower, player)
case _ => logForgedPacket(player)
}
case _ => logForgedPacket(player)
}
case _ => // Invalid packet.
}
}
Expand Down Expand Up @@ -122,16 +141,17 @@ object PacketHandler extends CommonPacketHandler {
def onDronePower(p: PacketParser): Unit = {
val entity = p.readEntity[Drone]()
val power = p.readBoolean()
entity match {
case Some(drone) => p.player match {
case player: EntityPlayerMP =>
p.player match {
case player: EntityPlayerMP => (player.openContainer, entity) match {
case (c: container.Drone, Some(readDrone)) if c.drone == readDrone =>
val drone = c.drone
if (power) {
drone.preparePowerUp()
}
trySetComputerPower(drone.machine, power, player)
case _ =>
case _ => logForgedPacket(player)
}
case _ => // Invalid packet.
case _ =>
}
}

Expand Down Expand Up @@ -249,13 +269,14 @@ object PacketHandler extends CommonPacketHandler {
val mountableIndex = p.readInt()
val nodeIndex = p.readInt()
val side = p.readDirection()
entity match {
case Some(t) => p.player match {
case player: EntityPlayerMP if t.isUseableByPlayer(player) =>
t.connect(mountableIndex, nodeIndex - 1, side)
case _ =>
p.player match {
case player: EntityPlayerMP => (player.openContainer, entity) match {
case (container: container.Rack, Some(readRack)) if readRack == container.rack =>
if (container.rack.isUseableByPlayer(player))
container.rack.connect(mountableIndex, nodeIndex - 1, side)
case _ => logForgedPacket(player)
}
case _ => // Invalid packet.
case _ =>
}
}

Expand Down

0 comments on commit 050a0ae

Please sign in to comment.