Permalink
Browse files

Added PLCs

  • Loading branch information...
me4502 committed Sep 10, 2017
1 parent ce31c03 commit 5638b6c0aefa935b359edf8eaf5d3e3312db4269
View
@@ -103,6 +103,7 @@ subprojects {
maven { url "http://repo.spongepowered.org/maven/" }
maven { url "http://maven.sk89q.com/repo/" }
maven { url "http://repo.maven.apache.org/maven2" }
maven { url 'https://oss.sonatype.org/content/groups/public/' }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
maven { url 'https://dl.bintray.com/me4502/maven' }
maven { url 'http://repo.bstats.org/content/repositories/releases/' }
@@ -16,10 +16,10 @@ def spongeFullVersion = spongeVersion + ".0-SNAPSHOT"
dependencies {
compile project(':craftbook-core')
compile "org.spongepowered:spongeapi:${spongeFullVersion}"
compileOnly "org.spongepowered:spongeapi:${spongeFullVersion}"
compile 'com.me4502:ModularFramework:1.8.4'
compile 'com.sk89q.worldedit:worldedit-sponge:6.1.7-SNAPSHOT'
testCompile group: 'org.mockito', name: 'mockito-core', version:'1.9.0-rc1'
testCompile "org.mockito:mockito-core:2.+"
}
project.archivesBaseName = "${project.archivesBaseName}-S${spongeVersion}"
@@ -80,7 +80,7 @@ public void unload() {
public Sign getSign() {
if (sign == null) {
sign = (Sign) block.getTileEntity().orElseThrow(() -> new IllegalStateException("IC given block that is not a sign! (" + block.getBlockPosition().toString() + ") in world " + block.getExtent().getName()));
sign = (Sign) block.getTileEntity().orElseThrow(() -> new IllegalStateException("IC given block that is not a sign! (" + block.getBlockPosition() + ") in world " + block.getExtent().getName()));
}
return sign;
@@ -51,6 +51,8 @@
import com.sk89q.craftbook.sponge.mechanics.ics.chips.world.weather.TimeControlAdvanced;
import com.sk89q.craftbook.sponge.mechanics.ics.factory.ICFactory;
import com.sk89q.craftbook.sponge.mechanics.ics.factory.SerializedICFactory;
import com.sk89q.craftbook.sponge.mechanics.ics.plc.PlcFactory;
import com.sk89q.craftbook.sponge.mechanics.ics.plc.lang.Perlstone;
import org.spongepowered.api.Sponge;
import java.util.Comparator;
@@ -122,6 +124,9 @@
registerICType(new ICType<>("MC4110", "HALF SUBTR", "Half Subtractor", "A compact half-subtractor", new HalfSubtractor.Factory(), "3I3O"));
registerICType(new ICType<>("MC4200", "DISPATCH", "Dispatcher", "Outputs the centre input on the appropriate outputs when input is high.", new Dispatcher.Factory(), "3I3O"));
//PLC
registerICType(new ICType<>("MC5000", "PERLSTONE", "Perlstone 3ISO Programmable Logic Chip", "3ISO PLC programmable with Perlstone.", new PlcFactory<>(new Perlstone()), "3ISO"));
registerICType(new ICType<>("MC6020", "RANDOM 5", "Random 5-Bit", "Randomly sets the outputs on high.", new RandomBit.Factory(), "SI5O"));
}
@@ -0,0 +1,24 @@
/*
* CraftBook Copyright (C) 2010-2017 sk89q <http://www.sk89q.com>
* CraftBook Copyright (C) 2011-2017 me4502 <http://www.me4502.com>
* CraftBook Copyright (C) Contributors
*
* 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.sponge.mechanics.ics.plc;
public class CodeNotFoundException extends Exception {
public CodeNotFoundException(String message) {
super(message);
}
}
@@ -0,0 +1,32 @@
/*
* CraftBook Copyright (C) 2010-2017 sk89q <http://www.sk89q.com>
* CraftBook Copyright (C) 2011-2017 me4502 <http://www.me4502.com>
* CraftBook Copyright (C) Contributors
*
* 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.sponge.mechanics.ics.plc;
public class PlcException extends Exception {
private static final long serialVersionUID = -3541787352186665688L;
public final String detailedMessage;
public PlcException(String message) {
this(message, message);
}
public PlcException(String message, String detailedMessage) {
super(message);
this.detailedMessage = detailedMessage;
}
}
@@ -0,0 +1,109 @@
/*
* CraftBook Copyright (C) 2010-2017 sk89q <http://www.sk89q.com>
* CraftBook Copyright (C) 2011-2017 me4502 <http://www.me4502.com>
* CraftBook Copyright (C) Contributors
*
* 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.sponge.mechanics.ics.plc;
import com.sk89q.craftbook.sponge.mechanics.ics.SerializedICData;
import com.sk89q.craftbook.sponge.mechanics.ics.factory.SerializedICFactory;
import org.spongepowered.api.data.DataContainer;
import org.spongepowered.api.data.DataQuery;
import org.spongepowered.api.data.DataView;
import org.spongepowered.api.data.persistence.InvalidDataException;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class PlcFactory<Lang extends PlcLanguage> extends SerializedICFactory<PlcIC<Lang>, PlcFactory.PlcStateData> {
private Lang lang;
public PlcFactory(Lang lang) {
super(PlcStateData.class, 1);
this.lang = lang;
}
@Override
public PlcIC<Lang> createInstance(Location<World> location) {
return new PlcIC<>(this, location, lang);
}
@Override
protected Optional<PlcStateData> buildContent(DataView container) throws InvalidDataException {
PlcStateData state = new PlcStateData();
state.state = container.getBooleanList(DataQuery.of("State")).orElse(new ArrayList<>());
state.languageName = container.getString(DataQuery.of("LanguageName")).orElse("Perlstone");
state.codeString = container.getString(DataQuery.of("Code")).orElse("");
state.error = container.getBoolean(DataQuery.of("Error")).orElse(false);
state.errorCode = container.getString(DataQuery.of("ErrorCode")).orElse("");
return Optional.of(state);
}
@Override
public PlcStateData getData(PlcIC<Lang> ic) {
return ic.state;
}
@Override
public void setData(PlcIC<Lang> ic, PlcStateData data) {
ic.state = data;
}
@Override
public String[] getLineHelp() {
return new String[] {"", ""};
}
@Override
public String[][] getPinHelp() {
return new String[][] {
new String[] {
"Programmable Input",
"Programmable Input",
"Programmable Input"
},
new String[] {
"Program Output"
}
};
}
public static class PlcStateData extends SerializedICData {
public List<Boolean> state;
public String languageName;
public String codeString = "";
public boolean error;
public String errorCode = "";
@Override
public int getContentVersion() {
return 1;
}
@Override
public DataContainer toContainer() {
return super.toContainer()
.set(DataQuery.of("State"), this.state)
.set(DataQuery.of("LanguageName"), this.languageName)
.set(DataQuery.of("Code"), this.codeString)
.set(DataQuery.of("Error"), this.error)
.set(DataQuery.of("ErrorCode"), this.errorCode);
}
}
}
@@ -0,0 +1,171 @@
/*
* CraftBook Copyright (C) 2010-2017 sk89q <http://www.sk89q.com>
* CraftBook Copyright (C) 2011-2017 me4502 <http://www.me4502.com>
* CraftBook Copyright (C) Contributors
*
* 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.sponge.mechanics.ics.plc;
import com.google.common.collect.Lists;
import com.sk89q.craftbook.sponge.mechanics.ics.IC;
import com.sk89q.craftbook.sponge.mechanics.ics.InvalidICException;
import com.sk89q.craftbook.sponge.mechanics.ics.plc.lang.WithLineInfo;
import com.sk89q.craftbook.sponge.util.SignUtil;
import org.spongepowered.api.block.BlockTypes;
import org.spongepowered.api.block.tileentity.Sign;
import org.spongepowered.api.block.tileentity.carrier.Chest;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.Slot;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.util.Direction;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
import java.util.List;
public class PlcIC<Lang extends PlcLanguage> extends IC {
private Lang lang;
public PlcFactory.PlcStateData state;
private WithLineInfo[] code;
PlcIC(PlcFactory<Lang> factory, Location<World> location, Lang lang) {
super(factory, location);
this.lang = lang;
}
@Override
public void create(Player player, List<Text> lines) throws InvalidICException {
super.create(player, lines);
state = new PlcFactory.PlcStateData();
state.state = lang.initState();
state.languageName = lang.getName();
}
public void load() {
String codeString;
try {
codeString = getCode();
} catch (CodeNotFoundException e) {
error("Error Retrieving Code", e.getMessage());
return;
}
try {
if (codeString != null) {
code = lang.compile(codeString);
}
} catch (InvalidICException e) {
error("Inconsistent compile check!", e.getMessage());
}
state.codeString = codeString;
}
private String getBookCode(Location<World> chestBlock) throws CodeNotFoundException {
Chest c = chestBlock.getTileEntity().map(te -> (Chest) te).get();
Inventory i = c.getDoubleChestInventory().orElse(c.getInventory());
ItemStack book = null;
for (Slot slot : i.query(ItemTypes.WRITABLE_BOOK, ItemTypes.WRITTEN_BOOK).<Slot>slots()) {
ItemStack item = slot.peek().orElse(null);
if (item != null && item.getQuantity() > 0 ) {
if (book != null) throw new CodeNotFoundException("More than one written book found in chest!!");
book = item;
}
}
if (book == null) throw new CodeNotFoundException("No written books found in chest.");
StringBuilder code = new StringBuilder();
for (Text page : book.get(Keys.BOOK_PAGES).orElse(Lists.newArrayList())) {
code.append(SignUtil.getTextRaw(page)).append('\n');
}
return code.toString();
}
private String getCode() throws CodeNotFoundException {
Location<World> above = getBlock().getRelative(Direction.UP);
if (above.getBlockType() == BlockTypes.CHEST) return getBookCode(above);
Location<World> below = getBlock().getRelative(Direction.DOWN);
if (below.getBlockType() == BlockTypes.CHEST) return getBookCode(below);
int x = getBlock().getBlockX();
int z = getBlock().getBlockZ();
for (int y = 0; y < 256; y++) {
if (y != getBlock().getBlockY()) {
Location<World> testBlock = getBlock().getExtent().getLocation(x, y, z);
if (SignUtil.isSign(testBlock)) {
Sign sign = testBlock.getTileEntity().map(te -> (Sign) te).get();
if ("[Code Block]".equalsIgnoreCase(SignUtil.getTextRaw(sign, 1))) {
y--;
Location<World> b = testBlock.getExtent().getLocation(x, y, z);
StringBuilder code = new StringBuilder();
while (SignUtil.isSign(b)) {
sign = b.getTileEntity().map(te -> (Sign) te).get();
for (int li = 0; li < 4 && y != getBlock().getBlockY(); li++) {
code.append(SignUtil.getTextRaw(sign, li)).append('\n');
}
b = getBlock().getExtent().getLocation(x, --y, z);
}
return code.toString();
}
}
}
}
throw new CodeNotFoundException("No code source found.");
}
public void error(String shortMessage, String detailedMessage) {
setLine(2, Text.of(TextColors.RED, "!Error!"));
setLine(3, Text.of(shortMessage));
state.error = true;
state.errorCode = detailedMessage;
}
@Override
public void trigger() {
try {
lang.execute(this, state.state, code);
} catch (PlcException e) {
error(e.getMessage(), e.detailedMessage);
} catch (Exception e) {
e.printStackTrace();
error(e.getClass().getName(), "Internal error encountered: " + e.getClass().getName());
}
}
/*@Override
public void onRightClick(Player p) {
if (CraftBookPlugin.inst().hasPermission(p, "craftbook.plc.debug")) {
p.sendMessage(ChatColor.GREEN + "Programmable Logic Controller debug information");
BlockWorldVector l = sign.getBlockVector();
p.sendMessage(ChatColor.RED + "Status:" + ChatColor.RESET + " " + (error ? "Error Encountered" : "OK"));
p.sendMessage(ChatColor.RED + "Location:" + ChatColor.RESET + " (" + l.getBlockX() + ", " +
"" + l.getBlockY() + ", " + l.getBlockZ() + ")");
p.sendMessage(ChatColor.RED + "Language:" + ChatColor.RESET + " " + lang.getName());
p.sendMessage(ChatColor.RED + "Full Storage Name:" + ChatColor.RESET + " " + getFileName());
if (error) {
p.sendMessage(errorString);
} else {
p.sendMessage(lang.dumpState(state.state));
}
} else {
p.sendMessage(ChatColor.RED + "You do not have the necessary permissions to do that.");
}
}*/
}
Oops, something went wrong.

0 comments on commit 5638b6c

Please sign in to comment.