Jump to content

[CustomNPCs] [1.8.8] [Script] Wireless Redstone


Recommended Posts

Posted

A Wireless Redstone script that includes a Transmitter, Receiver and channel support;

 

Usage:

Transmitter:

 

var rsTransmitter;

function init(event){
    rsTransmitter = new WirelessRedstone.Transmitter(event, "My Channel Name");
}

function redstone(event){
    rsTransmitter.redstone(event);
}

 

Receiver:

var rsReceiver;

function init(event){
    rsReceiver = new WirelessRedstone.Receiver(event, "My Channel Name");
}

function broken(event){
    rsReceiver.broken(event);
}

 

Script:

 

Note: as of CustomNpcs_1.8.8_beta(17dec15) you cannot load external scripts, so you'll have to paste the following into the top of each ScriptedBlock you want to use this on.

 

function RedstoneTransmitter(event, channel){
    var world = event.block.getWorld();

    this.block = event.block;
    this.channel = channel;

    var tempData = world.getTempdata();
    var worldChannels = tempData.get("RedstoneChannels");
    
    if(!worldChannels){
        worldChannels = {};
    }

    if(!worldChannels[this.channel]){
        worldChannels[this.channel] = [];
    }

    tempData.put("RedstoneChannels", worldChannels);
}

    RedstoneTransmitter.prototype.redstone = function(event) {
        var world = event.block.world;
        var worldChannels = world.getTempdata().get("RedstoneChannels");

        if(!worldChannels){
            worldChannels = {};
        }

        var receiverList = worldChannels[this.channel];
        
        if(receiverList.length>0){
            for(var i=0;i<receiverList.length;i++){
                var receiver = receiverList[i];

                if(receiver){
                    receiver.redstone(event);
                }
            }
        }
    };

function RedstoneReceiver(event, channel){
    var world = event.block.getWorld();

    this.block = event.block;
    this.channel = channel;

    var tempData = world.getTempdata();
    var worldChannels = tempData.get("RedstoneChannels");
    
    if(!worldChannels){
        worldChannels = {};
    }

    if(!worldChannels[this.channel]){
        worldChannels[this.channel] = [];
    }

    worldChannels[this.channel].push(this);

    tempData.put("RedstoneChannels", worldChannels);
}

    RedstoneReceiver.prototype.redstone = function(event) {
        var world = this.block.getWorld();
        this.block.setRedstonePower(event.power);
    };

    RedstoneReceiver.prototype.broken = function(event) {
        var world = event.block.world;
        var worldChannels = world.getTempdata().get("RedstoneChannels");

        if(!worldChannels){
            worldChannels = {};
        }

        var receiverList = worldChannels[this.channel];
        
        if(receiverList.length>0){
            for(var i=receiverList.length-1;i>=0;i--){
                var receiver = receiverList[i];
                if(receiver.block.getX()==this.block.getX() && receiver.block.getY()==this.block.getY() && receiver.block.getZ()==this.block.getZ()){
                    receiverList.splice(i, 1);
                }
            }
        }
        
        worldChannels[this.channel] = receiverList;
        world.getTempdata().put("RedstoneChannels", worldChannels);
    };

var WirelessRedstone = {
    Transmitter: RedstoneTransmitter,
    Receiver: RedstoneReceiver
};

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.