Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
Merge pull request #5 from Lapis256/beta/1.18.20
Browse files Browse the repository at this point in the history
Beta/1.18.20
  • Loading branch information
Lapis256 authored Jan 28, 2022
2 parents 01ebd79 + 4ec7b40 commit 1675093
Show file tree
Hide file tree
Showing 20 changed files with 287 additions and 172 deletions.
18 changes: 18 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*export { Event } from "./event.js";
export { GameRule } from "./gamerule.js";
export { Command } from "./command.js";
export { World } from "./world.js";
export { Tick } from "./tick.js";
export { Tag } from "./tag.js";
export { Player } from "./player.js";
export { Scoreboard } from "./scoreboard.js";
export { EventEmitter } from "./eventEmitter.js";*/

import "./src/commandInitializer.js";

export * from "./src/utils/index.js";
export * from "./src/debug/index.js";
export { Tick } from "./src/tick.js";
export { Command } from "./src/command.js";
export { Dimension } from "./src/dimension.js";
export { RawTextBuilder } from "./src/rawTextBuilder/index.js";
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "",
"main": "src/index.js",
"scripts": {
"build": "webpack"
"build": "webpack --config webpack.config.prod.js",
"dev": "webpack --config webpack.config.dev.js"
},
"author": "Lapis256",
"license": "MIT",
Expand Down
9 changes: 9 additions & 0 deletions src/commandInitializer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { world } from "mojang-minecraft";

void function() {
const func = (ev) => {
ev.player.runCommand("list");
world.events.playerJoin.unsubscribe(func);
}
world.events.playerJoin.subscribe(func);
}();
2 changes: 2 additions & 0 deletions src/debug/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { print, pprint, p, pp } from "./print.js";
export { default as toJson } from "./toJson.js";
28 changes: 28 additions & 0 deletions src/debug/print.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { world } from "mojang-minecraft";

import toJson from "./toJson.js";
import { RawTextBuilder } from "../rawTextBuilder/index.js";


export function print(...obj) {
const rawtext = new RawTextBuilder().addText(obj.map(String).join(" "));
const dimension = world.getDimension("overworld");
dimension.runCommand("tellraw @a " + rawtext.buildJson());
}


export function pprint(...obj) {
print(...obj.map(o => toJson(o, 4)));
}


export function p(obj) {
print(obj);
return obj;
}


export function pp(obj) {
pprint(obj);
return obj;
}
80 changes: 40 additions & 40 deletions src/utils/string.js → src/debug/toJson.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
function isClass(obj) {
return obj.toString().startsWith("class ");
}

function isGenerator(obj) {
return obj[Symbol.iterator] &&
obj[Symbol.iterator].name === "[Symbol.iterator]" &&
typeof obj.next === "function";
}

export function toJson(data, indent = 4) {
return JSON.stringify(data, (key, value) => {
switch(typeof value) {
case "function":
if(isClass(value)) {
return `[class ${value.name || key}]`;
}
return `[function ${value.name || key}]`;

case "object":
if(isGenerator(value)) {
return `[generator ${key || "Generator"}]`;
}
if(Array.isArray(value)) {
return value;
}
let obj = {};
for(const i in value) {
obj[i] = value[i];
}
return obj;

case "undefined":
return null;

default:
return value;
}
}, indent);
}
function isClass(obj) {
return obj.toString().startsWith("class ");
}

function isGenerator(obj) {
return obj[Symbol.iterator] &&
obj[Symbol.iterator].name === "[Symbol.iterator]" &&
typeof obj.next === "function";
}

export default function toJson(data, indent = 4) {
return JSON.stringify(data, (key, value) => {
switch(typeof value) {
case "function":
if(isClass(value)) {
return `[class ${value.name || key}]`;
}
return `[function ${value.name || key}]`;

case "object":
if(isGenerator(value)) {
return `[generator ${key || "Generator"}]`;
}
if(Array.isArray(value)) {
return value;
}
let obj = {};
for(const i in value) {
obj[i] = value[i];
}
return obj;

case "undefined":
return null;

default:
return value;
}
}, indent);
}
42 changes: 24 additions & 18 deletions src/dimension.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { Command } from "./command.js";
import { mergeObject } from "./object.js";


export class Dimension {
#dimension;

constructor(dimension) {
this.#dimension = dimension;
mergeObject(this, dimension);
}
commandRun(command) {
return Command.run(command, this.#dimension);
}
commandRunSafe(command) {
return Command.runSafe(command, this.#dimension);
}
}
import { world } from "mojang-minecraft";


const dimensions = [
"overworld",
"nether",
"the end"
].map(name => ({ name, dim: world.getDimension(name)}));
Object.freeze(dimensions);


class _Dimension {
getName(dimension) {
const { name } = dimensions.find(({ dim }) => dim === dimension);
return name;
}

get(dimName) {
const { dim } = dimensions.find(({ name }) => name === dimName);
return dim;
}
}

export const Dimension = new _Dimension();
3 changes: 0 additions & 3 deletions src/eventEmitter.js

This file was deleted.

11 changes: 0 additions & 11 deletions src/index.js

This file was deleted.

81 changes: 42 additions & 39 deletions src/iterator.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
export class AdvancedIterator {
#iterator;

constructor(iterator) {
this.#iterator = iterator;
}

*[Symbol.iterator]() {
for(const i of this.#iterator){
yield i;
}
}

map(func) {
const iterator = this.#iterator;
return new AdvancedIterator((function*() {
for(const i of iterator) {
yield func(i);
}
})());
}

filter(check) {
const iterator = this.#iterator;
return new AdvancedIterator((function*() {
for(const i of iterator) {
if(!check(i)) continue;
yield i;
}
})());
}

find(check) {
for(const i of this.#iterator) {
if(!check(i)) continue;
return i;
}
}
}
const CANCEL = Symbol("Cancel");

export class Iterator {
#iterator;

constructor(iterator) {
this.#iterator = iterator;
}

*[Symbol.iterator]() {
yield* this.#iterator;
}

#wrapper(func) {
const iterator = this.#iterator;
const iter = function* () {
for(const v of iterator) {
const result = func(v);
if(result === CANCEL) {
continue;
}
yield result;
}
}
this.#iterator = iter();
return this;
}

map(func) {
return this.#wrapper(func);
}

filter(check) {
return this.#wrapper(v => check(v) ? v : CANCEL);
}

find(check) {
for(const i of this.#iterator) {
if(check(i)) return i;
}
}
}
2 changes: 1 addition & 1 deletion src/rawTextBuilder/rawTextBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default class RawTextBuilder {
return { rawtext: this.#buildValues() };
}

buildJSON() {
buildJson() {
return JSON.stringify(this.build());
}
}
4 changes: 2 additions & 2 deletions src/tick.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Event } from "./event.js";
import { world } from "mojang-minecraft";


class Interval {
Expand Down Expand Up @@ -46,7 +46,7 @@ export const Tick = new (class {
#intervalHandler = new IntervalHandler();

constructor() {
Event.on("tick", _ => this.#intervalHandler.tick());
world.events.tick.subscribe(_ => this.#intervalHandler.tick());
}

setInterval(callback, interval) {
Expand Down
10 changes: 10 additions & 0 deletions src/utils/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function isSequence(array) {
let [ previous, ...values ] = Array.from(array);
for(const value of values) {
if(Math.abs(value - previous) !== 1) {
return false;
}
previous = value;
}
return true;
}
8 changes: 4 additions & 4 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "./object.js";
export * from "./print.js";
export * from "./range.js";
export * from "./string.js";
// export * from "./object.js";
// export * from "./range.js";
export * from "./array.js";
export * from "./options.js";
22 changes: 22 additions & 0 deletions src/utils/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as m from "mojang-minecraft";


function generateClass(baseClass) {
return class extends baseClass {
constructor(options) {
super();
for(const key in options) {
if(key in this) this[key] = options[key];
else throw `${key} does not exist in ${baseClass.name}`;
}
}
}
}

export const BlockRaycastOptions = generateClass(m.BlockRaycastOptions);
export const EntityDataDrivenTriggerEventOptions = generateClass(m.EntityDataDrivenTriggerEventOptions);
export const EntityEventOptions = generateClass(m.EntityEventOptions);
export const EntityQueryOptions = generateClass(m.EntityQueryOptions);
export const EntityQueryScoreOptions = generateClass(m.EntityQueryScoreOptions);
export const EntityRaycastOptions = generateClass(m.EntityRaycastOptions);
export const ExplosionOptions = generateClass(m.ExplosionOptions);
32 changes: 0 additions & 32 deletions src/utils/print.js

This file was deleted.

Loading

0 comments on commit 1675093

Please sign in to comment.