UNPKG

chest-form

Version:

Sample plugin provides a basic configuration for plugin usage in the SerenityJS software.

2 lines (1 loc) 3.93 kB
"use strict";var w=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var v=Object.getOwnPropertyNames;var B=Object.prototype.hasOwnProperty;var m=(c,t)=>w(c,"name",{value:t,configurable:!0});var x=(c,t)=>{for(var i in t)w(c,i,{get:t[i],enumerable:!0})},S=(c,t,i,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of v(t))!B.call(c,s)&&s!==i&&w(c,s,{get:()=>t[s],enumerable:!(o=I(t,s))||o.enumerable});return c};var T=c=>S(w({},"__esModule",{value:!0}),c);var z={};x(z,{ChestForm:()=>b,ChestFormContainer:()=>u,ChestFormPlugin:()=>C,default:()=>N});module.exports=T(z);var g=require("@serenityjs/protocol"),y=require("@serenityjs/plugins");var n=require("@serenityjs/protocol"),h=require("@serenityjs/core"),k=require("@serenityjs/nbt");var u=class c extends h.Container{static{m(this,"ChestFormContainer")}static CHEST_TYPE=h.BlockType.get(h.BlockIdentifier.Chest);title;placedPositions=[];showCallback=null;constructor(t,i=27){super(n.ContainerType.Container,n.ContainerId.None,i),this.title=t}show(t,i){super.show(t),this.showCallback=i||null;let{x:o,y:s,z:l}=t.position.floor(),e=Math.ceil(this.size/27);for(let p=0;p<e;p++){let f=new n.BlockPosition(o+p,s+3,l);this.placedPositions.push(f);let d=new n.UpdateBlockPacket;d.networkBlockId=c.CHEST_TYPE.getPermutation().networkId,d.position=f,d.layer=n.UpdateBlockLayerType.Normal,d.flags=n.UpdateBlockFlagsType.Network;let a=new n.BlockActorDataPacket;a.position=f,a.nbt=new k.CompoundTag,a.nbt.add(new k.IntTag(o,"pairx")),a.nbt.add(new k.IntTag(s,"pairy")),a.nbt.add(new k.IntTag(l,"pairz")),a.nbt.add(new k.StringTag(this.title,"CustomName")),t.send(d,a)}let r=new n.ContainerOpenPacket;r.type=this.type,r.identifier=this.identifier,r.position=new n.BlockPosition(o,s+3,l),r.uniqueId=-1n,t.world.schedule(10).on(()=>{t.send(r),this.update()})}close(t,i,o=-1){if(super.close(t,i),this.placedPositions.length!==0){for(let s of this.placedPositions){let l=t.dimension.getBlock(s),e=new n.UpdateBlockPacket;e.networkBlockId=l.getPermutation().networkId,e.position=s,e.layer=n.UpdateBlockLayerType.Normal,e.flags=n.UpdateBlockFlagsType.Network,t.send(e)}this.showCallback&&this.showCallback(o),this.placedPositions=[],this.showCallback=null}}};var b=class{static{m(this,"ChestForm")}container;constructor(t,i="single"){this.container=new u(t,i==="single"?27:54)}button(t,i){this.container.setItem(t,i)}show(t,i){this.container.show(t,i)}};var C=class extends y.Plugin{static{m(this,"ChestFormPlugin")}priority=y.PluginPriority.High;ChestFormContainer=u;ChestForm=b;constructor(){super("chest-form","0.1.2")}onInitialize(){this.logger.info("Initialized and ready to rock!"),this.serenity.network.before(g.Packet.ItemStackRequest,({packet:t,connection:i})=>{let o=this.serenity.getPlayerByConnection(i);if(!o)return!1;for(let s of t.requests)for(let l of s.actions){if(l.takeOrPlace){let e=l.takeOrPlace,r=o.getContainer(e.source.container.identifier),p=o.getContainer(e.destination.container.identifier);if(p instanceof u){let f=o.getContainer(e.source.container.identifier),d=o.getContainer(g.ContainerName.Inventory),a=f.takeItem(e.source.slot,e.amount);return a&&d.addItem(a),d.update(),p.update(),!1}if(r instanceof u){if(!r.getItem(e.source.slot))return!1;let d=e.source.slot;return r.close(o,!0,d),!1}}if(l.swap){let e=l.swap,r=o.getContainer(e.source.container.identifier),p=o.getContainer(e.destination.container.identifier);if(p instanceof u){if(!p.getItem(e.destination.slot))return!1;let d=o.getContainer(g.ContainerName.Inventory),a=r.getItem(e.source.slot);a&&(d.addItem(a),r.clearSlot(e.source.slot));let P=e.destination.slot;return p.close(o,!0,P),!1}if(r instanceof u)return!1}if(l.drop){let e=l.drop,r=o.getContainer(e.source.container.identifier);if(r instanceof u){if(!r.getItem(e.source.slot))return!1;let f=e.source.slot;return r.close(o,!0,f),!1}}}return!0})}},N=new C;0&&(module.exports={ChestForm,ChestFormContainer,ChestFormPlugin});