From 7bdb08365c25dc559544ab44a68530e765be5f61 Mon Sep 17 00:00:00 2001 From: gravityfox Date: Fri, 15 Jul 2016 18:09:55 -0700 Subject: [PATCH] Lots of region bug fixes --- FoxCore | 2 +- .../sponge/foxguard/plugin/FGManager.java | 4 + .../plugin/command/CommandDetail.java | 4 +- .../plugin/command/CommandEnableDisable.java | 57 +-- .../foxguard/plugin/handler/GroupHandler.java | 10 +- .../listener/PlayerMoveListenerNew.java | 358 ++++++++++++++++++ .../foxguard/plugin/region/GlobalRegion.java | 3 +- .../foxguard/plugin/region/RegionBase.java | 7 + .../plugin/region/world/CuboidRegion.java | 1 + .../plugin/region/world/ElevationRegion.java | 2 + .../region/world/RectangularRegion.java | 1 + .../foxguard/plugin/util/RegionCache.java | 4 + 12 files changed, 401 insertions(+), 52 deletions(-) create mode 100644 src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/PlayerMoveListenerNew.java diff --git a/FoxCore b/FoxCore index bf360b7..9e941ed 160000 --- a/FoxCore +++ b/FoxCore @@ -1 +1 @@ -Subproject commit bf360b7fbd4378d078b77f93650c6e839acebeb6 +Subproject commit 9e941ed8c6ad0a132bf2e22db9fddbdc17adb8cc diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FGManager.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FGManager.java index f78a0cd..7420f84 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FGManager.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FGManager.java @@ -395,4 +395,8 @@ public void markDirty(IRegion region, RegionCache.DirtyType type) { regionCache.markDirty(region, type); } + public void clearRegionCache(){ + this.regionCache.clearCaches(); + } + } diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandDetail.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandDetail.java index aaa6e5f..629c985 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandDetail.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandDetail.java @@ -110,9 +110,9 @@ public CommandResult process(CommandSource source, String arguments) throws Comm Text.Builder builder = Text.builder(); builder.append(Text.of(TextColors.GOLD, "\n-----------------------------------------------------\n")); if (parse.args.length < 3 || parse.args[2].isEmpty() || parse.flags.containsKey("all")) { - builder.append(Text.of(TextColors.GREEN, "------- General -------\n")); builder.append(Text.of(TextActions.runCommand("/foxguard detail region " + FGUtil.genWorldFlag(region) + region.getName()), TextActions.showText(Text.of("View details for region \"" + region.getName() + "\"")), + TextColors.GREEN, "------- General -------\n", TextColors.GOLD, "Name: ", TextColors.RESET, region.getName() + "\n")); builder.append(Text.of(TextColors.GOLD, "Type: "), Text.of(TextColors.RESET, region.getLongTypeName() + "\n")); builder.append(Text.builder() @@ -146,9 +146,9 @@ public CommandResult process(CommandSource source, String arguments) throws Comm Text.Builder builder = Text.builder(); builder.append(Text.of(TextColors.GOLD, "\n-----------------------------------------------------\n")); if (parse.args.length <= 2 || parse.args[2].isEmpty() || parse.flags.containsKey("all")) { - builder.append(Text.of(TextColors.GREEN, "------- General -------\n")); builder.append(Text.of(TextActions.runCommand("/foxguard detail handler " + handler.getName()), TextActions.showText(Text.of("View details for handler \"" + handler.getName() + "\"")), + TextColors.GREEN, "------- General -------\n", TextColors.GOLD, "Name: ", TextColors.RESET, handler.getName() + "\n")); builder.append(Text.of(TextColors.GOLD, "Type: "), Text.of(TextColors.RESET, handler.getLongTypeName() + "\n")); builder.append(Text.builder() diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandEnableDisable.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandEnableDisable.java index 449f68a..b90ef92 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandEnableDisable.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandEnableDisable.java @@ -31,16 +31,18 @@ import net.foxdenstudio.sponge.foxcore.plugin.state.FCStateManager; import net.foxdenstudio.sponge.foxguard.plugin.FGManager; import net.foxdenstudio.sponge.foxguard.plugin.FoxGuardMain; -import net.foxdenstudio.sponge.foxguard.plugin.event.FGUpdateEvent; +import net.foxdenstudio.sponge.foxguard.plugin.event.util.FGEventFactory; import net.foxdenstudio.sponge.foxguard.plugin.handler.GlobalHandler; import net.foxdenstudio.sponge.foxguard.plugin.handler.IHandler; import net.foxdenstudio.sponge.foxguard.plugin.object.IFGObject; +import net.foxdenstudio.sponge.foxguard.plugin.object.IGlobal; import net.foxdenstudio.sponge.foxguard.plugin.region.IRegion; import net.foxdenstudio.sponge.foxguard.plugin.region.world.GlobalWorldRegion; import net.foxdenstudio.sponge.foxguard.plugin.region.world.IWorldRegion; import net.foxdenstudio.sponge.foxguard.plugin.state.HandlersStateField; import net.foxdenstudio.sponge.foxguard.plugin.state.RegionsStateField; import net.foxdenstudio.sponge.foxguard.plugin.util.FGUtil; +import net.foxdenstudio.sponge.foxguard.plugin.util.RegionCache; import org.spongepowered.api.Sponge; import org.spongepowered.api.command.CommandCallable; import org.spongepowered.api.command.CommandException; @@ -48,7 +50,6 @@ import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.command.args.ArgumentParseException; import org.spongepowered.api.entity.living.player.Player; -import org.spongepowered.api.event.cause.Cause; import org.spongepowered.api.text.Text; import org.spongepowered.api.text.format.TextColors; import org.spongepowered.api.util.GuavaCollectors; @@ -108,21 +109,11 @@ public CommandResult process(CommandSource source, String arguments) throws Comm } FCStateManager.instance().getStateMap().get(source).flush(RegionsStateField.ID, HandlersStateField.ID); if (successes == 1 && failures == 0) { - Sponge.getGame().getEventManager().post(new FGUpdateEvent() { - @Override - public Cause getCause() { - return FoxGuardMain.getCause(); - } - }); + Sponge.getGame().getEventManager().post(FGEventFactory.createFGUpdateEvent(FoxGuardMain.getCause())); source.sendMessage(Text.of(TextColors.GREEN, "Successfully " + (this.enableState ? "enabled" : "disabled") + " object!")); return CommandResult.success(); } else if (successes > 0) { - Sponge.getGame().getEventManager().post(new FGUpdateEvent() { - @Override - public Cause getCause() { - return FoxGuardMain.getCause(); - } - }); + Sponge.getGame().getEventManager().post(FGEventFactory.createFGUpdateEvent(FoxGuardMain.getCause())); source.sendMessage(Text.of(TextColors.GREEN, "Successfully " + (this.enableState ? "enabled" : "disabled") + " objects with " + successes + " successes" + (failures > 0 ? " and " + failures + " failures!" : "!"))); return CommandResult.builder().successCount(successes).build(); @@ -158,7 +149,7 @@ public Cause getCause() { } if (regions.isEmpty()) throw new CommandException(Text.of("Must specify at least one region!")); for (IRegion region : regions) { - if (region instanceof GlobalWorldRegion || region.isEnabled() == this.enableState) failures++; + if (region instanceof IGlobal || region.isEnabled() == this.enableState) failures++; else { region.setIsEnabled(this.enableState); successes++; @@ -166,21 +157,11 @@ public Cause getCause() { } FCStateManager.instance().getStateMap().get(source).flush(RegionsStateField.ID); if (successes == 1 && failures == 0) { - Sponge.getGame().getEventManager().post(new FGUpdateEvent() { - @Override - public Cause getCause() { - return FoxGuardMain.getCause(); - } - }); + Sponge.getGame().getEventManager().post(FGEventFactory.createFGUpdateEvent(FoxGuardMain.getCause())); source.sendMessage(Text.of(TextColors.GREEN, "Successfully " + (this.enableState ? "enabled" : "disabled") + " region!")); return CommandResult.success(); } else if (successes > 0) { - Sponge.getGame().getEventManager().post(new FGUpdateEvent() { - @Override - public Cause getCause() { - return FoxGuardMain.getCause(); - } - }); + Sponge.getGame().getEventManager().post(FGEventFactory.createFGUpdateEvent(FoxGuardMain.getCause())); source.sendMessage(Text.of(TextColors.GREEN, "Successfully " + (this.enableState ? "enabled" : "disabled") + " regions with " + successes + " successes" + (failures > 0 ? " and " + failures + " failures!" : "!"))); return CommandResult.builder().successCount(successes).build(); @@ -205,7 +186,7 @@ public Cause getCause() { } if (handlers.isEmpty()) throw new CommandException(Text.of("Must specify at least one handler!")); for (IHandler handler : handlers) { - if (handler instanceof GlobalHandler || handler.isEnabled() == this.enableState) failures++; + if (handler instanceof IGlobal || handler.isEnabled() == this.enableState) failures++; else { handler.setIsEnabled(this.enableState); successes++; @@ -213,21 +194,11 @@ public Cause getCause() { } FCStateManager.instance().getStateMap().get(source).flush(HandlersStateField.ID); if (successes == 1 && failures == 0) { - Sponge.getGame().getEventManager().post(new FGUpdateEvent() { - @Override - public Cause getCause() { - return FoxGuardMain.getCause(); - } - }); + Sponge.getGame().getEventManager().post(FGEventFactory.createFGUpdateEvent(FoxGuardMain.getCause())); source.sendMessage(Text.of(TextColors.GREEN, "Successfully " + (this.enableState ? "enabled" : "disabled") + " handler!")); return CommandResult.success(); } else if (successes > 0) { - Sponge.getGame().getEventManager().post(new FGUpdateEvent() { - @Override - public Cause getCause() { - return FoxGuardMain.getCause(); - } - }); + Sponge.getGame().getEventManager().post(FGEventFactory.createFGUpdateEvent(FoxGuardMain.getCause())); source.sendMessage(Text.of(TextColors.GREEN, "Successfully " + (this.enableState ? "enabled" : "disabled") + " handlers with " + successes + " successes" + (failures > 0 ? " and " + failures + " failures!" : "!"))); return CommandResult.builder().successCount(successes).build(); @@ -266,14 +237,14 @@ else if (parse.current.index > 0) { } } if (world == null) return FGManager.getInstance().getRegions().stream() - .filter(region -> region.isEnabled() != this.enableState && !(region instanceof GlobalWorldRegion)) + .filter(region -> region.isEnabled() != this.enableState && !(region instanceof IGlobal)) .map(IFGObject::getName) .filter(new StartsWithPredicate(parse.current.token)) .filter(alias -> !isIn(parse.args, alias)) .map(args -> parse.current.prefix + args) .collect(GuavaCollectors.toImmutableList()); return FGManager.getInstance().getAllRegions(world).stream() - .filter(region -> region.isEnabled() != this.enableState && !(region instanceof GlobalWorldRegion)) + .filter(region -> region.isEnabled() != this.enableState && !(region instanceof IGlobal)) .map(IFGObject::getName) .filter(new StartsWithPredicate(parse.current.token)) .filter(alias -> !isIn(parse.args, alias)) @@ -281,7 +252,7 @@ else if (parse.current.index > 0) { .collect(GuavaCollectors.toImmutableList()); } else if (isIn(HANDLERS_ALIASES, parse.args[0])) { return FGManager.getInstance().getHandlers().stream() - .filter(handler -> handler.isEnabled() != this.enableState && !(handler instanceof GlobalHandler)) + .filter(handler -> handler.isEnabled() != this.enableState && !(handler instanceof IGlobal)) .map(IFGObject::getName) .filter(new StartsWithPredicate(parse.current.token)) .filter(alias -> !isIn(parse.args, alias)) diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/GroupHandler.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/GroupHandler.java index 458023f..32b2d4c 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/GroupHandler.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/GroupHandler.java @@ -777,17 +777,17 @@ public EventResult handle(@Nullable User user, FlagBitSet flags, ExtraContext ex @Override public String getShortTypeName() { - return "Basic"; + return "Group"; } @Override public String getLongTypeName() { - return "Basic"; + return "Group"; } @Override public String getUniqueTypeString() { - return "basic"; + return "group"; } @Override @@ -1207,7 +1207,7 @@ public String getPermission() { public static class Factory implements IHandlerFactory { - private static final String[] ALIASES = {"basic", "base"}; + private static final String[] ALIASES = {"group", "permgroup"}; @Override public IHandler create(String name, int priority, String arguments, CommandSource source) throws CommandException { @@ -1271,7 +1271,7 @@ public String[] getAliases() { @Override public String getType() { - return "basic"; + return "group"; } @Override diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/PlayerMoveListenerNew.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/PlayerMoveListenerNew.java new file mode 100644 index 0000000..e1bf061 --- /dev/null +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/PlayerMoveListenerNew.java @@ -0,0 +1,358 @@ +/* + * This file is part of FoxGuard, licensed under the MIT License (MIT). + * + * Copyright (c) gravityfox - https://gravityfox.net/ + * Copyright (c) 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.foxdenstudio.sponge.foxguard.plugin.listener; + +import com.flowpowered.math.GenericMath; +import com.flowpowered.math.vector.Vector3d; +import com.flowpowered.math.vector.Vector3i; +import com.google.common.collect.ImmutableList; +import net.foxdenstudio.sponge.foxcore.plugin.command.CommandHUD; +import net.foxdenstudio.sponge.foxcore.plugin.util.CacheMap; +import net.foxdenstudio.sponge.foxguard.plugin.FGManager; +import net.foxdenstudio.sponge.foxguard.plugin.event.FGUpdateEvent; +import net.foxdenstudio.sponge.foxguard.plugin.flag.FlagBitSet; +import net.foxdenstudio.sponge.foxguard.plugin.handler.IHandler; +import net.foxdenstudio.sponge.foxguard.plugin.object.IFGObject; +import net.foxdenstudio.sponge.foxguard.plugin.region.IRegion; +import net.foxdenstudio.sponge.foxguard.plugin.util.ExtraContext; +import net.foxdenstudio.sponge.foxguard.plugin.util.FGUtil; +import org.spongepowered.api.entity.Entity; +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.event.EventListener; +import org.spongepowered.api.event.Listener; +import org.spongepowered.api.event.entity.DisplaceEntityEvent; +import org.spongepowered.api.event.network.ClientConnectionEvent; +import org.spongepowered.api.scoreboard.Score; +import org.spongepowered.api.scoreboard.Scoreboard; +import org.spongepowered.api.scoreboard.critieria.Criteria; +import org.spongepowered.api.scoreboard.displayslot.DisplaySlots; +import org.spongepowered.api.scoreboard.objective.Objective; +import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.chat.ChatTypes; +import org.spongepowered.api.text.format.TextColors; +import org.spongepowered.api.util.Tristate; +import org.spongepowered.api.world.World; + +import java.util.*; + +import static net.foxdenstudio.sponge.foxguard.plugin.flag.Flags.*; + +/** + * Created by Fox on 1/4/2016. + * Project: SpongeForge + */ +public class PlayerMoveListenerNew implements EventListener { + + private static final FlagBitSet ENTER_FLAG_SET = new FlagBitSet(ROOT, DEBUFF, PASS, ENTER); + private static final FlagBitSet EXIT_FLAG_SET = new FlagBitSet(ROOT, DEBUFF, PASS, EXIT); + private static final LastWrapper EMPTY_LAST_WRAPPER = new LastWrapper(null, null); + + private static PlayerMoveListenerNew instance; + + public final boolean full; + + private final Map last = new CacheMap<>((key, map) -> EMPTY_LAST_WRAPPER); + private final Map scoreboardMap = new CacheMap<>((k, m) -> { + if (k instanceof Player) { + Scoreboard s = Scoreboard.builder().build(); + Objective o = Objective.builder().criterion(Criteria.DUMMY).name("foxguardhere").displayName(Text.EMPTY).build(); + s.addObjective(o); + s.updateDisplaySlot(o, DisplaySlots.SIDEBAR); + m.put((Player) k, s); + return s; + } else return null; + }); + private final Map hudConfigMap = new CacheMap<>((k, m) -> new HUDConfig()); + + public PlayerMoveListenerNew(boolean full) { + this.full = full; + if (instance == null) instance = this; + } + + @Override + public void handle(DisplaceEntityEvent event) throws Exception { + + if (event.isCancelled() || event.getTargetEntity().getVehicle().isPresent()) return; + + World world = event.getTargetEntity().getWorld(); + //boolean cancel = false; + + getPassengerStack(event.getTargetEntity()).stream() + .filter(entity -> entity instanceof Player) + .map(entity -> (Player) entity) + .forEach(player -> { + final boolean hud = CommandHUD.instance().getIsHUDEnabled().get(player) && player.getScoreboard() == scoreboardMap.get(player); + final HUDConfig config = this.hudConfigMap.get(player); + final boolean regionHUD = hud && config.regions; + + List fromList = last.get(player).list, toList = new ArrayList<>(); + List regionList = new ArrayList<>(); + Vector3d to = event.getToTransform().getPosition().add(0, 0.1, 0); + if (fromList == null) { + fromList = new ArrayList<>(); + final List temp = fromList; + Vector3d from = event.getFromTransform().getPosition().add(0, 0.1, 0); + FGManager.getInstance().getAllRegions(world, new Vector3i( + GenericMath.floor(from.getX() / 16.0), + GenericMath.floor(from.getY() / 16.0), + GenericMath.floor(from.getZ() / 16.0))).stream() + .filter(region -> region.contains(from, world)) + .forEach(region -> region.getHandlers().stream() + .filter(IFGObject::isEnabled) + .filter(handler -> !temp.contains(handler)) + .forEach(temp::add)); + } else { + fromList = new ArrayList<>(fromList); + } + FGManager.getInstance().getAllRegions(world, new Vector3i( + GenericMath.floor(to.getX() / 16.0), + GenericMath.floor(to.getY() / 16.0), + GenericMath.floor(to.getZ() / 16.0))).stream() + .filter(region -> region.contains(to, world)) + .forEach(region -> { + if (regionHUD) regionList.add(region); + region.getHandlers().stream() + .filter(IFGObject::isEnabled) + .filter(handler -> !toList.contains(handler)) + .forEach(toList::add); + }); + + final List toComplete = new ArrayList<>(toList); + + final List temp = fromList; + ImmutableList.copyOf(fromList).stream() + .filter(toList::contains) + .forEach(handler -> { + temp.remove(handler); + toList.remove(handler); + }); + List finalList = new ArrayList<>(); + fromList.stream() + .map(handler -> new HandlerWrapper(handler, Type.FROM)) + .forEach(finalList::add); + toList.stream() + .map(handler -> new HandlerWrapper(handler, Type.TO)) + .forEach(finalList::add); + + if (finalList.size() == 0) { + this.last.put(player, new LastWrapper(toComplete, event.getToTransform().getPosition())); + return; + } + + if (full) { + Collections.sort(finalList); + int currPriority = finalList.get(0).handler.getPriority(); + Tristate flagState = Tristate.UNDEFINED; + for (HandlerWrapper wrap : finalList) { + if (wrap.handler.getPriority() < currPriority && flagState != Tristate.UNDEFINED) { + break; + } + if (wrap.type == Type.FROM) { + flagState = flagState.and(wrap.handler.handle(player, (FlagBitSet) EXIT_FLAG_SET.clone(), ExtraContext.of(event)).getState()); + } else { + flagState = flagState.and(wrap.handler.handle(player, (FlagBitSet) ENTER_FLAG_SET.clone(), ExtraContext.of(event)).getState()); + } + currPriority = wrap.handler.getPriority(); + } + + if (flagState == Tristate.FALSE) { + player.sendMessage(ChatTypes.ACTION_BAR, Text.of("You don't have permission to pass!")); + Vector3d position = this.last.get(player).position; + if (position == null) position = event.getFromTransform().getPosition(); + event.setToTransform(event.getToTransform().setPosition(position)); + } else { + this.last.put(player, new LastWrapper(toComplete, event.getToTransform().getPosition())); + //makes sure that handlers are unable to cancel the event directly. + event.setCancelled(false); + if (hud) { + renderHUD(player, regionList, toComplete, config); + player.setScoreboard(this.scoreboardMap.get(player)); + } + } + } else if (hud) { + renderHUD(player, regionList, toComplete, config); + player.setScoreboard(this.scoreboardMap.get(player)); + } + }); + } + + public void renderHUD(Player player, List regions, List handlers, HUDConfig config) { + this.scoreboardMap.remove(player); + Scoreboard scoreboard = this.scoreboardMap.get(player); + Objective objective = scoreboard.getObjective("foxguardhere").get(); + if (config.regions) { + Collections.sort(regions, (o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName())); + if (config.handlers) { + if (config.priority) { + Collections.sort(handlers, (o1, o2) -> o2.getPriority() - o1.getPriority()); + } else { + Collections.sort(handlers, (o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName())); + } + objective.setDisplayName(Text.of(TextColors.GOLD, " Regions and Handlers Here ")); + final int total = regions.size() + handlers.size(); + final int regionCount = (int) Math.round(13.0 * regions.size() / total); + final int handlerCount = (int) Math.round(13.0 * handlers.size() / total); + int slot = Math.min(15, total + 2); + Score regionsScore = objective.getOrCreateScore(Text.of(TextColors.GREEN, "Regions (" + player.getWorld().getName() + ") ", + TextColors.YELLOW, "(" + regions.size() + ")")); + regionsScore.setScore(slot--); + for (int i = 0; i < regionCount && i < regions.size(); i++) { + IRegion region = regions.get(i); + Score score = objective.getOrCreateScore(Text.of(FGUtil.getColorForObject(region), + " " + FGUtil.getRegionName(region, false))); + score.setScore(slot--); + } + Score handlersScore = objective.getOrCreateScore(Text.of(TextColors.GREEN, "Handlers " + (config.priority ? "by Priority " : ""), + TextColors.YELLOW, "(" + handlers.size() + ")")); + handlersScore.setScore(slot--); + for (int i = 0; i < handlerCount && i < handlers.size(); i++) { + IHandler handler = handlers.get(i); + Score score = objective.getOrCreateScore(Text.of(FGUtil.getColorForObject(handler), + " " + handler.getShortTypeName() + " : " + handler.getName())); + score.setScore(slot--); + } + + } else { + int slot = regions.size(); + objective.setDisplayName(Text.of(TextColors.GOLD, " Regions Here (" + player.getWorld().getName() + ") ")); + for (IRegion region : regions) { + Score score = objective.getOrCreateScore(Text.of(FGUtil.getColorForObject(region), + " " + FGUtil.getRegionName(region, false))); + score.setScore(slot--); + if (slot <= 0) break; + } + } + } else if (config.handlers) { + if (config.priority) { + objective.setDisplayName(Text.of(TextColors.GOLD, " Handlers Here by Priority ")); + for (IHandler handler : handlers) { + Score score = objective.getOrCreateScore(Text.of(FGUtil.getColorForObject(handler), + " " + handler.getShortTypeName() + " : " + handler.getName())); + score.setScore(handler.getPriority()); + } + } else { + int slot = handlers.size(); + objective.setDisplayName(Text.of(TextColors.GOLD, " Handlers Here ")); + Collections.sort(handlers, (o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName())); + for (IHandler handler : handlers) { + Score score = objective.getOrCreateScore(Text.of(FGUtil.getColorForObject(handler), + " " + handler.getShortTypeName() + " : " + handler.getName())); + score.setScore(slot--); + if (slot <= 0) break; + } + } + } + } + + public Map getHudConfigMap() { + return hudConfigMap; + } + + public void showScoreboard(Player player) { + player.setScoreboard(this.scoreboardMap.get(player)); + } + + public static PlayerMoveListenerNew getInstance() { + return instance; + } + + private static Set getPassengerStack(Entity e) { + Set set = new HashSet<>(); + set.add(e); + Optional po = e.getPassenger(); + if (po.isPresent()) + set.addAll(getPassengerStack(po.get())); + return set; + } + + private enum Type { + FROM, TO + } + + private class HandlerWrapper implements Comparable { + public IHandler handler; + public Type type; + + public HandlerWrapper(IHandler handler, Type type) { + this.handler = handler; + this.type = type; + } + + @Override + public int compareTo(HandlerWrapper w) { + int val = handler.compareTo(w.handler); + return val != 0 ? val : type.compareTo(w.type); + } + + @Override + public String toString() { + return this.type + ":" + this.handler; + } + } + + private static class LastWrapper { + public List list; + public Vector3d position; + + public LastWrapper(List list, Vector3d position) { + this.list = list; + this.position = position; + } + } + + public static class HUDConfig { + public boolean regions; + public boolean handlers; + public boolean priority; + + public HUDConfig() { + this(true, true, false); + } + + public HUDConfig(boolean regions, boolean handlers, boolean priority) { + this.regions = regions; + this.handlers = handlers; + this.priority = priority; + } + } + + public class Listeners { + @Listener + public void onJoin(ClientConnectionEvent.Join event) { + last.put(event.getTargetEntity(), new LastWrapper(null, event.getTargetEntity().getTransform().getPosition())); + } + + /*@Listener + public void onPlayerChangeWorld() { + + }*/ + + @Listener + public void onChange(FGUpdateEvent event) { + last.clear(); + } + } +} diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/GlobalRegion.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/GlobalRegion.java index 927d839..f2e7f2a 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/GlobalRegion.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/GlobalRegion.java @@ -29,6 +29,7 @@ import com.flowpowered.math.vector.Vector3i; import com.google.common.collect.ImmutableList; import net.foxdenstudio.sponge.foxcore.plugin.command.util.ProcessResult; +import net.foxdenstudio.sponge.foxguard.plugin.object.IGlobal; import org.spongepowered.api.command.CommandException; import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.text.Text; @@ -40,7 +41,7 @@ /** * Created by Fox on 4/2/2016. */ -public class GlobalRegion extends RegionBase { +public class GlobalRegion extends RegionBase implements IGlobal { public static final String NAME = "_sglobal"; public GlobalRegion() { diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/RegionBase.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/RegionBase.java index 96e951d..6fcc4de 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/RegionBase.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/RegionBase.java @@ -30,6 +30,7 @@ import net.foxdenstudio.sponge.foxguard.plugin.handler.IHandler; import net.foxdenstudio.sponge.foxguard.plugin.object.FGObjectBase; import net.foxdenstudio.sponge.foxguard.plugin.util.FGUtil; +import net.foxdenstudio.sponge.foxguard.plugin.util.RegionCache; import java.util.HashSet; import java.util.List; @@ -44,6 +45,12 @@ protected RegionBase(String name, boolean isEnabled) { this.handlers = new HashSet<>(); } + @Override + public void setIsEnabled(boolean state) { + super.setIsEnabled(state); + FGManager.getInstance().markDirty(this, RegionCache.DirtyType.MODIFIED); + } + @Override public List getHandlers() { return ImmutableList.copyOf(this.handlers); diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/CuboidRegion.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/CuboidRegion.java index 3a22458..56a8d1b 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/CuboidRegion.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/CuboidRegion.java @@ -196,6 +196,7 @@ public BoundingBox3 getBoundingBox() { public void setBoundingBox(BoundingBox3 boundingBox) { this.boundingBox = boundingBox; + markDirty(); } public static class Factory implements IWorldRegionFactory { diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/ElevationRegion.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/ElevationRegion.java index 6a70293..956102e 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/ElevationRegion.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/ElevationRegion.java @@ -156,6 +156,7 @@ public int getUpperBound() { public void setUpperBound(int upperBound) { this.upperBound = upperBound; + markDirty(); } public int getLowerBound() { @@ -164,6 +165,7 @@ public int getLowerBound() { public void setLowerBound(int lowerBound) { this.lowerBound = lowerBound; + markDirty(); } @Override diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/RectangularRegion.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/RectangularRegion.java index 3928ba0..0d3412d 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/RectangularRegion.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/RectangularRegion.java @@ -180,6 +180,7 @@ public BoundingBox2 getBoundingBox() { public void setBoundingBox(BoundingBox2 boundingBox) { this.boundingBox = boundingBox; + markDirty(); } public static class Factory implements IWorldRegionFactory { diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/util/RegionCache.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/util/RegionCache.java index 9814eb0..71d295f 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/util/RegionCache.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/util/RegionCache.java @@ -82,6 +82,10 @@ public void markDirty(IRegion region, DirtyType type) { } } + public void clearCaches() { + this.chunks.values().forEach(Map::clear); + } + public ChunkData getData(World world, Vector3i chunk) { return this.chunks.get(world).get(chunk); }