Skip to content

Commit

Permalink
Eliminate serv.supportFeature
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavin John committed Jan 4, 2024
1 parent 623bd68 commit 0aa4a58
Show file tree
Hide file tree
Showing 20 changed files with 80 additions and 71 deletions.
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class MCServer extends EventEmitter {
throw new Error(`Server version '${serverPingVersion}' is not supported. Oldest supported version is '${oldestSupportedVersion}'.`)
}

this.supportFeature = feature => mcData.supportFeature(feature)
this.commands = new Command({})
this._server = createServer(options)

Expand Down
4 changes: 2 additions & 2 deletions src/lib/plugins/blockUpdates.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ class ChunkUpdates {
}

module.exports.server = (serv, { version }) => {
multiBlockChangeHasTrustEdges = serv.supportFeature('multiBlockChangeHasTrustEdges')

const mcData = require('minecraft-data')(version)

multiBlockChangeHasTrustEdges = mcData.supportFeature('multiBlockChangeHasTrustEdges')

serv.MAX_UPDATES_PER_TICK = 10000

// Each world has its own block update queue
Expand Down
4 changes: 2 additions & 2 deletions src/lib/plugins/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { skipMcPrefix } = require('../utils')

const Vec3 = require('vec3').Vec3

module.exports.player = function (player, serv) {
module.exports.player = function (player, serv, { version }) {
player.changeBlock = async (position, blockType, blockData) => {
serv.players
.filter(p => p.world === player.world && player !== p)
Expand Down Expand Up @@ -74,7 +74,7 @@ module.exports.server = function (serv, { version }) {
const blockParam = params[4]
const id = isNaN(+blockParam) ? mcData.blocksByName[skipMcPrefix(blockParam)]?.id : +blockParam
const data = parseInt(params[5] || 0, 10)
const stateId = serv.supportFeature('theFlattening') ? (blocks[id].minStateId + data) : (id << 4 | data)
const stateId = mcData.supportFeature('theFlattening') ? (blocks[id].minStateId + data) : (id << 4 | data)

if (ctx.player) ctx.player.setBlock(new Vec3(res[0], res[1], res[2]).floored(), stateId)
else serv.setBlock(serv.overworld, new Vec3(res[0], res[1], res[2]).floored(), stateId)
Expand Down
4 changes: 2 additions & 2 deletions src/lib/plugins/chest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports.server = function (serv, { version }) {
// TODO: Large chest (NOT IMPLEMENTED)
// Shulker boxes
let blockShulkerBox = []
if (serv.supportFeature('theShulkerBoxes')) {
if (mcData.supportFeature('theShulkerBoxes')) {
blockShulkerBox = [mcData.blocksByName.shulker_box.id, mcData.blocksByName.red_shulker_box.id,
mcData.blocksByName.orange_shulker_box.id, mcData.blocksByName.yellow_shulker_box.id, mcData.blocksByName.lime_shulker_box.id,
mcData.blocksByName.green_shulker_box.id, mcData.blocksByName.light_blue_shulker_box.id, mcData.blocksByName.cyan_shulker_box.id,
Expand Down Expand Up @@ -99,7 +99,7 @@ module.exports.server = function (serv, { version }) {
serv.onBlockInteraction(mcData.blocksByName.ender_chest.name, containerBlockInteractionHandler)
// TODO: Large chest (NOT IMPLEMENTED)
// Shulker boxes
if (serv.supportFeature('theShulkerBoxes')) {
if (mcData.supportFeature('theShulkerBoxes')) {
for (const currentShulkerBoxID of blockShulkerBox) {
serv.onBlockInteraction(mcData.blocks[currentShulkerBoxID].name, containerBlockInteractionHandler)
}
Expand Down
10 changes: 5 additions & 5 deletions src/lib/plugins/digging.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ module.exports.player = function (player, serv, { version }) {
})
}
}
if (serv.supportFeature('acknowledgePlayerDigging')) {
if (mcData.supportFeature('acknowledgePlayerDigging')) {
player._client.write('acknowledge_player_digging', {
location,
block: currentlyDugBlock.stateId,
Expand All @@ -123,7 +123,7 @@ module.exports.player = function (player, serv, { version }) {
location,
destroyStage: -1
})
if (serv.supportFeature('acknowledgePlayerDigging')) {
if (mcData.supportFeature('acknowledgePlayerDigging')) {
player._client.write('acknowledge_player_digging', {
location,
block: currentlyDugBlock.stateId,
Expand Down Expand Up @@ -157,7 +157,7 @@ module.exports.player = function (player, serv, { version }) {
drops.push({
...dropBase,
blockDropVelocity: new Vec3(Math.random() * 4 - 2, Math.random() * 2 + 2, Math.random() * 4 - 2),
blockDropId: serv.supportFeature('theFlattening') ? currentlyDugBlock.drops[0] : currentlyDugBlock.type
blockDropId: mcData.supportFeature('theFlattening') ? currentlyDugBlock.drops[0] : currentlyDugBlock.type
})
} else {
const heldItem = player.inventory.slots[36 + player.heldItemSlot]
Expand Down Expand Up @@ -185,7 +185,7 @@ module.exports.player = function (player, serv, { version }) {
if (data.dropBlock) {
drops.forEach(drop => dropBlock(drop))
}
if (serv.supportFeature('acknowledgePlayerDigging')) {
if (mcData.supportFeature('acknowledgePlayerDigging')) {
player._client.write('acknowledge_player_digging', {
location,
block: 0,
Expand All @@ -199,7 +199,7 @@ module.exports.player = function (player, serv, { version }) {
location,
type: currentlyDugBlock.type << 4
})
if (serv.supportFeature('acknowledgePlayerDigging')) {
if (mcData.supportFeature('acknowledgePlayerDigging')) {
player._client.write('acknowledge_player_digging', {
location,
block: currentlyDugBlock.stateId,
Expand Down
5 changes: 3 additions & 2 deletions src/lib/plugins/inventory.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Vec3 = require('vec3')

module.exports.player = function (player, serv, { version }) {
const mcData = require('minecraft-data')(version)
const Item = require('prismarine-item')(version)
const windows = require('prismarine-windows')(version)

Expand All @@ -12,7 +13,7 @@ module.exports.player = function (player, serv, { version }) {
player.heldItemSlot = slotId
player.setEquipment(0, player.inventory.slots[36 + player.heldItemSlot])

if (serv.supportFeature('allEntityEquipmentInOne')) {
if (mcData.supportFeature('allEntityEquipmentInOne')) {
player._writeOthersNearby('entity_equipment', {
entityId: player.id,
equipments: [{
Expand Down Expand Up @@ -180,7 +181,7 @@ module.exports.player = function (player, serv, { version }) {
equipments[player.heldItemSlot] = 0
if (equipments[slot] !== undefined) {
player.setEquipment(equipments[slot], newItem)
if (serv.supportFeature('allEntityEquipmentInOne')) {
if (mcData.supportFeature('allEntityEquipmentInOne')) {
player._writeOthersNearby('entity_equipment', {
entityId: player.id,
equipments: [{
Expand Down
6 changes: 3 additions & 3 deletions src/lib/plugins/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ module.exports.player = async function (player, serv, settings) {
worldNames: Object.values(serv.dimensionNames),
dimensionCodec: mcData.loginPacket?.dimensionCodec,
worldName: serv.dimensionNames[0],
dimension: (serv.supportFeature('dimensionIsAString') || serv.supportFeature('dimensionIaAWorld')) ? mcData.loginPacket.dimension : 0,
dimension: (mcData.supportFeature('dimensionIsAString') || mcData.supportFeature('dimensionIaAWorld')) ? mcData.loginPacket.dimension : 0,
hashedSeed: serv.hashedSeed,
difficulty: serv.difficulty,
viewDistance: settings['view-distance'],
Expand All @@ -102,7 +102,7 @@ module.exports.player = async function (player, serv, settings) {
isDebug: false,
isFlat: settings.generation?.name === 'superflat'
})
if (serv.supportFeature('difficultySentSeparately')) {
if (mcData.supportFeature('difficultySentSeparately')) {
player._client.write('difficulty', {
difficulty: serv.difficulty,
difficultyLocked: false
Expand All @@ -113,7 +113,7 @@ module.exports.player = async function (player, serv, settings) {
function sendChunkWhenMove () {
player.on('move', () => {
if (!player.sendingChunks && player.position.distanceTo(player.lastPositionChunkUpdated) > 16) { player.sendRestMap() }
if (!serv.supportFeature('updateViewPosition')) {
if (!mcData.supportFeature('updateViewPosition')) {
return
}
const chunkX = Math.floor(player.position.x / 16)
Expand Down
5 changes: 3 additions & 2 deletions src/lib/plugins/physics.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const Vec3 = require('vec3').Vec3

module.exports.entity = function (entity, serv, { version }) {
const blocks = require('minecraft-data')(version).blocks
const mcData = require('minecraft-data')(version)
const { blocks } = mcData

entity.calculatePhysics = async (delta) => {
if (entity.gravity) {
Expand Down Expand Up @@ -41,7 +42,7 @@ module.exports.entity = function (entity, serv, { version }) {
const velocity = vel
const maxVelocity = maxVel
let scaledVelocity = velocity.scaled(8000 / 20) // from fixed-position/second to unit => 1/8000 blocks per tick
if (serv.supportFeature('fixedPointPosition')) {
if (mcData.supportFeature('fixedPointPosition')) {
scaledVelocity = scaledVelocity.scaled(1 / 32)
}
scaledVelocity = scaledVelocity.floored()
Expand Down
6 changes: 3 additions & 3 deletions src/lib/plugins/placeBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports.server = (serv, { version }) => {
itemPlaceHandlers.set(item.id, handler)
}

if (serv.supportFeature('theFlattening')) {
if (mcData.supportFeature('theFlattening')) {
const parseValue = (value, state) => {
if (state.type === 'enum') {
return state.values.indexOf(value)
Expand Down Expand Up @@ -110,7 +110,7 @@ module.exports.player = function (player, serv, { version }) {
const dz = player.position.z - (placedPosition.z + 0.5)
const angle = Math.atan2(dx, -dz) * 180 / Math.PI + 180 // Convert to [0,360[

if (serv.supportFeature('blockPlaceHasIntCursor')) cursorY /= 16
if (mcData.supportFeature('blockPlaceHasIntCursor')) cursorY /= 16

let half = cursorY > 0.5 ? 'top' : 'bottom'
if (direction === 0) half = 'top'
Expand Down Expand Up @@ -149,7 +149,7 @@ module.exports.player = function (player, serv, { version }) {
}
}

const stateId = serv.supportFeature('theFlattening') ? (blocks[id].minStateId + data) : (id << 4 | data)
const stateId = mcData.supportFeature('theFlattening') ? (blocks[id].minStateId + data) : (id << 4 | data)
player.setBlock(placedPosition, stateId)
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/plugins/portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports.player = function (player, serv, { version }) {
const mcData = require('minecraft-data')(version)

const obsidianType = mcData.blocksByName.obsidian.id
const portalType = serv.supportFeature('theFlattening') ? mcData.blocksByName.nether_portal.id : mcData.blocksByName.portal.id
const portalType = mcData.supportFeature('theFlattening') ? mcData.blocksByName.nether_portal.id : mcData.blocksByName.portal.id

player.on('dug', ({ position, block }) => {
function destroyPortal (portal, positionAlreadyDone = null) {
Expand Down Expand Up @@ -39,7 +39,7 @@ module.exports.server = function (serv, { version }) {

let portalX
let portalZ
if (serv.supportFeature('theFlattening')) {
if (mcData.supportFeature('theFlattening')) {
const portalBlock = mcData.blocksByName.nether_portal
portalX = portalBlock.minStateId
portalZ = portalBlock.minStateId + 1
Expand Down
18 changes: 9 additions & 9 deletions src/lib/plugins/redstone.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ module.exports.server = function (serv, { version }) {
const redstoneTorchType = mcData.blocksByName.redstone_torch.id

let poweredRepeaterType, unpoweredRepeaterType, unlitRedstoneTorchType, repeaterType
if (!serv.supportFeature('theFlattening')) {
if (!mcData.supportFeature('theFlattening')) {
unlitRedstoneTorchType = mcData.blocksByName.unlit_redstone_torch.id
poweredRepeaterType = mcData.blocksByName.powered_repeater.id
unpoweredRepeaterType = mcData.blocksByName.unpowered_repeater.id
} else { repeaterType = mcData.blocksByName.repeater.id }
const powerLevel = (block, dir) => {
if (block.type === redstoneWireType) return block.metadata
if (block.type === redstoneTorchType) return 15
if (serv.supportFeature('theFlattening')) {
if (mcData.supportFeature('theFlattening')) {
// to do
} else {
// the if below is missing a check whether repeater is powered or not
Expand All @@ -37,7 +37,7 @@ module.exports.server = function (serv, { version }) {
if (block.type === redstoneWireType) {
if (dir.y === 1 || await isWireDirectedIn(world, pos, dir.scaled(-1))) { return block.metadata }
}
if (serv.supportFeature('theFlattening')) {
if (mcData.supportFeature('theFlattening')) {
// to-do
} else {
if (block.type === poweredRepeaterType) {
Expand Down Expand Up @@ -75,7 +75,7 @@ module.exports.server = function (serv, { version }) {
}

const isDirectedRepeater = (block, dir, powered = false) => {
if (serv.supportFeature('theFlattening')) {
if (mcData.supportFeature('theFlattening')) {
// TO-DO
return false
} else {
Expand Down Expand Up @@ -138,7 +138,7 @@ module.exports.server = function (serv, { version }) {
player.setBlock(block.position, block.type, data)
return true
}
if (!serv.supportFeature('theFlattening')) {
if (!mcData.supportFeature('theFlattening')) {
serv.onBlockInteraction('powered_repeater', repeaterInteraction)
serv.onBlockInteraction('unpowered_repeater', repeaterInteraction)
}
Expand Down Expand Up @@ -188,7 +188,7 @@ module.exports.server = function (serv, { version }) {

return changed
}
if (!serv.supportFeature('theFlattening')) {
if (!mcData.supportFeature('theFlattening')) {
serv.onBlockUpdate('unlit_redstone_torch', updateRedstoneTorch)
}
serv.onBlockUpdate('redstone_torch', updateRedstoneTorch)
Expand Down Expand Up @@ -235,10 +235,10 @@ module.exports.server = function (serv, { version }) {

let changed = false
if ((block.type === poweredRepeaterType || block.type === repeaterType) && p === 0) {
if (serv.supportFeature('theFlattening')) { block.metadata.powered = false } else { await world.setBlockType(pos, unpoweredRepeaterType) }
if (mcData.supportFeature('theFlattening')) { block.metadata.powered = false } else { await world.setBlockType(pos, unpoweredRepeaterType) }
changed = true
} else if ((block.type === unpoweredRepeaterType || block.type === repeaterType) && p !== 0) {
if (serv.supportFeature('theFlattening')) { block.metadata.powered = true } else { await world.setBlockType(pos, poweredRepeaterType) }
if (mcData.supportFeature('theFlattening')) { block.metadata.powered = true } else { await world.setBlockType(pos, poweredRepeaterType) }
changed = true
}

Expand All @@ -250,7 +250,7 @@ module.exports.server = function (serv, { version }) {

return changed
}
if (!serv.supportFeature('theFlattening')) {
if (!mcData.supportFeature('theFlattening')) {
serv.onBlockUpdate('powered_repeater', updateRepeater)
serv.onBlockUpdate('unpowered_repeater', updateRepeater)
}
Expand Down
10 changes: 6 additions & 4 deletions src/lib/plugins/respawn.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
module.exports.player = function (player, serv) {
module.exports.player = function (player, serv, { version }) {
const mcData = require('minecraft-data')(version)

player._client.on('client_command', (data) => {
let actionId

if (serv.supportFeature('respawnIsPayload')) {
if (mcData.supportFeature('respawnIsPayload')) {
actionId = data.payload
} else if (serv.supportFeature('respawnIsActionId')) {
} else if (mcData.supportFeature('respawnIsActionId')) {
actionId = data.actionId
}

if (actionId === 0) {
player.behavior('requestRespawn', {}, () => {
player._client.write('respawn', {
previousGameMode: player.prevGameMode,
dimension: (serv.supportFeature('dimensionIsAString') || serv.supportFeature('dimensionIaAWorld')) ? serv.dimensionNames[0] : 0,
dimension: (mcData.supportFeature('dimensionIsAString') || mcData.supportFeature('dimensionIaAWorld')) ? serv.dimensionNames[0] : 0,
worldName: serv.dimensionNames[0],
difficulty: serv.difficulty,
hashedSeed: serv.hashedSeed,
Expand Down
6 changes: 3 additions & 3 deletions src/lib/plugins/signs.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module.exports.server = (serv, { version }) => {
const mcData = require('minecraft-data')(version)

const oakSign = serv.supportFeature('theFlattening') ? mcData.blocksByName.oak_sign : mcData.blocksByName.standing_sign
const oakSign = mcData.supportFeature('theFlattening') ? mcData.blocksByName.oak_sign : mcData.blocksByName.standing_sign
const oakWallSign = mcData.blocksByName.wall_sign

serv.on('asap', () => {
if (serv.supportFeature('theFlattening')) {
if (mcData.supportFeature('theFlattening')) {
const placeHandler = ({ player, placedPosition, direction, properties }) => {
if (direction === 0) return { id: -1, data: 0 }
let block = oakSign
Expand All @@ -21,7 +21,7 @@ module.exports.server = (serv, { version }) => {
const data = serv.setBlockDataProperties(block.defaultState - block.minStateId, block.states, properties)
return { id: block.id, data }
}
if (serv.supportFeature('multiTypeSigns')) {
if (mcData.supportFeature('multiTypeSigns')) {
const signTypes = ['oak_sign', 'spruce_sign', 'birch_sign', 'acacia_sign', 'jungle_sign', 'dark_oak_sign']
signTypes.forEach(type => serv.onItemPlace(type, placeHandler))
} else {
Expand Down
Loading

0 comments on commit 0aa4a58

Please sign in to comment.