forked from PrismarineJS/flying-squid
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pickup up
- Loading branch information
Showing
11 changed files
with
205 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,111 @@ | ||
import MinecraftData from 'minecraft-data' | ||
import { Vec3 } from 'vec3' | ||
|
||
export const player = function (player: Player, serv: Server, { version }: Options) { | ||
// Importing necessary libraries | ||
const mcData = MinecraftData(version) | ||
// Getting ALL supported blocks | ||
// Chests | ||
const blockChest = mcData.blocksByName.chest | ||
const blockEnderChest = mcData.blocksByName.ender_chest | ||
// TODO: Large chest (NOT IMPLEMENTED) | ||
// Shulker boxes | ||
let blockShulkerBox = [] as any[] | ||
if (serv.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, | ||
mcData.blocksByName.blue_shulker_box.id, mcData.blocksByName.purple_shulker_box.id, mcData.blocksByName.magenta_shulker_box.id, | ||
mcData.blocksByName.brown_shulker_box.id, mcData.blocksByName.gray_shulker_box.id, mcData.blocksByName.light_gray_shulker_box.id, | ||
mcData.blocksByName.black_shulker_box.id, mcData.blocksByName.white_shulker_box.id, mcData.blocksByName.pink_shulker_box.id] | ||
} | ||
// Showing container GUI | ||
const openContainerGUI = (block, player, guiType, title, sound) => { | ||
// Default GUI shape and content | ||
const guiContent = [ | ||
{ present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, | ||
{ present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, | ||
{ present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }] | ||
const invType = 2 // 3 rows, 9 slots | ||
// Filling the GUI content and reshaping the GUI (if necessary) | ||
switch (guiType) { | ||
case 'chest': | ||
// TODO: Gathering of content (NOT IMPLEMENTED) | ||
break | ||
case 'double_chest': | ||
// TODO: Gathering of content (NOT IMPLEMENTED) | ||
break | ||
case 'ender_chest': | ||
// TODO: Gathering of content (NOT IMPLEMENTED) | ||
break | ||
case 'shulker_box': | ||
// TODO: Gathering of content (NOT IMPLEMENTED) | ||
break | ||
// TODO: Add more GUIs | ||
export const server = (serv: Server, { version }: Options) => { | ||
serv.once('asap', () => { | ||
// Importing necessary libraries | ||
const registry = require('prismarine-registry')(version) | ||
// Getting ALL supported blocks | ||
// Chests | ||
const blockChest = registry.blocksByName.chest | ||
const blockEnderChest = registry.blocksByName.ender_chest | ||
// TODO: Large chest (NOT IMPLEMENTED) | ||
// Shulker boxes | ||
let blockShulkerBox: string[] = [] | ||
if (registry.supportFeature('theShulkerBoxes')) { | ||
blockShulkerBox = [registry.blocksByName.shulker_box.id, registry.blocksByName.red_shulker_box.id, | ||
registry.blocksByName.orange_shulker_box.id, registry.blocksByName.yellow_shulker_box.id, registry.blocksByName.lime_shulker_box.id, | ||
registry.blocksByName.green_shulker_box.id, registry.blocksByName.light_blue_shulker_box.id, registry.blocksByName.cyan_shulker_box.id, | ||
registry.blocksByName.blue_shulker_box.id, registry.blocksByName.purple_shulker_box.id, registry.blocksByName.magenta_shulker_box.id, | ||
registry.blocksByName.brown_shulker_box.id, registry.blocksByName.gray_shulker_box.id, registry.blocksByName.light_gray_shulker_box.id, | ||
registry.blocksByName.black_shulker_box.id, registry.blocksByName.white_shulker_box.id, registry.blocksByName.pink_shulker_box.id] | ||
} | ||
if (sound) { | ||
// Playing provided sound | ||
serv.playSound(sound, player.world, block.position, {}) | ||
} | ||
// Opening container GUI window | ||
player._client.write('open_window', { | ||
windowId: player.windowId, | ||
inventoryType: invType, | ||
windowTitle: JSON.stringify(title) | ||
}) | ||
// Sending container content | ||
player._client.write('window_items', { | ||
windowId: player.windowId, | ||
stateId: 1, | ||
items: guiContent, | ||
carriedItem: { present: false } | ||
}) | ||
} | ||
// Interaction with a container block | ||
const containerBlockInteractionHandler = async ({ block, player }) => { | ||
if (player.crouching) return | ||
try { | ||
// Getting current block and block above it | ||
const id = await player.world.getBlockType(block.position) | ||
const blockAbove = await player.world.getBlockType(block.position.plus(new Vec3(0, 1, 0))) | ||
// If there is any block directly above container then we can't open it | ||
if (blockAbove) { return } | ||
// Dynamic window ID feature | ||
if (player.windowId === undefined) { player.windowId = 1 } else { player.windowId = player.windowId + 1 } | ||
player.windowPos = block.position | ||
if (id === blockChest.id) { | ||
player.windowType = 'chest' | ||
openContainerGUI(block, player, 'chest', { translate: 'container.chest' }, 'block.chest.open') | ||
return true | ||
// Showing container GUI | ||
const openContainerGUI = (block, player, guiType, title, sound) => { | ||
// Default GUI shape and content | ||
const guiContent = [ | ||
{ present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, | ||
{ present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, | ||
{ present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }] | ||
const invType = 2 // 3 rows, 9 slots | ||
// Filling the GUI content and reshaping the GUI (if necessary) | ||
switch (guiType) { | ||
case 'chest': | ||
// TODO: Gathering of content (NOT IMPLEMENTED) | ||
break | ||
case 'double_chest': | ||
// TODO: Gathering of content (NOT IMPLEMENTED) | ||
break | ||
case 'ender_chest': | ||
// TODO: Gathering of content (NOT IMPLEMENTED) | ||
break | ||
case 'shulker_box': | ||
// TODO: Gathering of content (NOT IMPLEMENTED) | ||
break | ||
// TODO: Add more GUIs | ||
} | ||
if (id === blockEnderChest.id) { | ||
player.windowType = 'ender_chest' | ||
openContainerGUI(block, player, 'ender_chest', { translate: 'container.enderchest' }, 'block.chest.open') | ||
return true | ||
if (sound) { | ||
// Playing provided sound | ||
serv.playSound(sound, player.world, block.position, {}) | ||
} | ||
// TODO: Large chest (NOT IMPLEMENTED) | ||
if (blockShulkerBox.includes(id)) { | ||
player.windowType = 'shulker_box' | ||
openContainerGUI(block, player, 'shulker_box', { translate: 'container.shulkerBox' }, 'block.chest.open') | ||
return true | ||
// Opening container GUI window | ||
player._client.write('open_window', { | ||
windowId: player.windowId, | ||
inventoryType: invType, | ||
windowTitle: JSON.stringify(title) | ||
}) | ||
// Sending container content | ||
player._client.write('window_items', { | ||
windowId: player.windowId, | ||
stateId: 1, | ||
items: guiContent, | ||
carriedItem: { present: false } | ||
}) | ||
} | ||
// Interaction with a container block | ||
const containerBlockInteractionHandler = async ({ block, player }) => { | ||
if (player.crouching) return | ||
try { | ||
// Getting current block and block above it | ||
const id = await player.world.getBlockType(block.position) | ||
const blockAbove = await player.world.getBlockType(block.position.plus(new Vec3(0, 1, 0))) | ||
// If there is any block directly above container then we can't open it | ||
if (blockAbove) { return } | ||
// Dynamic window ID feature | ||
if (player.windowId === undefined) { player.windowId = 1 } else { player.windowId = player.windowId + 1 } | ||
player.windowPos = block.position | ||
if (id === blockChest.id) { | ||
player.windowType = 'chest' | ||
openContainerGUI(block, player, 'chest', { translate: 'container.chest' }, 'block.chest.open') | ||
return true | ||
} | ||
if (id === blockEnderChest.id) { | ||
player.windowType = 'ender_chest' | ||
openContainerGUI(block, player, 'ender_chest', { translate: 'container.enderchest' }, 'block.chest.open') | ||
return true | ||
} | ||
// TODO: Large chest (NOT IMPLEMENTED) | ||
if (blockShulkerBox.includes(id)) { | ||
player.windowType = 'shulker_box' | ||
openContainerGUI(block, player, 'shulker_box', { translate: 'container.shulkerBox' }, 'block.chest.open') | ||
return true | ||
} | ||
} catch (err) { | ||
setTimeout(() => { throw err }, 0) | ||
} | ||
} catch (err) { | ||
setTimeout(() => { throw err }, 0) | ||
} | ||
} | ||
// Registering block interaction handlers | ||
// Chests | ||
serv.onBlockInteraction(mcData.blocksByName.chest.name, containerBlockInteractionHandler) | ||
serv.onBlockInteraction(mcData.blocksByName.ender_chest.name, containerBlockInteractionHandler) | ||
// TODO: Large chest (NOT IMPLEMENTED) | ||
// Shulker boxes | ||
if (serv.supportFeature('theShulkerBoxes')) { | ||
for (const currentShulkerBoxID of blockShulkerBox) { | ||
serv.onBlockInteraction(mcData.blocks[currentShulkerBoxID].name, containerBlockInteractionHandler) | ||
// Registering block interaction handlers | ||
// Chests | ||
serv.onBlockInteraction(registry.blocksByName.chest.name, containerBlockInteractionHandler) | ||
serv.onBlockInteraction(registry.blocksByName.ender_chest.name, containerBlockInteractionHandler) | ||
// TODO: Large chest (NOT IMPLEMENTED) | ||
// Shulker boxes | ||
if (registry.supportFeature('theShulkerBoxes')) { | ||
for (const currentShulkerBoxID of blockShulkerBox) { | ||
serv.onBlockInteraction(registry.blocks[currentShulkerBoxID].name, containerBlockInteractionHandler) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
declare global { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.