Skip to content

Instantly share code, notes, and snippets.

@lycano
Last active August 29, 2015 14:17
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 lycano/35c46308895377d0278a to your computer and use it in GitHub Desktop.
Save lycano/35c46308895377d0278a to your computer and use it in GitHub Desktop.
Hi,
im currently creating Wormhole X-Treme v2.0. Currently im working on ShapeParsing which later will be the template for all new Stargates.
Thanks to Cogito Learning - http://cogitolearning.co.uk/?p=525 i came up with this solution.
The following files have been created to illustrate the current ShapeParsing process (WIP) for key=value entries.
Next step would be to create a Class that can access the mapped entries.
private void parseShapeFile(List<String> fileLines) {
logger.info("LineCount: " + fileLines.size());
try {
ShapeTokenizerVersion2 tokenizer = new ShapeTokenizerVersion2();
tokenizer.add("\\w+", 1);
tokenizer.add("=", 2);
tokenizer.add("[0-9]+", 3);
Map<String, ShapeTokenizerVersion2Result> tokenMap = this.parseShapeFileLines(fileLines, tokenizer);
logger.info("tokenMap keys: " + tokenMap.keySet().toString());
for (String tokenMapKey: tokenMap.keySet()) {
ShapeTokenizerVersion2Result tokenResult = tokenMap.get(tokenMapKey);
String key = tokenResult.getKey();
String value = tokenResult.getValue();
logger.info("TokenMapKey.get(" + tokenMapKey + "): " + key + " is " + value);
}
} catch (PatternSyntaxException e) {
logger.warn("Caught exception PatternSyntaxException: " + e.getMessage());
}
}
private Map<String, ShapeTokenizerVersion2Result> parseShapeFileLines(List<String> fileLines, ShapeTokenizerVersion2 tokenizer) {
return this.parseShapeFileLines(fileLines, tokenizer, false);
}
private Map<String, ShapeTokenizerVersion2Result> parseShapeFileLines(List<String> fileLines, ShapeTokenizerVersion2 tokenizer, boolean enableDebug) {
String currentLine;
Map<String, ShapeTokenizerVersion2Result> tokenMap = new HashMap<String, ShapeTokenizerVersion2Result>();
LinkedList<ShapeTokenizerVersion2.Token> tokens;
for (int i=0; i<fileLines.size();i++) {
currentLine = fileLines.get(i);
try {
tokenizer.tokenize(currentLine);
tokens = tokenizer.getTokens();
if (tokens.isEmpty())
throw new WormholeParserException("Tokenizer did not return any tokens");
if (enableDebug) {
logger.info("-----------------------------------------------");
logger.info("[" + i + "]: " + currentLine);
for (ShapeTokenizerVersion2.Token tok : tokenizer.getTokens()) {
logger.info("" + tok.token + " " + tok.sequence);
}
}
ShapeTokenizerVersion2.Token firstToken = tokenizer.getTokens().getFirst();
ShapeTokenizerVersion2.Token lastToken = tokenizer.getTokens().getLast();
tokenMap.put(firstToken.sequence.toLowerCase(), new ShapeTokenizerVersion2Result(firstToken.sequence, lastToken.sequence));
} catch (WormholeParserException e) {
// silently ignore no matching entries
if (enableDebug)
logger.warn("[" + i + "] " + e.getMessage() + " - " + tokenizer.getLastTokenInfo());
} catch (NoSuchElementException e) {
// silently ignore null if we do not find the first entry
if (enableDebug)
logger.warn("[" + i + "] " + e.getMessage() + " - " + tokenizer.getLastTokenInfo() + " in '" + tokenizer.getLastString() + "'");
}
}
if (enableDebug)
logger.info("keys before return: " + tokenMap.keySet().toString());
return tokenMap;
}
/*
* Wormhole X-Treme Plugin for Bukkit
* Copyright (C) 2011 Lycano <https://github.com/lycano/Wormhole-X-Treme/>
*
* Copyright (C) 2011 Ben Echols
* Dean Bailey
*
* 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 de.luricos.bukkit.WormholeXTreme.Wormhole.shape.v2;
import de.luricos.bukkit.WormholeXTreme.Wormhole.exceptions.WormholeParserException;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author lycano
*
* This file is based on a Tutorial from Cogito Learning - Writing a Parser in Java: The Tokenizer
* Link: http://cogitolearning.co.uk/?p=525
*/
public class ShapeTokenizerVersion2 {
private LinkedList<TokenInfo> tokenInfos;
private LinkedList<Token> tokens;
private TokenInfo lastTokenInfo;
private String lastString;
public ShapeTokenizerVersion2() {
tokenInfos = new LinkedList<TokenInfo>();
tokens = new LinkedList<Token>();
}
public void add(String regex, int token) {
tokenInfos.add(new TokenInfo(Pattern.compile("^(" + regex + ")"), token));
}
public void tokenize(String str) {
String s = str.trim();
tokens.clear();
while (!s.equals("")) {
boolean match = false;
for (TokenInfo info : tokenInfos) {
Matcher m = info.regex.matcher(s);
lastTokenInfo = info;
lastString = s;
if (m.find()) {
match = true;
String tok = m.group().trim();
s = m.replaceFirst("").trim();
tokens.add(new Token(info.token, tok));
break;
}
}
if (!match)
throw new WormholeParserException("Unexpected character in input: " + s);
}
}
public LinkedList<Token> getTokens() {
return tokens;
}
public TokenInfo getLastTokenInfo() {
return this.lastTokenInfo;
}
public String getLastString() {
return this.lastString;
}
private class TokenInfo {
public final Pattern regex;
public final int token;
public TokenInfo(Pattern regex, int token) {
super();
this.regex = regex;
this.token = token;
}
public String toString() {
return "TokenInfo{token=" + this.token + ",regex=" + this.regex + "}";
}
}
public class Token {
public final int token;
public final String sequence;
public Token(int token, String sequence) {
super();
this.token = token;
this.sequence = sequence;
}
public String toString() {
return "Token{token=" + this.token + ",sequence=" + this.sequence + "}";
}
}
}
/*
* Wormhole X-Treme Plugin for Bukkit
* Copyright (C) 2011 Lycano <https://github.com/lycano/Wormhole-X-Treme/>
*
* Copyright (C) 2011 Ben Echols
* Dean Bailey
*
* 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 de.luricos.bukkit.WormholeXTreme.Wormhole.shape.v2;
/**
* @author lycano
*/
public class ShapeTokenizerVersion2Result {
private Object key;
private Object value;
public ShapeTokenizerVersion2Result(Object key, Object value) {
this.key = key;
this.value = value;
}
private String getAsString(Object obj) {
return (String) obj;
}
private boolean getAsBoolean(Object obj) {
return Boolean.parseBoolean((String) obj);
}
private int getAsInt(Object obj) {
return Integer.parseInt((String) obj);
}
public String getKey() {
return this.getAsString(this.key);
}
public String getValue() {
return this.getAsString(this.value);
}
public boolean getValueAsBoolean() {
return this.getAsBoolean(this.value);
}
public int getValueAsInt() {
return this.getAsInt(this.value);
}
}
/*
* Wormhole X-Treme Plugin for Bukkit
* Copyright (C) 2011 Lycano <https://github.com/lycano/Wormhole-X-Treme/>
*
* Wormhole X-Treme Plugin for Bukkit
* Copyright (C) 2011 Ben Echols
* Dean Bailey
*
* 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 de.luricos.bukkit.WormholeXTreme.Wormhole.exceptions;
/**
* @author lycano
*/
public class WormholeParserException extends RuntimeException {
public WormholeParserException(String msg) {
super(msg);
}
}
# The name for this shape
Name=Horizontal
# Version 2 of shape files allows for many new things.
# 3D gates, new format for blocks, woosh and light order etc
Version=2
# GateShape now needs "Layer" lines
# Each Layer requires a #number= and then a newline.
# Blocks can only be placed into layers.
# a 2D gate would have only 1 layer.
# Acceptable blocks are:
# [I] = Ignored
# [S] = Stargate Material
# [P] = Air blocks that will turn into the portal material when activated.
# Extra parameters:
# --- These parameters are 1 of each per gate ---
# :N = Block where the name sign will be created. This is optional.
# :EP = Block where players teleport in at. The players feet will be on this block.
# :EM = Block where minecarts teleport in at. The minecart wheels will be on this block.
# :A = Block where the activation switch is attached to. 1 per gate!
# The only restriction is that the block that faces it must be "I" (so nothing is in the way)
# The switch will face in the positive layer direction.
# In this example the switch will face towards where layer 3 would be (if there was a 3rd layer)
# :D = Block the sign dialer hangs from. Only 1 per gate!
# The only restriction is that the block that faces it must be "I" (so nothing is in the way)
# This block is not required, so shapes with this block can be either type. (sign or dial)
# Without this block a gateshape can only be /dial gate.
# :IA = Iris Activation Switch - Not required unless you want to be able to place an Iris on the gate.
#
# IA, D, N, and A cannot be the same block, and none of those can contain W
#
# --- There can be many of these per gate -- (Currently no restriction)
# :L = Blocks that will light when gate is activated
# Optionally you may add a #number after L to indicate the order it lights.
# Defaults to 1 if there is no #
# :W = Blocks that will woosh when gate is activated
# Optionally you may add a #number after W to indicate the order it wooshes in.
# Defaults to 1 if there is no # and the wait between numbers is configurable below.
# After all #s are active it removes them in reverse order but
# if a block is [P:W] the block will stay as portal material until gate is shutdown.
#
# Redstone Blocks:
# --- There can only be 1 of each of these per gate, and they can-not occupy the same block as anything else ---
# [RD] = Redstone activation block. A redstone charge next to this block will activate the gate.
# This block requires a :D block for targetting. This block should be on top of a [S] block.
# [RS] = Redstone sign dialer cycle block. A redstone charge next to this block will cycle sign targets.
# This block requires a :D block for targetting. This block should be on top of a [S] block.
# [RA] = Redstone gate Activated block. This block will provide redstone charge when the gate is activated.
# This block should be on top of a [S] block.
GateShape=
Layer#1=
[I][I][I][I][I][I][I]
[I][I][I][I][I][I][I]
[I][I][I][I][I][I][I]
[I][I][S][S:L#7][S][I][I]
Layer#2=
[I][I][I][I][I][I][I]
[I][I][I][I:W#2][I][I][I]
[I][I][I:W#1][I:W#1][I:W#1][I][I]
[I][S:L#5][P][P][P][S:L#2][I]
Layer#3=
[I][I][I][I:W#3][I][I][I]
[I][I][I:W#2][I:W#2][I:W#2][I][I]
[I][I:W#1][I:W#1][I:W#1][I:W#1][I:W#1][I]
[S][P][P][P][P][P][S]
Layer#4=
[I][I][I][I:W#3][I][I][I]
[I][I][I:W#2][I:W#2][I:W#2][I][I]
[I][I:W#1][I:W#1][I:W#1][I:W#1][I:W#1][I]
[S:L#4][P][P][P:EP][P][P][S:L#6]
Layer#5=
[I][I][I][I:W#3][I][I][I]
[I][I][I:W#2][I:W#2][I:W#2][I][I]
[I][I:W#1][I:W#1][I:W#1][I:W#1][I:W#1][I]
[S][P][P][P][P][P][S]
Layer#6=
[I][I][I][I][I][I][I]
[I][I][I][I:W#2][I][I][I]
[I][I][I:W#1][I:W#1][I:W#1][I][I]
[I][S:L#3][P][P][P][S:L#1][I]
Layer#7=
[I][I][I][I][I][I][I]
[I][I][I][I][I][I][I]
[I][I][I][I][I][I][I]
[I][I][S:A][S:N:L#8][S:IA][I][I]
# Number of ticks to wait before activating each # of the woosh. 1 tick = ~50ms
WOOSH_TICKS = 3;
# Number of ticks to wait before activating each # of the lights. 1 tick = ~50ms
LIGHT_TICKS = 3;
# None of the follow materials are required, they will default if not set.
# Portal material the material the [P] blocks will be when gate is active.
# Suggested values are as follows: STATIONARY_WATER, STATIONARY_LAVA, PORTAL, and AIR
PORTAL_MATERIAL=STATIONARY_WATER
# Iris material is the material the [P] block become when iris is active
# Suggested values are as follows: STONE, DIAMOND_BLOCK, GLASS, IRON_BLOCK, BEDROCK, OBSIDIAN, GLOWSTONE and LAPIS_BLOCK
IRIS_MATERIAL=GLASS
# Stargate material is the material the [O] blocks are made of
# Reasonable values are as follows: STONE, DIAMOND_BLOCK, GLASS, IRON_BLOCK, BEDROCK, OBSIDIAN, GLOWSTONE and LAPIS_BLOCK
STARGATE_MATERIAL=OBSIDIAN
# Active material is the material that :L blocks become when gate is active
# Suggested Values are as follows: GLOWSTONE and GLOWING_REDSTONE_ORE
ACTIVE_MATERIAL=GLOWSTONE
# Redstone activated is the parameter to allow redstone to the DHD lever to activate the gate.
REDSTONE_ACTIVATED=FALSE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment