Skip to content

Instantly share code, notes, and snippets.

@demipixel
Last active Nov 6, 2015
Embed
What would you like to do?
Reddit Builds a House (flying-squid pseudo code)
module.exports = {
player: playerInject,
server: serverInject,
disable: disable
}
function playerInject(serv, player, self) {
var pData = player.plugin(self); // Player Data
pData.blocklist = [];
pData.brokenlist = [];
pData.placed = false;
player.world = self.redditworld;
player.on('login', () => { // Say hey to the user!
player.chat('Welcome to "Reddit Builds a House"!');
player.chat('Place and destroy blocks as you need to (it will not affect the world)!');
player.chat('Use /ready when you\'re ready or /reset if you want to, well... reset.');
player.chat('Use /info for more crazy info about how this server was made (Not Bukkit or Spigot!)');
});
player.on('place_block_cancel', async ({id, damage, block, position, reference, cancel}) => {
if (player.world != self.redditworld) return; // Only in reddit world
cancel(); // Don't do default action of sending to all users and saving to world
var list = pData.blocklist; // List of loally placed blocks
list.push({ id: id, damage: damage, position: position}); // Add block to list
if (pData.ready) { // Unready if readied
pData.ready = false;
player.chat('Unready!');
}
if (list.length > 5) { // Remove the 6th oldest block (only 5 shown to user at a time)
var remove = list.splice(0, 1);
var id = await self.redditworld.getBlockType(position);
var data = await self.redditworld.getBlockData(position);
player.sendBlock(position, id, data);
}
}
player.on('block_dig_cancel', async ({position,status,cancel}) => {
if (player.world != self.redditworld) return;
if (status == 0 && player.gameMode == 1) { // Everybody is in creative so this immediately digs
cancel();
var list = pData.brokenlist; // List of locally destroyed blocks
list.push(position);
}
});
player.on('disconnect', () => {
if (pData.placed) {
player.ban('Thanks for stopping by! Check out the house on /r/Mineraft soon!');
}
});
player.commands.add({ // Resets all block so that player is in sync with the server (brokenlist and blocklist)
base: 'reset',
info: 'Reset all blocks',
usage: '/reset',
async action() {
var resetlist = pData.blocklist.map((b) => b.position)
.concat(pData.brokenlist);
for (var r in resetlist) {
var id = await self.redditworld.getBlockType(resetlist[r]);
var data = await self.redditworld.getBlockData(resetlist[r]);
player.sendBlock(resetlist[r], id, data);
}
pData.blocklist = [];
player.chat('Cleared blocks!');
if (pData.ready) {
player.chat('Unready!');
pData.ready = false;
}
}
});
player.commands.add({ // Removes all local-blocks except the latest block
base: 'ready',
info: 'Complete block placement',
usage: '/ready',
async action() {
if (!pData.blocklist || !pData.blocklist.length) {
player.chat('You must place a block in order to complete!');
return;
}
var resetlist = pData.blocklist.map((b) => b.position).filter((a, index) => index != pData.blocklist.length - 1)
.concat(pData.brokenlist); // Get everything EXCEPT the one block they're going to place
for (var r in resetlist) {
var id = await self.redditworld.getBlockType(resetlist[r]);
var data = await self.redditworld.getBlockData(resetlist[r]);
player.sendBlock(resetlist[r], id, data);
}
var list = pData.blocklist;
if (list.length > 1) list.splice(0, list.length - 2);
pData.breaklist = [];
pData.ready = true;
var choose = list[0];
player.chat('Are you use you want to place your block ' + choose.id + ':' + choose.data + ' at ' + choose.position.toString() + '?');
player.chat('Type "/confirm" to confirm.');
plaer.chat('Place another block if you would like to switch your choice');
}
});
player.commands.add({ // Confirms the latest block, places it
base: 'confirm',
info: 'Confirm your block placement',
usage: '/confirm',
action() {
if (!pData.ready) {
player.chat('First use "/ready" to check your block.');
return;
}
var choose = pData.blocklist[0];
serv.setBlock(self.redditworld, choose.position, choose.id, choose.data);
player.chat('Your block has been placed! You will be banned in 60 seconds or when you leave');
pData.placed = true;
setTimeout(() => {
if (!player.connected) return;
else player.ban('Thanks for stopping by! Check out the house on /r/Mineraft soon!');
});
}
}
player.commands.add({ // Info about this amazing server/plugin!
base: 'info',
info: 'Get info about this server',
usage: '/info',
action() {
player.chat('This server was made in node.js (a form of server-side javascript).');
player.chat('Everything is built from the packets up, meaning if runs faster and more efficiently than java servers.');
player.chat('You can easily make your own plugins or contribute to the project at:');
player.chat('https://github.com/mhsjlw/flying-squid');
}
});
}
function serverInject(serv, self) { // Add world and pregen a bit
self.redditworld = new World(someSuperflatGeneration({}));
serv.pregenWorld(self.redditworld);
}
function disable(self) { // player.self(plugin) gets removed as well as all listeners automagically
serv.playersInWorld(self.redditworld).forEach((p) => p.changeWorld(serv.overworld));
delete self.redditworld;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment