Skip to content

Instantly share code, notes, and snippets.

@nidefawl
Created May 1, 2019 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nidefawl/214ffb3dcee5cb702434a5b144fcc141 to your computer and use it in GitHub Desktop.
Save nidefawl/214ffb3dcee5cb702434a5b144fcc141 to your computer and use it in GitHub Desktop.
CircuitsPlugin.java Craftland.org
// $Id$
/*
* Copyright (C) 2010, 2011 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.craftbook.bukkit;
import org.bukkit.Chunk;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.craftland.mod.ModServer;
import com.sk89q.craftbook.CircuitsConfiguration;
import com.sk89q.craftbook.MechanicManager;
import com.sk89q.craftbook.circuits.JackOLantern;
import com.sk89q.craftbook.circuits.Netherrack;
import com.sk89q.craftbook.gates.logic.AndGate;
import com.sk89q.craftbook.gates.logic.Clock;
import com.sk89q.craftbook.gates.logic.DownCounter;
import com.sk89q.craftbook.gates.logic.EdgeTriggerDFlipFlop;
import com.sk89q.craftbook.gates.logic.InvertedRsNandLatch;
import com.sk89q.craftbook.gates.logic.Inverter;
import com.sk89q.craftbook.gates.logic.JkFlipFlop;
import com.sk89q.craftbook.gates.logic.LevelTriggeredDFlipFlop;
import com.sk89q.craftbook.gates.logic.Marquee;
import com.sk89q.craftbook.gates.logic.Monostable;
import com.sk89q.craftbook.gates.logic.Multiplexer;
import com.sk89q.craftbook.gates.logic.NandGate;
import com.sk89q.craftbook.gates.logic.RandomBit;
import com.sk89q.craftbook.gates.logic.Repeater;
import com.sk89q.craftbook.gates.logic.RsNandLatch;
import com.sk89q.craftbook.gates.logic.RsNorFlipFlop;
import com.sk89q.craftbook.gates.logic.ToggleFlipFlop;
import com.sk89q.craftbook.gates.logic.XnorGate;
import com.sk89q.craftbook.gates.logic.XorGate;
import com.sk89q.craftbook.gates.world.CreatureSpawner;
import com.sk89q.craftbook.gates.world.DaySensor;
import com.sk89q.craftbook.gates.world.DaySensorST;
import com.sk89q.craftbook.gates.world.FlexibleSetBlock;
import com.sk89q.craftbook.gates.world.LavaSensor;
import com.sk89q.craftbook.gates.world.LavaSensorST;
import com.sk89q.craftbook.gates.world.LightSensor;
import com.sk89q.craftbook.gates.world.LightningSummon;
import com.sk89q.craftbook.gates.world.MessageSender;
import com.sk89q.craftbook.gates.world.MultipleSetBlock;
import com.sk89q.craftbook.gates.world.ResurrectDumbledore;
import com.sk89q.craftbook.gates.world.ServerTimeModulus;
import com.sk89q.craftbook.gates.world.TimeControl;
import com.sk89q.craftbook.gates.world.WaterSensor;
import com.sk89q.craftbook.gates.world.WaterSensorST;
import com.sk89q.craftbook.gates.world.WirelessReceiver;
import com.sk89q.craftbook.gates.world.WirelessReceiverST;
import com.sk89q.craftbook.gates.world.WirelessTransmitter;
import com.sk89q.craftbook.ic.ICFamily;
import com.sk89q.craftbook.ic.ICManager;
import com.sk89q.craftbook.ic.ICMechanicFactory;
import com.sk89q.craftbook.ic.families.Family3ISO;
import com.sk89q.craftbook.ic.families.FamilySI3O;
import com.sk89q.craftbook.ic.families.FamilySISO;
/**
* Plugin for CraftBook's redstone additions.
*
* @author sk89q
*/
public class CircuitsPlugin extends BaseBukkitPlugin {
protected CircuitsConfiguration config;
protected ICManager icManager;
public MechanicManager manager;
public static CircuitsPlugin instance;
@Override
public void onEnable() {
super.onEnable();
this.createDefaultConfiguration("config.yml");
this.createDefaultConfiguration("custom-ics.txt");
this.config = new CircuitsConfiguration(this.getConfiguration(), this.getDataFolder());
this.manager = new MechanicManager(this);
final MechanicListenerAdapter adapter = new MechanicListenerAdapter(this);
adapter.listenInteract = false;
adapter.register(this.manager);
this.registerICs();
// Let's register mechanics!
if (this.config.enableNetherstone) {
this.manager.register(new Netherrack.Factory());
}
if (this.config.enablePumpkins) {
this.manager.register(new JackOLantern.Factory());
}
if (this.config.enableICs) {
this.manager.register(new ICMechanicFactory(this, this.icManager));
this.setupSelfTriggered();
}
}
@Override
public void onLoad() {
instance = this;
}
/**
* Register ICs.
*/
private void registerICs() {
final Server server = this.getServer();
// Let's register ICs!
this.icManager = new ICManager();
final ICFamily familySISO = new FamilySISO();
final ICFamily family3ISO = new Family3ISO();
final ICFamily familySI3O = new FamilySI3O();
// ICFamily family3I3O = new Family3I3O();
// SISOs
this.icManager.register("MC9999", new ResurrectDumbledore.Factory(server, true), familySISO);
this.icManager.register("MC1000", new Repeater.Factory(server), familySISO);
this.icManager.register("MC1001", new Inverter.Factory(server), familySISO);
this.icManager.register("MC1017", new ToggleFlipFlop.Factory(server, true), familySISO);
this.icManager.register("MC1018", new ToggleFlipFlop.Factory(server, false), familySISO);
this.icManager.register("MC1020", new RandomBit.Factory(server, true), familySISO);
this.icManager.register("MC1025", new ServerTimeModulus.Factory(server, true), familySISO);
this.icManager.register("MC1110", new WirelessTransmitter.Factory(server), familySISO);
this.icManager.register("MC1111", new WirelessReceiver.Factory(server, true), familySISO);
this.icManager.register("MC1200", new CreatureSpawner.Factory(server, true), familySISO); // REQ
// PERM
this.icManager.register("MC1207", new FlexibleSetBlock.Factory(server), familySISO); // REQ
// PERM
this.icManager.register("MC1208", new MultipleSetBlock.Factory(server), familySISO); // REQ
// PERM
// Missing: 1202 (replaced by dispenser?) // REQ PERM
this.icManager.register("MC1203", new LightningSummon.Factory(server, true), familySISO); // REQ
// PERM
// Missing: 1205 // REQ PERM
// Missing: 1206 // REQ PERM
this.icManager.register("MC1230", new DaySensor.Factory(server, true), familySISO);
this.icManager.register("MC1231", new TimeControl.Factory(server, true), familySISO); // REQ
// PERM
this.icManager.register("MC1260", new WaterSensor.Factory(server, true), familySISO);
this.icManager.register("MC1261", new LavaSensor.Factory(server, true), familySISO);
this.icManager.register("MC1262", new LightSensor.Factory(server, true), familySISO);
// Missing: 1240 (replaced by dispenser?) // REQ PERM
// Missing: 1241 (replaced by dispenser?) // REQ PERM
// Missing: 1420
this.icManager.register("MC1510", new MessageSender.Factory(server, true), familySISO);
// SI3Os
// Missing: 2020 (?)
this.icManager.register("MC2999", new Marquee.Factory(server), familySI3O);
// 3ISOs
this.icManager.register("MC3002", new AndGate.Factory(server), family3ISO);
this.icManager.register("MC3003", new NandGate.Factory(server), family3ISO);
this.icManager.register("MC3020", new XorGate.Factory(server), family3ISO);
this.icManager.register("MC3021", new XnorGate.Factory(server), family3ISO);
this.icManager.register("MC3030", new RsNorFlipFlop.Factory(server), family3ISO);
this.icManager.register("MC3031", new InvertedRsNandLatch.Factory(server), family3ISO);
this.icManager.register("MC3032", new JkFlipFlop.Factory(server), family3ISO);
this.icManager.register("MC3033", new RsNandLatch.Factory(server), family3ISO);
this.icManager.register("MC3034", new EdgeTriggerDFlipFlop.Factory(server), family3ISO);
this.icManager.register("MC3036", new LevelTriggeredDFlipFlop.Factory(server), family3ISO);
this.icManager.register("MC3040", new Multiplexer.Factory(server), family3ISO);
this.icManager.register("MC3101", new DownCounter.Factory(server), family3ISO);
// Missing: 3231 // REQ PERM
// 3I3Os
// Missing: 4000
// Missing: 4010
// Missing: 4100
// Missing: 4110
// Missing: 4200
// Self triggered
this.icManager.register("MC0111", new WirelessReceiverST.Factory(server), familySISO);
this.icManager.register("MC0230", new DaySensorST.Factory(server), familySISO);
this.icManager.register("MC0260", new WaterSensorST.Factory(server), familySISO);
this.icManager.register("MC0261", new LavaSensorST.Factory(server), familySISO);
this.icManager.register("MC0420", new Clock.Factory(server), familySISO);
this.icManager.register("MC1421", new Clock.Factory(server), familySISO);
this.icManager.register("MC0421", new Monostable.Factory(server), familySISO);
// Missing: 0020
// Missing: 0230
// Missing: 0262
// Missing: 0420
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args, String sentFromChannel) {
return super.onCommand(sender, command, label, args, sentFromChannel);
}
/**
* Setup the required components of self-triggered ICs.
*/
private void setupSelfTriggered() {
logger.info("CraftBook: Enumerating chunks for self-triggered components...");
final long start = System.currentTimeMillis();
int numWorlds = 0;
int numChunks = 0;
for (final World world : this.getServer().getWorlds()) {
for (final Chunk chunk : world.getLoadedChunks()) {
this.manager.enumerate(chunk);
numChunks++;
}
numWorlds++;
}
final long time = System.currentTimeMillis() - start;
logger.info("CraftBook: " + numChunks + " chunk(s) for " + numWorlds + " world(s) processed " + "(" + Math.round(time / 1000.0 * 10) / 10 + "s elapsed)");
// Set up the clock for self-triggered ICs.
ModServer.instance.scheduleRepeatingTask(this, new MechanicClock(this.manager), 0, 2).setName("CraftBook circuits clock");
}
@Override
protected void registerEvents() {
}
public CircuitsConfiguration getLocalConfiguration() {
return this.config;
}
public ICManager getIcManager() {
return this.icManager;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment