re-arrange ammo
This commit is contained in:
David Schmenk 2016-07-04 11:11:37 -07:00
commit fc365eaee8
5 changed files with 99 additions and 32 deletions

View File

@ -9,25 +9,26 @@ import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.badvision.outlaweditor.api.ApplicationState;
import org.badvision.outlaweditor.api.MenuAction;
import org.badvision.outlaweditor.ui.UIAction;
import org.osgi.framework.BundleContext;
/**
* This registers a simple plugin that does nothing more than print a message
* to the console when executed. However, this plugin also demonstrates how
* to inject dependencies to more useful features, specifically the ApplicationState
* which in turn provides all game data, etc.
* This registers a simple plugin that does nothing more than print a message to
* the console when executed. However, this plugin also demonstrates how to
* inject dependencies to more useful features, specifically the
* ApplicationState which in turn provides all game data, etc.
*
* @author blurry
*/
@Component(immediate = true)
@Service(MenuAction.class)
public class ExamplePlugin implements MenuAction {
// Note: Because ApplicationState is already a defined service, this will automatically be bound.
// Hence, it is not necessary to worry about passing it it.
@Reference
ApplicationState app;
// This is called when our plugin is starting
@Activate
public void activate() throws Exception {
@ -53,18 +54,19 @@ public class ExamplePlugin implements MenuAction {
System.out.println("Clicked!");
JAXB.marshal(ApplicationState.getInstance().getGameData(), System.out);
checkReferences();
UIAction.confirm("Did you mean to click that?",
() -> UIAction.alert("Well isn't that special?"),
() -> UIAction.alert("You should be more careful next time then."));
}
private void checkReferences() {
// app = ApplicationState.getInstance();
if (app == null) {
System.out.println("App is null?!?!");
} else if (app.getCurrentPlatform() == null) {
System.out.println("Current platform is null?");
} else {
if (app.getCurrentPlatform() == null) {
System.out.println("Current platform is null?");
} else {
System.out.println("Current platform is "+app.getCurrentPlatform());
}
}
System.out.println("Current platform is " + app.getCurrentPlatform());
}
}
}

View File

@ -17,7 +17,9 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
@ -36,11 +38,15 @@ import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.badvision.outlaweditor.api.ApplicationState;
import org.badvision.outlaweditor.data.DataUtilities;
import static org.badvision.outlaweditor.data.DataUtilities.extract;
import static org.badvision.outlaweditor.data.DataUtilities.extractFirst;
import org.badvision.outlaweditor.data.xml.Block;
import org.badvision.outlaweditor.data.xml.Global;
import org.badvision.outlaweditor.data.xml.Mutation;
import org.badvision.outlaweditor.data.xml.Scope;
import org.badvision.outlaweditor.data.xml.Script;
import org.badvision.outlaweditor.data.xml.Statement;
import org.badvision.outlaweditor.data.xml.UserType;
import org.badvision.outlaweditor.data.xml.Variable;
import org.badvision.outlaweditor.spelling.SpellChecker;
@ -69,6 +75,9 @@ public class MythosEditor {
script = theScript;
scope = theScope;
spellChecker = new SpellChecker();
if (script.getBlock() != null) {
fixMutators(script.getBlock());
}
}
public void show() {
@ -226,10 +235,8 @@ public class MythosEditor {
public List<String> getParametersForScript(Script script) {
List<String> allArgs = new ArrayList();
if (script.getBlock() != null && script.getBlock().getFieldOrMutationOrStatement() != null) {
script.getBlock().getFieldOrMutationOrStatement()
.stream().filter((o) -> (o instanceof Mutation))
.map((o) -> (Mutation) o).findFirst().ifPresent((m) -> {
if (script.getBlock() != null) {
extractFirst(script.getBlock(), Mutation.class).ifPresent((m) -> {
m.getArg().stream().forEach((a) -> {
allArgs.add(a.getName());
});
@ -259,4 +266,44 @@ public class MythosEditor {
Logger.getLogger(getClass().getName()).warning(message);
System.out.println(message);
}
public static enum MutationType {
controls_if(MythosEditor::fixIfStatement);
Consumer<Block> rebuildMutation;
MutationType(Consumer<Block> rebuilder) {
rebuildMutation = rebuilder;
}
}
private void fixMutators(Block block) {
extractFirst(block, Mutation.class).ifPresent((mutation)-> {
if (mutation.getOtherAttributes().isEmpty()) {
try {
MutationType type = MutationType.valueOf(block.getType());
type.rebuildMutation.accept(block);
} catch (IllegalArgumentException ex) {
// No big deal, it just doesn't have a mutation we know how to handle
}
}
});
extract(block, Statement.class).map((s)->s.getBlock()).flatMap((l)->l.stream()).forEach(this::fixMutators);
if (block.getNext() != null && block.getNext().getBlock() != null) {
fixMutators(block.getNext().getBlock());
}
}
private static void fixIfStatement(Block block) {
Mutation mutation = extractFirst(block, Mutation.class).get();
long doCount = extract(block, Statement.class).filter((s)->s.getName().startsWith("DO")).collect(Collectors.counting());
long elseCount = extract(block, Statement.class).filter((s)->s.getName().startsWith("ELSE")).collect(Collectors.counting());
if (doCount > 1) {
mutation.getOtherAttributes().put(new QName("elseif"), String.valueOf(doCount - 1));
}
if (elseCount > 0) {
mutation.getOtherAttributes().put(new QName("else"), String.valueOf(elseCount));
}
}
}

View File

@ -7,7 +7,6 @@
* ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package org.badvision.outlaweditor.data;
import java.util.ArrayList;
@ -15,7 +14,10 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import org.badvision.outlaweditor.api.ApplicationState;
import org.badvision.outlaweditor.data.xml.Block;
import org.badvision.outlaweditor.data.xml.Field;
import org.badvision.outlaweditor.data.xml.Global;
import org.badvision.outlaweditor.data.xml.Map;
@ -24,6 +26,7 @@ import org.badvision.outlaweditor.data.xml.Scope;
import org.badvision.outlaweditor.data.xml.Script;
public class DataUtilities {
private DataUtilities() {
}
@ -49,7 +52,7 @@ public class DataUtilities {
return nameA.compareTo(nameB);
});
}
public static void sortNamedEntities(List<? extends NamedEntity> entities) {
if (entities == null) {
return;
@ -88,23 +91,37 @@ public class DataUtilities {
if (script.getName() != null) {
return;
}
script.getBlock().getFieldOrMutationOrStatement().stream()
.filter((obj) -> (obj instanceof Field && ((Field) obj).getName().equalsIgnoreCase("NAME")))
.forEach((obj) -> {
script.setName(((Field) obj).getValue());
});
extract(script.getBlock(), Field.class)
.filter((f) -> f.getName().equalsIgnoreCase("NAME"))
.findFirst().ifPresent(
(f) -> script.setName(f.getValue())
);
}
public static void cleanupScriptNames(Scope s) {
if (s.getScripts() == null || s.getScripts().getScript() == null) return;
if (s.getScripts() == null || s.getScripts().getScript() == null) {
return;
}
s.getScripts().getScript().forEach(DataUtilities::cleanupScriptName);
}
public static void cleanupAllScriptNames() {
cleanupScriptNames(ApplicationState.getInstance().getGameData().getGlobal());
ApplicationState.getInstance().getGameData().getMap().forEach(DataUtilities::cleanupScriptNames);
}
public static <T> Optional<T> extractFirst(Block block, Class<T> desiredType) {
return extract(block, desiredType).findFirst();
}
public static <T> Stream<T> extract(Block block, Class<T> desiredType) {
if (block != null && block.getMutationOrFieldOrValue() != null) {
return (Stream<T>) block.getMutationOrFieldOrValue().stream().filter((o) -> o.getClass().equals(desiredType));
} else {
return Stream.empty();
}
}
//------------------------------ String comparators
/**
* Rank two strings similarity in terms of distance The lower the number,
@ -198,8 +215,8 @@ public class DataUtilities {
@Override
public int compare(String o1, String o2) {
double s1 = levenshteinDistance(match, o1,20);
double s2 = levenshteinDistance(match, o2,20);
double s1 = levenshteinDistance(match, o1, 20);
double s2 = levenshteinDistance(match, o2, 20);
if (s2 == s1) {
s1 = rankMatch(o1, match, 3) + rankMatch(o1, match, 2);
s2 = rankMatch(o2, match, 3) + rankMatch(o2, match, 2);

View File

@ -130,6 +130,7 @@ public class MythosScriptEditorController
}
//TODO: Verify the path conversion works in Win7 with a jar file
// Affected by https://bugs.openjdk.java.net/browse/JDK-8136466
editorView.getEngine().load(getClass().getResource(MYTHOS_EDITOR).toExternalForm());
}

View File

@ -144,13 +144,13 @@
</xs:complexType>
<xs:complexType name="block">
<xs:sequence>
<xs:element minOccurs="0" name="next" type="tns:next"/>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="field" type="tns:field"/>
<xs:element name="mutation" type="tns:mutation"/>
<xs:element name="statement" type="tns:statement"/>
<xs:element name="field" type="tns:field"/>
<xs:element name="value" type="tns:value"/>
<xs:element name="statement" type="tns:statement"/>
</xs:choice>
<xs:element minOccurs="0" name="next" type="tns:next"/>
</xs:sequence>
<xs:attribute name="inline" type="xs:boolean"/>
<xs:attribute name="type" use="required" type="xs:NCName"/>