Skip to content

Commit

Permalink
Yet another update that breaks everything.
Browse files Browse the repository at this point in the history
Objects can now be enabled and disabled without being deleted.
  • Loading branch information
gravityfox committed Nov 30, 2015
1 parent 31235a4 commit 4276e63
Show file tree
Hide file tree
Showing 26 changed files with 312 additions and 70 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import java.nio.file.Files
import java.nio.file.StandardCopyOption

group 'tk.elektrofuchse.fox.foxguard'
version '0.9.1-SNAPSHOT'
version '0.10-SNAPSHOT'

apply plugin: 'java'

Expand Down
4 changes: 4 additions & 0 deletions permissionslist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ foxguard {
handlers.own
regions.own
}
enabledisable{
handlers.own
regions.own
}
modify{
handlers.own
regions.own
Expand Down
78 changes: 52 additions & 26 deletions src/main/java/net/gravityfox/foxguard/FGStorageManager.java

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/main/java/net/gravityfox/foxguard/FoxGuardMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
@Plugin(id = "foxguard", name = "FoxGuard", version = FoxGuardMain.PLUGIN_VERSION)
public class FoxGuardMain {

public static final String PLUGIN_VERSION = "0.9.1";
public static final String PLUGIN_VERSION = "0.10";

private static FoxGuardMain instance;

Expand Down Expand Up @@ -167,6 +167,8 @@ private void registerCommands() {
fgDispatcher.register(new CommandModify(), "modify", "mod", "change", "edit", "update");
fgDispatcher.register(new CommandLink(), "link", "connect", "attach");
fgDispatcher.register(new CommandUnlink(), "unlink", "disconnect", "detach");
fgDispatcher.register(new CommandEnableDisable(true), "enable", "activate","engage", "on");
fgDispatcher.register(new CommandEnableDisable(false), "disable", "deactivate", "disengage", "off");
fgDispatcher.register(new CommandList(), "list", "ls");
fgDispatcher.register(new CommandDetail(), "detail", "det", "show");
fgDispatcher.register(new CommandState(), "state", "current", "cur");
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/gravityfox/foxguard/IFGObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public interface IFGObject {

String getUniqueTypeString();

boolean isEnabled();

void setIsEnabled(boolean state);

Text getDetails(String arguments);

void writeToDatabase(DataSource dataSource) throws SQLException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm
builder.append(Texts.of(TextColors.GREEN, "---General---\n"));
builder.append(Texts.of(TextColors.GOLD, "Name: "), Texts.of(TextColors.RESET, region.getName() + "\n"));
builder.append(Texts.of(TextColors.GOLD, "Type: "), Texts.of(TextColors.RESET, region.getLongTypeName() + "\n"));
builder.append(Texts.of(TextColors.GOLD, "Enabled: "), Texts.of(TextColors.RESET, (region.isEnabled() ? "True" : "False") + "\n"));
builder.append(Texts.of(TextColors.GOLD, "World: "), Texts.of(TextColors.RESET, region.getWorld().getName() + "\n"));
builder.append(Texts.of(TextColors.GREEN, "---Details---\n"));
builder.append(region.getDetails(args.length < 3 + flag ? "" : args[2 + flag]));
Expand All @@ -113,6 +114,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm
builder.append(Texts.of(TextColors.GREEN, "---General---\n"));
builder.append(Texts.of(TextColors.GOLD, "Name: "), Texts.of(TextColors.RESET, handler.getName() + "\n"));
builder.append(Texts.of(TextColors.GOLD, "Type: "), Texts.of(TextColors.RESET, handler.getLongTypeName() + "\n"));
builder.append(Texts.of(TextColors.GOLD, "Enabled: "), Texts.of(TextColors.RESET, (handler.isEnabled() ? "True" : "False") + "\n"));
builder.append(Texts.of(TextColors.GOLD, "Priority: "), Texts.of(TextColors.RESET, handler.getPriority() + "\n"));
builder.append(Texts.of(TextColors.GREEN, "---Details---\n"));
builder.append(handler.getDetails(args.length < 3 ? "" : args[2]));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* This file is part of FoxGuard, licensed under the MIT License (MIT).
*
* Copyright (c) 2015 - 2015. gravityfox - https://gravityfox.net/ and contributors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package net.gravityfox.foxguard.commands;

import net.gravityfox.foxguard.FGManager;
import net.gravityfox.foxguard.FoxGuardMain;
import net.gravityfox.foxguard.handlers.GlobalHandler;
import net.gravityfox.foxguard.handlers.IHandler;
import net.gravityfox.foxguard.regions.GlobalRegion;
import net.gravityfox.foxguard.regions.IRegion;
import net.gravityfox.foxguard.util.FGHelper;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.Texts;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.util.command.CommandCallable;
import org.spongepowered.api.util.command.CommandException;
import org.spongepowered.api.util.command.CommandResult;
import org.spongepowered.api.util.command.CommandSource;
import org.spongepowered.api.util.command.args.ArgumentParseException;
import org.spongepowered.api.world.World;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import static net.gravityfox.foxguard.util.Aliases.*;

/**
* Created by Fox on 11/29/2015.
* Project: SpongeForge
*/
public class CommandEnableDisable implements CommandCallable {

private final boolean enableState;

public CommandEnableDisable(boolean enableState) {
this.enableState = enableState;
}

@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
if (!testPermission(source)) {
source.sendMessage(Texts.of(TextColors.RED, "You don't have permission to use this command!"));
return CommandResult.empty();
}
String[] args = {};
if (!arguments.isEmpty()) args = arguments.split(" +");
if (source instanceof Player) {
Player player = (Player) source;
if (args.length == 0) {
source.sendMessage(Texts.builder()
.append(Texts.of(TextColors.GREEN, "Usage: "))
.append(getUsage(source))
.build());
return CommandResult.empty();
} else if (isAlias(REGIONS_ALIASES, args[0])) {
if (args.length < 2) throw new CommandException(Texts.of("Must specify a name!"));
int flag = 0;
Optional<World> optWorld = FGHelper.parseWorld(args[1], FoxGuardMain.getInstance().getGame().getServer());
World world;
if (optWorld != null && optWorld.isPresent()) {
world = optWorld.get();
flag = 1;
} else world = player.getWorld();
if (args.length < 2 + flag) throw new CommandException(Texts.of("Must specify a name!"));
int successes = 0;
int failures = 0;
for (String name : Arrays.copyOfRange(args, 1 + flag, args.length)) {
IRegion region = FGManager.getInstance().getRegion(world, name);
if (region == null) failures++;
else if (region instanceof GlobalRegion || region.isEnabled() == this.enableState) failures++;
else {
region.setIsEnabled(this.enableState);
successes++;
}
}
if (successes > 0) {
source.sendMessage(Texts.of(TextColors.GREEN, "Successfully " + (this.enableState ? "enabled" : "disabled") + " regions with "
+ successes + " successes and " + failures + " failures!"));
return CommandResult.builder().successCount(successes).build();
} else {
throw new CommandException(Texts.of(failures + " failures while trying to " + (this.enableState ? "enable" : "disable") +
" regions. Check to make sure you spelled their names correctly and that they are not already "
+ (this.enableState ? "enabled." : "disabled.")));
}

} else if (isAlias(HANDLERS_ALIASES, args[0])) {
if (args.length < 2) throw new CommandException(Texts.of("Must specify a name!"));
int successes = 0;
int failures = 0;
for (String name : Arrays.copyOfRange(args, 1, args.length)) {
IHandler handler = FGManager.getInstance().gethandler(name);
if (handler == null) failures++;
else if (handler instanceof GlobalHandler || handler.isEnabled() == this.enableState) {
failures++;
} else {
handler.setIsEnabled(this.enableState);
successes++;
}
}
if (successes > 0) {
source.sendMessage(Texts.of(TextColors.GREEN, "Successfully " + (this.enableState ? "enabled" : "disabled") + " handlers with "
+ successes + " successes and " + failures + " failures!"));
return CommandResult.builder().successCount(successes).build();
} else {
throw new CommandException(Texts.of(failures + " failures while trying to " + (this.enableState ? "enable" : "disable") +
" handlers. Check to make sure you spelled their names correctly and that they are not already "
+ (this.enableState ? "enabled." : "disabled.")));
}
} else throw new ArgumentParseException(Texts.of("Not a valid category!"), args[0], 0);
} else {

}
return CommandResult.empty();
}

@Override
public List<String> getSuggestions(CommandSource source, String arguments) throws CommandException {
return null;
}

@Override
public boolean testPermission(CommandSource source) {
return source.hasPermission("foxguard.command.modify.objects.enabledisable");
}

@Override
public Optional<? extends Text> getShortDescription(CommandSource source) {
return Optional.empty();
}

@Override
public Optional<? extends Text> getHelp(CommandSource source) {
return Optional.empty();
}

@Override
public Text getUsage(CommandSource source) {
if (source instanceof Player)
return Texts.of((this.enableState ? "enable" : "disable") + " <region [w:<worldname>] | handler> [names]...");
else return Texts.of((this.enableState ? "enable" : "disable") + " <region <worldname> | handler> [names]...");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ public IRegion createRegion(String name, String type, String args, InternalComma
return null;
}

public IRegion createRegion(DataSource source, String name, String type) throws SQLException {
public IRegion createRegion(DataSource source, String name, String type, boolean isEnabled) throws SQLException {
for (IRegionFactory rf : regionFactories) {
if (isAlias(rf.getTypes(), type)) {
IRegion region = rf.createRegion(source, name, type);
IRegion region = rf.createRegion(source, name, type, isEnabled);
if (region != null) return region;
}
}
Expand All @@ -90,10 +90,10 @@ public IHandler createHandler(String name, String type, int priority, String arg
return null;
}

public IHandler createHandler(DataSource source, String name, String type, int priority) throws SQLException {
public IHandler createHandler(DataSource source, String name, String type, int priority, boolean isEnabled) throws SQLException {
for (IHandlerFactory fsf : handlerFactories) {
if (isAlias(fsf.getTypes(), type)) {
IHandler handler = fsf.createHandler(source, name, type, priority);
IHandler handler = fsf.createHandler(source, name, type, priority, isEnabled);
if (handler != null) return handler;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import net.gravityfox.foxguard.util.FGHelper;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.User;
import org.spongepowered.api.service.permission.PermissionService;
import org.spongepowered.api.util.Tristate;
import org.spongepowered.api.util.command.CommandSource;

Expand Down Expand Up @@ -82,11 +83,11 @@ public IHandler createHandler(String name, String type, int priority, String arg
return handler;
} else if (Aliases.isAlias(permissionAliases, type)) {
return new PermissionHandler(name, priority);
}else return null;
} else return null;
}

@Override
public IHandler createHandler(DataSource source, String name, String type, int priority) throws SQLException {
public IHandler createHandler(DataSource source, String name, String type, int priority, boolean isEnabled) throws SQLException {
if (type.equalsIgnoreCase("simple")) {
List<User> ownerList = new LinkedList<>();
List<User> memberList = new LinkedList<>();
Expand Down Expand Up @@ -157,6 +158,7 @@ public IHandler createHandler(DataSource source, String name, String type, int p
handler.setOwners(ownerList);
handler.setMembers(memberList);
handler.setPassiveOption(po);
handler.setIsEnabled(isEnabled);
return handler;
} else if (type.equalsIgnoreCase("passive")) {
List<User> ownerList = new LinkedList<>();
Expand All @@ -183,9 +185,12 @@ public IHandler createHandler(DataSource source, String name, String type, int p
}
PassiveHandler handler = new PassiveHandler(name, priority, flagMap);
handler.setOwners(ownerList);
handler.setIsEnabled(isEnabled);
return handler;
} else if (type.equalsIgnoreCase("permission")) {
return new PermissionHandler(name, priority);
PermissionHandler handler = new PermissionHandler(name, priority);
handler.setIsEnabled(isEnabled);
return handler;
} else return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public IRegion createRegion(String name, String type, String arguments, Internal
}

@Override
public IRegion createRegion(DataSource source, String name, String type) throws SQLException {
public IRegion createRegion(DataSource source, String name, String type, boolean isEnabled) throws SQLException {
if (type.equalsIgnoreCase("rectangular")) {
Vector2i a, b;
List<User> userList = new LinkedList<>();
Expand All @@ -107,6 +107,7 @@ public IRegion createRegion(DataSource source, String name, String type) throws
}
RectangularRegion region = new RectangularRegion(name, new BoundingBox2(a, b));
region.setOwners(userList);
region.setIsEnabled(isEnabled);
return region;
} else if (type.equalsIgnoreCase("cuboid")) {
Vector3i a, b;
Expand All @@ -129,6 +130,7 @@ public IRegion createRegion(DataSource source, String name, String type) throws
}
CuboidRegion region = new CuboidRegion(name, new BoundingBox3(a, b));
region.setOwners(userList);
region.setIsEnabled(isEnabled);
return region;
} else if (type.equalsIgnoreCase("elevation")) {
int lowerBound, upperBound;
Expand All @@ -152,6 +154,7 @@ public IRegion createRegion(DataSource source, String name, String type) throws
}
ElevationRegion region = new ElevationRegion(name, lowerBound, upperBound);
region.setOwners(userList);
region.setIsEnabled(isEnabled);
return region;
} else return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public interface IHandlerFactory extends IFGFactory {

IHandler createHandler(String name, String type, int priority, String arguments, InternalCommandState state, CommandSource source);

IHandler createHandler(DataSource source, String name, String type, int priority) throws SQLException;
IHandler createHandler(DataSource source, String name, String type, int priority, boolean isEnabled) throws SQLException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ public interface IRegionFactory extends IFGFactory {

IRegion createRegion(String name, String type, String arguments, InternalCommandState state, World world, CommandSource source) throws CommandException;

IRegion createRegion(DataSource source, String name, String type) throws SQLException;
IRegion createRegion(DataSource source, String name, String type, boolean isEnabled) throws SQLException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,16 @@ public Text getDetails(String arguments) {

@Override
public void writeToDatabase(DataSource dataSource) {
}

@Override
public boolean isEnabled() {
return true;
}

@Override
public void setIsEnabled(boolean state) {
this.isEnabled = true;
}

@Override
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/net/gravityfox/foxguard/handlers/IHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ public interface IHandler extends IFGObject, Comparable<IHandler> {

Tristate handle(@Nullable User user, Flags flag, Event event);

boolean isEnabled();

void setIsEnabled(boolean state);

int getPriority();

void setPriority(int priority);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public PassiveHandler(String name, int priority, CallbackHashMap<Flags, Tristate

@Override
public Tristate handle(@Nullable User user, Flags flag, Event event) {
if (!isEnabled) return Tristate.UNDEFINED;
if (user != null) return Tristate.UNDEFINED;
if (!isEnabled || user != null) return Tristate.UNDEFINED;
if (FGHelper.contains(availableFlags, flag)) {
return passiveMap.get(flag);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public PermissionHandler(String name, int priority) {

@Override
public Tristate handle(@Nullable User user, Flags flag, Event event) {
if (user == null) return Tristate.UNDEFINED;
if (!isEnabled || user == null) return Tristate.UNDEFINED;
if (user.hasPermission("foxguard.flags." + this.name + "." + flag.flagName() + ".allow"))
return Tristate.TRUE;
else if (user.hasPermission("foxguard.flags." + this.name + "." + flag.flagName() + ".deny"))
Expand Down
Loading

0 comments on commit 4276e63

Please sign in to comment.