forbid interaction if out of world

This commit is contained in:
Bixilon 2022-08-22 22:28:43 +02:00
parent 612d02e253
commit 3cda1d88a1
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
8 changed files with 53 additions and 39 deletions

View File

@ -92,7 +92,6 @@ class CampfireBlockEntity(connection: PlayConnection) : BlockEntity(connection)
for (i in 0 until 4) { for (i in 0 until 4) {
connection.world.addParticle(SmokeParticle(connection, position, Vec3d(0.0, 5.0E-4, 0.0))) connection.world.addParticle(SmokeParticle(connection, position, Vec3d(0.0, 5.0E-4, 0.0)))
} }
} }
} }

View File

@ -13,6 +13,7 @@
package de.bixilon.minosoft.data.registries.items.block package de.bixilon.minosoft.data.registries.items.block
import de.bixilon.kotlinglm.vec3.Vec3i
import de.bixilon.kutil.cast.CastUtil.unsafeNull import de.bixilon.kutil.cast.CastUtil.unsafeNull
import de.bixilon.kutil.concurrent.pool.DefaultThreadPool import de.bixilon.kutil.concurrent.pool.DefaultThreadPool
import de.bixilon.minosoft.data.abilities.Gamemodes import de.bixilon.minosoft.data.abilities.Gamemodes
@ -41,10 +42,10 @@ open class BlockItem(
override fun interactBlock(connection: PlayConnection, target: BlockTarget, hand: Hands, stack: ItemStack): InteractionResults { override fun interactBlock(connection: PlayConnection, target: BlockTarget, hand: Hands, stack: ItemStack): InteractionResults {
if (!connection.player.gamemode.canBuild) { if (!connection.player.gamemode.canBuild) {
return InteractionResults.PASS return InteractionResults.ERROR
} }
val placePosition = target.blockPosition val placePosition = Vec3i(target.blockPosition)
if (!target.blockState.material.replaceable) { if (!target.blockState.material.replaceable) {
placePosition += target.direction placePosition += target.direction
@ -54,7 +55,7 @@ open class BlockItem(
} }
if (!connection.world.isPositionChangeable(placePosition)) { if (!connection.world.isPositionChangeable(placePosition)) {
return InteractionResults.PASS return InteractionResults.ERROR
} }
if (connection.world.getBlockEntity(placePosition) != null && !connection.player.isSneaking) { if (connection.world.getBlockEntity(placePosition) != null && !connection.player.isSneaking) {
@ -71,7 +72,7 @@ open class BlockItem(
val collisionShape = placeBlockState.collisionShape + placePosition val collisionShape = placeBlockState.collisionShape + placePosition
if (connection.world.entities.isEntityIn(collisionShape)) { if (connection.world.entities.isEntityIn(collisionShape)) {
return InteractionResults.CONSUME return InteractionResults.ERROR
} }

View File

@ -208,8 +208,12 @@ class World(
if (border.isOutside(blockPosition)) { if (border.isOutside(blockPosition)) {
return false return false
} }
return isValidPosition(blockPosition)
}
fun isValidPosition(blockPosition: Vec3i): Boolean {
val dimension = connection.world.dimension!! val dimension = connection.world.dimension!!
return (blockPosition.y >= dimension.minY || blockPosition.y < dimension.maxY) return (blockPosition.y >= dimension.minY && blockPosition.y < dimension.maxY)
} }
fun forceSetBlockState(blockPosition: Vec3i, blockState: BlockState?, check: Boolean = false) { fun forceSetBlockState(blockPosition: Vec3i, blockState: BlockState?, check: Boolean = false) {

View File

@ -72,9 +72,9 @@ open class SectionDataProvider<T>(
} }
var count = 0 var count = 0
var minX = 16 var minX = ProtocolDefinition.SECTION_WIDTH_X
var minY = 16 var minY = ProtocolDefinition.SECTION_HEIGHT_Y
var minZ = 16 var minZ = ProtocolDefinition.SECTION_WIDTH_Z
var maxX = 0 var maxX = 0
var maxY = 0 var maxY = 0
@ -112,8 +112,8 @@ open class SectionDataProvider<T>(
if (z > maxZ) { if (z > maxZ) {
maxZ = z maxZ = z
} }
} }
this.minPosition = Vec3i(minX, minY, minZ) this.minPosition = Vec3i(minX, minY, minZ)
this.maxPosition = Vec3i(maxX, maxY, maxZ) this.maxPosition = Vec3i(maxX, maxY, maxZ)
this.count = count this.count = count
@ -219,6 +219,6 @@ open class SectionDataProvider<T>(
companion object { companion object {
private val EMPTY_ITERATOR = listOf<Any>().iterator() private val EMPTY_ITERATOR = emptyArray<Any>().iterator()
} }
} }

View File

@ -50,11 +50,11 @@ class InteractInteractionHandler(
fun init() { fun init() {
renderWindow.inputHandler.registerCheckCallback(USE_ITEM_KEYBINDING to KeyBinding( renderWindow.inputHandler.registerCheckCallback(
mapOf( USE_ITEM_KEYBINDING to KeyBinding(
KeyActions.CHANGE to setOf(KeyCodes.MOUSE_BUTTON_RIGHT), KeyActions.CHANGE to setOf(KeyCodes.MOUSE_BUTTON_RIGHT),
), )
)) )
} }
fun stopUsingItem() { fun stopUsingItem() {
@ -80,14 +80,15 @@ class InteractInteractionHandler(
} }
// if out of world (border): return CONSUME // if out of world (border): return CONSUME
var result: InteractionResults = InteractionResults.PASS
try { try {
if (connection.player.gamemode == Gamemodes.SPECTATOR) { if (connection.player.gamemode == Gamemodes.SPECTATOR) {
return InteractionResults.SUCCESS return InteractionResults.SUCCESS
} }
val result = target.blockState.block.onUse(connection, target, hand, stack) result = target.blockState.block.onUse(connection, target, hand, stack)
if (result == InteractionResults.SUCCESS) { if (result != InteractionResults.PASS) {
return InteractionResults.SUCCESS return result
} }
if (stack == null) { if (stack == null) {
@ -97,17 +98,22 @@ class InteractInteractionHandler(
return InteractionResults.PASS // ToDo: Check return InteractionResults.PASS // ToDo: Check
} }
return stack.item.item.interactBlock(connection, target, hand, stack) result = stack.item.item.interactBlock(connection, target, hand, stack)
} finally { } finally {
connection.sendPacket(BlockInteractC2SP( if (result != InteractionResults.ERROR) {
position = target.blockPosition, connection.sendPacket(
direction = target.direction, BlockInteractC2SP(
cursorPosition = Vec3(target.hitPosition), position = target.blockPosition,
item = stack, direction = target.direction,
hand = hand, cursorPosition = Vec3(target.hitPosition),
insideBlock = false, // ToDo: insideBlock item = stack,
)) hand = hand,
insideBlock = false, // ToDo: insideBlock
)
)
}
} }
return result
} }
fun interactEntityAt(target: EntityTarget, hand: Hands): InteractionResults { fun interactEntityAt(target: EntityTarget, hand: Hands): InteractionResults {
@ -187,7 +193,11 @@ class InteractInteractionHandler(
return return
} }
} }
is BlockTarget -> { is BlockTarget -> {
if (!connection.world.isValidPosition(target.blockPosition)) {
return
}
val result = interactBlock(target, item, hand) val result = interactBlock(target, item, hand)
if (result == InteractionResults.SUCCESS) { if (result == InteractionResults.SUCCESS) {
interactionManager.swingHand(hand) interactionManager.swingHand(hand)

View File

@ -1,6 +1,6 @@
/* /*
* Minosoft * Minosoft
* Copyright (C) 2021 Moritz Zwerger * Copyright (C) 2020-2022 Moritz Zwerger
* *
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* *
@ -28,5 +28,11 @@ enum class InteractionResults {
* Nothing happens from block side (e.g. right clicking on dirt). You can maybe place a block, whatever * Nothing happens from block side (e.g. right clicking on dirt). You can maybe place a block, whatever
*/ */
PASS, PASS,
/**
* Like consume, but with an error (no packet will be sent to the server)
*/
ERROR,
; ;
} }

View File

@ -133,8 +133,8 @@ class TabListS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
entity.tabListItem = tabListItem entity.tabListItem = tabListItem
} }
} }
connection.fireEvent(TabListEntryChangeEvent(connection, this)) connection.fireEvent(TabListEntryChangeEvent(connection, this))
} }

View File

@ -23,7 +23,6 @@ import java.util.regex.Pattern;
public final class ProtocolDefinition { public final class ProtocolDefinition {
public static final int STRING_MAX_LENGTH = 32767; public static final int STRING_MAX_LENGTH = 32767;
public static final int DEFAULT_PORT = 25565; public static final int DEFAULT_PORT = 25565;
public static final int SOCKET_CONNECT_TIMEOUT = 5000;
public static final int SOCKET_TIMEOUT = 30000; public static final int SOCKET_TIMEOUT = 30000;
public static final int STATUS_PROTOCOL_PACKET_MAX_SIZE = 1 << 16; public static final int STATUS_PROTOCOL_PACKET_MAX_SIZE = 1 << 16;
public static final float ANGLE_CALCULATION_CONSTANT = 360.0F / 256.0F; public static final float ANGLE_CALCULATION_CONSTANT = 360.0F / 256.0F;
@ -51,10 +50,7 @@ public final class ProtocolDefinition {
public static final Pattern MINECRAFT_NAME_VALIDATOR = Pattern.compile("\\w{3,16}"); public static final Pattern MINECRAFT_NAME_VALIDATOR = Pattern.compile("\\w{3,16}");
public static final Pattern RESOURCE_LOCATION_PATTERN = Pattern.compile("([a-z_0-9]+:)?[a-zA-Z_0-9.]+");
public static final Pattern SCOREBOARD_OBJECTIVE_PATTERN = Pattern.compile("[a-zA-z-.+]{1,16}");
public static final int SECTION_SIZE = 16;
public static final int SECTION_WIDTH_X = 16; public static final int SECTION_WIDTH_X = 16;
public static final int SECTION_MAX_X = SECTION_WIDTH_X - 1; public static final int SECTION_MAX_X = SECTION_WIDTH_X - 1;
public static final int SECTION_WIDTH_Z = 16; public static final int SECTION_WIDTH_Z = 16;
@ -92,17 +88,15 @@ public final class ProtocolDefinition {
public static final float TICKS_PER_DAYf = (float) TICKS_PER_DAY; public static final float TICKS_PER_DAYf = (float) TICKS_PER_DAY;
public static final byte LIGHT_LEVELS = 16; public static final byte LIGHT_LEVELS = 16;
public static final byte MAX_LIGHT_LEVEL = LIGHT_LEVELS - 1;
static { static {
// java does (why ever) not allow to directly assign a null InetAddress inetAddress;
InetAddress tempInetAddress;
try { try {
tempInetAddress = InetAddress.getByName(LAN_SERVER_BROADCAST_ADDRESS); inetAddress = InetAddress.getByName(LAN_SERVER_BROADCAST_ADDRESS);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
tempInetAddress = null; inetAddress = null;
} }
LAN_SERVER_BROADCAST_INET_ADDRESS = tempInetAddress; LAN_SERVER_BROADCAST_INET_ADDRESS = inetAddress;
} }
} }