Skip to content

Commit

Permalink
Some more cleanup and bug fixes.
Browse files Browse the repository at this point in the history
Last commit with API4. Preparing for release.
  • Loading branch information
gravityfox committed Oct 30, 2016
1 parent 4ea2af1 commit c86521c
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 58 deletions.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Wed Oct 19 05:03:09 PDT 2016
#Thu Oct 20 00:08:32 PDT 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ public static FCPattern parseUserRegex(String regex) throws CommandException {

public static final String[] colorNames = {"black", "darkblue", "darkgreen", "darkaqua", "darkred", "darkpurple", "gold", "gray",
"darkgray", "blue", "green", "aqua", "red", "lightpurple", "yellow", "white"};
public static final TextColor[] colors = {BLACK, DARK_BLUE, DARK_GREEN, DARK_AQUA, DARK_RED, DARK_PURPLE, GOLD, GRAY,
DARK_GRAY, BLUE, GREEN, AQUA, RED, LIGHT_PURPLE, YELLOW, WHITE};

public static int colorCodeFromName(String name) {
for (int i = 0; i < colorNames.length; i++) {
Expand All @@ -151,7 +149,7 @@ public static int colorCodeFromName(String name) {
}

public static boolean checkPermissionString(String permission) {
return permission.matches("[a-zA-Z0-9\\-_\\.]") && !permission.matches("^.*\\.\\..*$") && !permission.startsWith(".") && !permission.endsWith(".");
return permission.matches("[a-zA-Z0-9\\-_.]") && !permission.matches("^.*\\.\\..*$") && !permission.startsWith(".") && !permission.endsWith(".");
}

public static class FCPattern {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ public static FoxCoreMain instance() {
}

@Listener
public void gameConstruct(GameConstructionEvent event) {
public void construct(GameConstructionEvent event) {
instance = this;
}

@Listener
public void gamePreInit(GamePreInitializationEvent event) {
public void preInit(GamePreInitializationEvent event) {
logger.info("Beginning FoxCore initialization");
logger.info("Version: " + container.getVersion().orElse("Unknown"));
logger.info("Initializing state manager");
Expand All @@ -112,31 +112,53 @@ public void gamePreInit(GamePreInitializationEvent event) {
}

@Listener
public void gameInit(GameInitializationEvent event) {
public void init(GameInitializationEvent event) {
logger.info("Starting network packet manager");
FCServerNetworkManager.instance().registerNetworkingChannels();
logger.info("Creating server network channel");
foxcoreNetworkChannel = FCServerNetworkManager.instance().getOrCreateServerChannel("foxcore");
logger.info("Registering packet listeners");
this.registerPackets();
registerPackets();
logger.info("Registering positions state field");
FCStateManager.instance().registerStateFactory(new PositionStateField.Factory(), PositionStateField.ID, PositionStateField.ID, Aliases.POSITIONS_ALIASES);
logger.info("Registering wand factories");
registerWands();
logger.info("Registering commands");
game.getCommandManager().register(this, fcDispatcher, "foxcore", "foxc", "fcommon", "fc");
logger.info("Setting default player permissions");
configurePermissions();
logger.info("Registering custom data manipulators");
registerData();
}

@Listener
public void registerListeners(GameInitializationEvent event) {
logger.info("Registering event listeners");
registerListeners();
EventManager manager = game.getEventManager();
try {
manager.registerListeners(this, FCServerNetworkManager.instance());
} catch (Exception e) {
logger.error("Error registering Network Manager Listeners", e);
}
try {
manager.registerListener(this, InteractBlockEvent.class, new WandBlockListener());
} catch (Exception e) {
logger.error("Error registering Wand Block Listener", e);
}
try {
manager.registerListener(this, InteractEntityEvent.class, new WandEntityListener());
} catch (Exception e){
logger.error("Error registering Wand Entity Listener", e);
}
}

@Listener
public void registerData(GameInitializationEvent event) {
logger.info("Registering custom data manipulators");
game.getDataManager().register(WandData.class, ImmutableWandData.class, new WandDataBuilder());
}

@Listener
public void gamePostInit(GamePostInitializationEvent event){
public void configurePermissions(GamePostInitializationEvent event) {
logger.info("Configuring permissions");
configurePermissions();
game.getServiceManager().provide(PermissionService.class).get()
.getDefaultData().setPermission(SubjectData.GLOBAL_CONTEXT, "foxcommon.command.info", Tristate.TRUE);
}

private void configureCommands() {
Expand All @@ -151,7 +173,7 @@ private void configureCommands() {
fcDispatcher.register(new CommandPosition(), "position", "pos", "p");
fcDispatcher.register(new CommandFlush(), "flush", "clear", "wipe", "f");
fcDispatcher.register(new CommandWand(), "wand", "tool", "stick", "w");
fcDispatcher.register(new CommandTest(), "test");
//fcDispatcher.register(new CommandTest(), "test");
fcDispatcher.register(new CommandDebug(), "debug");
fcDispatcher.register(new CommandHUD(), "hud", "scoreboard");

Expand All @@ -164,27 +186,12 @@ private void registerPackets() {
manager.registerPacket(ServerPrintStringPacket.ID);
}

private void registerListeners() {
EventManager manager = game.getEventManager();
manager.registerListener(this, InteractBlockEvent.class, new WandBlockListener());
manager.registerListener(this, InteractEntityEvent.class, new WandEntityListener());
manager.registerListeners(this, FCServerNetworkManager.instance());
}

private void registerData() {
game.getDataManager().register(WandData.class, ImmutableWandData.class, new WandDataBuilder());
}

private void registerWands(){
private void registerWands() {
FCWandRegistry registry = FCWandRegistry.getInstance();
registry.registerBuilder(PositionWand.type, new PositionWand.Factory());
registry.registerBuilder(CounterWand.type, new CounterWand.Factory());
}

private void configurePermissions() {
game.getServiceManager().provide(PermissionService.class).get()
.getDefaultData().setPermission(SubjectData.GLOBAL_CONTEXT, "foxcommon.command.info", Tristate.TRUE);
}

public Logger logger() {
return logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import com.google.common.collect.ImmutableList;
import net.foxdenstudio.sponge.foxcore.plugin.FoxCoreMain;
import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParser;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
Expand All @@ -43,24 +44,36 @@

public class CommandTest extends FCCommandBase {

/*
public CommandResult process(CommandSource source, String arguments) throws CommandException {
if (!testPermission(source)) {
source.sendMessage(Text.of(TextColors.RED, "You don't have permission to use this command!"));
return CommandResult.empty();
}
AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).limit(3).parse2();
AdvCmdParser.ParseResult parse = AdvCmdParser.builder()
.arguments(arguments)
.limit(3)
.parse();

Text.Builder builder = Text.builder();
builder.append(Text.of(TextColors.GOLD, "-----------------------------\n"));
builder.append(Text.of(TextColors.GOLD, "Args: \"", TextColors.RESET, arguments, TextColors.GOLD, "\"\n"));
builder.append(Text.of(TextColors.GOLD, "Type: ", TextColors.RESET, parse.currentElement.type, TextColors.GOLD, "\n"));
builder.append(Text.of(TextColors.GOLD, "Token: \"", TextColors.RESET, parse.currentElement.token, TextColors.GOLD, "\"\n"));
builder.append(Text.of(TextColors.GOLD, "Index: ", TextColors.RESET, parse.currentElement.index, TextColors.GOLD, "\n"));
builder.append(Text.of(TextColors.GOLD, "Key: \"", TextColors.RESET, parse.currentElement.key, TextColors.GOLD, "\"\n"));
builder.append(Text.of(TextColors.GOLD, "Raw: \"", TextColors.RESET, arguments, TextColors.GOLD, "\"\n"));
builder.append(Text.of(TextColors.GOLD, "Args: \""));

for (int i = 0; i < parse.args.length; i++) {
builder.append(Text.of(TextColors.RESET, parse.args[i]));
if(i < parse.args.length - 1) builder.append(Text.of(TextColors.GOLD, ", "));
}
builder.append(Text.of(TextColors.GOLD, "\"\n"));

builder.append(Text.of(TextColors.GOLD, "Flags: \"", TextColors.RESET, parse.flags, TextColors.GOLD, "\"\n"));

builder.append(Text.of(TextColors.GOLD, "Type: ", TextColors.RESET, parse.current.type, TextColors.GOLD, "\n"));
builder.append(Text.of(TextColors.GOLD, "Token: \"", TextColors.RESET, parse.current.token, TextColors.GOLD, "\"\n"));
builder.append(Text.of(TextColors.GOLD, "Index: ", TextColors.RESET, parse.current.index, TextColors.GOLD, "\n"));
builder.append(Text.of(TextColors.GOLD, "Key: \"", TextColors.RESET, parse.current.key, TextColors.GOLD, "\"\n"));
source.sendMessage(builder.build());
return CommandResult.empty();
}
*/

/*@Override
public List<String> getSuggestions(CommandSource source, String arguments) throws CommandException {
Expand Down Expand Up @@ -109,7 +122,7 @@ public Text getUsage(CommandSource source) {
}


@Override
/*@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
if (!testPermission(source)) {
source.sendMessage(Text.of(TextColors.RED, "You don't have permission to use this command!"));
Expand All @@ -123,5 +136,5 @@ public CommandResult process(CommandSource source, String arguments) throws Comm
source.sendMessage(Text.of("You must be a player to use this command!"));
}
return CommandResult.empty();
}
}*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ public ParseResult parse() throws CommandException {
boolean jump = false;
// Stores flags that were not accepted by the mapper, to be given to the final block, if it exists.
List<String> extraFlags = new ArrayList<>();
String finalBlock = "";
// Iterate through matches
while (matcher.find()) {

Expand Down Expand Up @@ -251,10 +252,15 @@ public ParseResult parse() throws CommandException {
// Simply adds the result to the argument list. Quotes are trimmed.
// Fallback if the result isn't a flag.
} else {
if (limit > 0 && argsList.size() == limit && result.equals(">")) {
jump = true;
continue;
}

if (leaveFinalAsIs && limit > 0 && argsList.size() >= limit) {
String finalBlock = arguments.substring(matcher.start(), arguments.length() - (inQuote ? 1 : 0));
finalBlock = arguments.substring(matcher.start(), arguments.length() - (inQuote ? 1 : 0));
parseResult.current = new CurrentElement(CurrentElement.ElementType.FINAL, finalBlock, argsList.size(), "");
argsList.add(finalBlock);

lastIsCurrent = true;
break;
}
Expand Down Expand Up @@ -290,24 +296,26 @@ public ParseResult parse() throws CommandException {
// A number of arguments are copied to a new list less than or equal to the limit.
// The rest of the arguments, if any, are concatenated together.
List<String> finalList = new ArrayList<>();
String finalString = "";
for (int i = 0; i < argsList.size(); i++) {
if (limit <= 0 || i < limit) {
finalList.add(argsList.get(i));
} else {
finalString += argsList.get(i);
finalBlock += argsList.get(i);
if (i + 1 < argsList.size()) {
finalString += " ";
finalBlock += " ";
}
}
}
for(String flag : extraFlags){
finalBlock = flag + " " + finalBlock;
}

if (parseResult.current != null && parseResult.current.type == CurrentElement.ElementType.ARGUMENT && limit > 0 && parseResult.current.index >= limit)
parseResult.current = new CurrentElement(CurrentElement.ElementType.FINAL, finalString + (finalString.isEmpty() || lastIsCurrent ? "" : " "), finalList.size(), "");
parseResult.current = new CurrentElement(CurrentElement.ElementType.FINAL, finalBlock + (finalBlock.isEmpty() || lastIsCurrent ? "" : " "), finalList.size(), "");

if (!finalString.isEmpty()) {
if (!finalBlock.isEmpty()) {
if (!excludeCurrent || parseResult.current.type == CurrentElement.ElementType.COMMENT) {
finalList.add(finalString);
finalList.add(finalBlock);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.spongepowered.api.text.Text;

import javax.annotation.Nullable;
import java.util.Optional;

public final class ProcessResult {
Expand All @@ -35,19 +36,19 @@ public final class ProcessResult {
private static final ProcessResult FAILURE = of(false);

private final boolean success;
private final Optional<Text> message;
private final @Nullable Text message;

private ProcessResult(boolean success, Optional<Text> message) {
private ProcessResult(boolean success, @Nullable Text message) {
this.success = success;
this.message = message;
}

public static ProcessResult of(boolean success) {
return new ProcessResult(success, Optional.empty());
return new ProcessResult(success, null);
}

public static ProcessResult of(boolean success, Text message) {
return new ProcessResult(success, Optional.of(message));
return new ProcessResult(success, message);
}

public static ProcessResult success() {
Expand All @@ -63,6 +64,6 @@ public boolean isSuccess() {
}

public Optional<Text> getMessage() {
return message;
return Optional.ofNullable(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.foxdenstudio.sponge.foxcore.plugin.util;

import com.flowpowered.math.vector.Vector3i;
import net.foxdenstudio.sponge.foxcore.common.network.server.packet.ServerPositionPacket;
import net.foxdenstudio.sponge.foxcore.common.util.FCCUtil;
import net.foxdenstudio.sponge.foxcore.plugin.FoxCoreMain;
Expand Down Expand Up @@ -35,10 +34,17 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.spongepowered.api.text.format.TextColors.*;

/**
* Created by Fox on 7/7/2016.
*/
public class FCPUtil {

public static Text readableBooleanText(boolean bool) {
return bool ? Text.of(TextColors.GREEN, "True") : Text.of(TextColors.RED, "False");
}

public static String readableTristate(Tristate state) {
switch (state) {
case UNDEFINED:
Expand Down Expand Up @@ -102,16 +108,60 @@ public static void updatePositions(Player player) {
FoxCoreMain.instance().getFoxcoreNetworkChannel().sendPacket(player, new ServerPositionPacket(getPositions(player)));
}

public static final TextColor[] colors = {BLACK, DARK_BLUE, DARK_GREEN, DARK_AQUA, DARK_RED, DARK_PURPLE, GOLD, GRAY,
DARK_GRAY, BLUE, GREEN, AQUA, RED, LIGHT_PURPLE, YELLOW, WHITE};

public static Optional<TextColor> textColorFromName(String name) {
int code = FCCUtil.colorCodeFromName(name);
if (code >= 0) return Optional.of(FCCUtil.colors[code]);
if (code >= 0) return Optional.of(colors[code]);
else return Optional.empty();
}

public static Optional<TextColor> textColorFromHex(String hex) {
if (!hex.matches("[0-9a-f]")) return Optional.empty();
int code = Integer.parseInt(hex, 16);
return Optional.of(FCCUtil.colors[code]);
return Optional.of(colors[code]);
}

public static String getColorName(TextColor color, boolean upperCase) {
switch (color.getId()) {
case "RED":
return upperCase ? "Red" : "red";
case "YELLOW":
return upperCase ? "Yellow" : "yellow";
case "GREEN":
return upperCase ? "Green" : "green";
case "AQUA":
return upperCase ? "Aqua" : "aqua";
case "BLUE":
return upperCase ? "Blue" : "blue";
case "LIGHT_PURPLE":
return upperCase ? "Light purple" : "light purple";
case "DARK_RED":
return upperCase ? "Dark red" : "dark red";
case "GOLD":
return upperCase ? "Gold" : "gold";
case "DARK_GREEN":
return upperCase ? "Dark green" : "dark green";
case "DARK_AQUA":
return upperCase ? "Dark aqua" : "dark aqua";
case "DARK_BLUE":
return upperCase ? "Dark blue" : "dark blue";
case "DARK_PURPLE":
return upperCase ? "Dark purple" : "dark puple";
case "WHITE":
return upperCase ? "White" : "white";
case "GRAY":
return upperCase ? "Gray" : "gray";
case "DARK_GRAY":
return upperCase ? "Dark gray" : "dark gray";
case "BLACK":
return upperCase ? "Black" : "black";
case "RESET":
return upperCase ? "Reset" : "reset";
default:
return "What?";
}
}

public static CommentedConfigurationNode getHOCONConfiguration(Path file, ConfigurationLoader<CommentedConfigurationNode> loader) {
Expand Down

0 comments on commit c86521c

Please sign in to comment.