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