Skip to content

Commit

Permalink
* Finished customizable flag system
Browse files Browse the repository at this point in the history
* Cuboid interface ready for use
* Interaction events to go with new Region.java
* Have fun :)
  • Loading branch information
Hempfest committed May 13, 2021
1 parent 6284ddb commit 283862e
Show file tree
Hide file tree
Showing 11 changed files with 424 additions and 148 deletions.
106 changes: 66 additions & 40 deletions src/main/java/com/github/sanctum/labyrinth/Labyrinth.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.github.sanctum.labyrinth.data.DefaultProvision;
import com.github.sanctum.labyrinth.data.EconomyProvision;
import com.github.sanctum.labyrinth.data.Region;
import com.github.sanctum.labyrinth.data.RegionFlag;
import com.github.sanctum.labyrinth.data.RegionServicesManager;
import com.github.sanctum.labyrinth.data.VaultHook;
import com.github.sanctum.labyrinth.data.container.DataContainer;
Expand All @@ -22,6 +23,7 @@
import com.github.sanctum.labyrinth.library.StringUtils;
import com.github.sanctum.labyrinth.task.Schedule;
import com.github.sanctum.labyrinth.task.Synchronous;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -121,56 +123,80 @@ public void onEnable() {

})).repeatReal(0, 5);


if (Region.DATA.exists()) {
run(() -> {
RegionServicesManager manager = RegionServicesManager.getInstance();
manager.load(manager.getFlagManager().getDefault(Cuboid.FlagManager.FlagType.BREAK));
manager.load(manager.getFlagManager().getDefault(Cuboid.FlagManager.FlagType.BUILD));
manager.load(manager.getFlagManager().getDefault(Cuboid.FlagManager.FlagType.PVP));
if (Region.DATA.getConfig().isConfigurationSection("Markers.spawn")) {
for (String id : Region.DATA.getConfig().getConfigurationSection("Markers.spawn").getKeys(false)) {
Location o = Region.DATA.getConfig().getLocation("Markers.spawn." + id + ".pos1");
if (Region.DATA.exists()) {
if (Region.DATA.getConfig().isConfigurationSection("Markers.spawn")) {
for (String id : Region.DATA.getConfig().getConfigurationSection("Markers.spawn").getKeys(false)) {
Location o = Region.DATA.getConfig().getLocation("Markers.spawn." + id + ".pos1");

if (o.getWorld() == null) {
throw new IllegalStateException("World is null??");
}
if (o.getWorld() == null) {
throw new IllegalStateException("World is null??");
}

Location t = Region.DATA.getConfig().getLocation("Markers.spawn." + id + ".pos2");
Location s = Region.DATA.getConfig().getLocation("Markers.spawn." + id + ".start");
HUID d = HUID.fromString(id);
UUID owner = UUID.fromString(Region.DATA.getConfig().getString("Markers.spawn." + id + ".owner"));
List<UUID> members = Region.DATA.getConfig().getStringList("Markers.spawn." + id + ".members").stream().map(UUID::fromString).collect(Collectors.toList());
//List<Region.Flag> flags = Region.DATA.getConfig().getStringList("Markers.spawn." + id + ".flags").stream().map(Region.Flag::valueOf).collect(Collectors.toList());
Region.Spawning spawn = new Region.Spawning(o, t, d);
spawn.setLocation(s);
spawn.setOwner(owner);
run(() -> spawn.setPlugin(getServer().getPluginManager().getPlugin(Region.DATA.getConfig().getString("Markers.spawn." + id + ".plugin")))).wait(2);
spawn.addMember(members.stream().map(Bukkit::getOfflinePlayer).toArray(OfflinePlayer[]::new));
//spawn.addFlag(flags.toArray(new Region.Flag[0]));
if (!spawn.load()) {
getLogger().warning("- A region under the name '" + spawn.getId() + "' has failed to load properly.");
Location t = Region.DATA.getConfig().getLocation("Markers.spawn." + id + ".pos2");
Location s = Region.DATA.getConfig().getLocation("Markers.spawn." + id + ".start");
HUID d = HUID.fromString(id);
UUID owner = UUID.fromString(Region.DATA.getConfig().getString("Markers.spawn." + id + ".owner"));
List<UUID> members = Region.DATA.getConfig().getStringList("Markers.spawn." + id + ".members").stream().map(UUID::fromString).collect(Collectors.toList());
List<Region.Flag> flags = new ArrayList<>();
if (Region.DATA.getConfig().isConfigurationSection("Markers.spawn." + id + ".flags")) {
for (String flag : Region.DATA.getConfig().getConfigurationSection("Markers.spawn." + id + ".flags").getKeys(false)) {
Cuboid.Flag f = manager.getFlagManager().getFlag(flag).orElse(null);
if (f != null) {
RegionFlag copy = new RegionFlag(f);
copy.setMessage(Region.DATA.getConfig().getString("Markers.spawn." + id + ".flags." + flag + ".message"));
copy.setAllowed(Region.DATA.getConfig().getBoolean("Markers.spawn." + id + ".flags." + flag + ".allowed"));
flags.add(copy);
}
}
}
Region.Spawning spawn = new Region.Spawning(o, t, d);
spawn.setLocation(s);
spawn.setOwner(owner);
run(() -> spawn.setPlugin(getServer().getPluginManager().getPlugin(Region.DATA.getConfig().getString("Markers.spawn." + id + ".plugin")))).run();
spawn.addMember(members.stream().map(Bukkit::getOfflinePlayer).toArray(OfflinePlayer[]::new));
spawn.addFlag(flags.toArray(new Region.Flag[0]));
if (!spawn.load()) {
getLogger().warning("- A region under the name '" + spawn.getId() + "' has failed to load properly.");
}
}
}
}
if (Region.DATA.getConfig().isConfigurationSection("Markers.region")) {
for (String id : Region.DATA.getConfig().getConfigurationSection("Markers.region").getKeys(false)) {
Location o = Region.DATA.getConfig().getLocation("Markers.region." + id + ".pos1");
Location t = Region.DATA.getConfig().getLocation("Markers.region." + id + ".pos2");
HUID d = HUID.fromString(id);
UUID owner = UUID.fromString(Region.DATA.getConfig().getString("Markers.region." + id + ".owner"));
List<UUID> members = Region.DATA.getConfig().getStringList("Markers.region." + id + ".members").stream().map(UUID::fromString).collect(Collectors.toList());
//List<Region.Flag> flags = Region.DATA.getConfig().getStringList("Markers.region." + id + ".flags").stream().map(Region.Flag::valueOf).collect(Collectors.toList());
Region.Loading region = new Region.Loading(o, t, d);
region.setOwner(owner);
run(() -> region.setPlugin(getServer().getPluginManager().getPlugin(Region.DATA.getConfig().getString("Markers.region." + id + ".plugin")))).wait(2);
region.addMember(members.stream().map(Bukkit::getOfflinePlayer).toArray(OfflinePlayer[]::new));
//region.addFlag(flags.toArray(new Region.Flag[0]));
if (!region.load()) {
getLogger().warning("- A region under the name '" + region.getId() + "' has failed to load properly.");
if (Region.DATA.getConfig().isConfigurationSection("Markers.region")) {
for (String id : Region.DATA.getConfig().getConfigurationSection("Markers.region").getKeys(false)) {
Location o = Region.DATA.getConfig().getLocation("Markers.region." + id + ".pos1");
Location t = Region.DATA.getConfig().getLocation("Markers.region." + id + ".pos2");
HUID d = HUID.fromString(id);
UUID owner = UUID.fromString(Region.DATA.getConfig().getString("Markers.region." + id + ".owner"));
List<UUID> members = Region.DATA.getConfig().getStringList("Markers.region." + id + ".members").stream().map(UUID::fromString).collect(Collectors.toList());
List<Region.Flag> flags = new ArrayList<>();
if (Region.DATA.getConfig().isConfigurationSection("Markers.region." + id + ".flags")) {
for (String flag : Region.DATA.getConfig().getConfigurationSection("Markers.region." + id + ".flags").getKeys(false)) {
Cuboid.Flag f = manager.getFlagManager().getFlag(flag).orElse(null);
if (f != null) {
RegionFlag copy = new RegionFlag(f);
copy.setMessage(Region.DATA.getConfig().getString("Markers.region." + id + ".flags." + flag + ".message"));
copy.setAllowed(Region.DATA.getConfig().getBoolean("Markers.region." + id + ".flags." + flag + ".allowed"));
flags.add(copy);
}
}
}
Region.Loading region = new Region.Loading(o, t, d);
region.setOwner(owner);
run(() -> region.setPlugin(getServer().getPluginManager().getPlugin(Region.DATA.getConfig().getString("Markers.region." + id + ".plugin")))).run();
region.addMember(members.stream().map(Bukkit::getOfflinePlayer).toArray(OfflinePlayer[]::new));
region.addFlag(flags.toArray(new Region.Flag[0]));
if (!region.load()) {
getLogger().warning("- A region under the name '" + region.getId() + "' has failed to load properly.");
}
}
}
}
}
}).wait(2);

run(() -> {
for (Region.Loading load : Region.loading().list()) {
Region.Standard result = new Region.Standard(load);
Expand All @@ -188,7 +214,7 @@ public void onEnable() {
}
spawn.remove();
}
}).wait(3);
}).wait(5);
}

@Override
Expand Down
45 changes: 38 additions & 7 deletions src/main/java/com/github/sanctum/labyrinth/data/Region.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ protected Region(int xmin, int xmax, int ymin, int ymax, int zmin, int zmax, Wor
this.FLAGS = new LinkedList<>();
this.MEMBERS = new LinkedList<>();
this.plugin = JavaPlugin.getProvidingPlugin(getClass());
addFlag(RegionServicesManager.getInstance().getFlagManager().getDefault(FlagManager.FlagType.BREAK),
RegionServicesManager.getInstance().getFlagManager().getDefault(FlagManager.FlagType.BUILD),
RegionServicesManager.getInstance().getFlagManager().getDefault(FlagManager.FlagType.PVP));
}

protected void setPlugin(Plugin plugin) {
Expand Down Expand Up @@ -297,7 +294,7 @@ public boolean removeMember(OfflinePlayer p) {
}

public boolean removeFlag(Flag flag) {
return this.FLAGS.remove(flag);
return this.FLAGS.removeIf(f -> f.getId().equals(flag.getId()));
}

public boolean removeFlag(Flag... flag) {
Expand Down Expand Up @@ -350,7 +347,11 @@ public void save() throws IOException {
DATA.getConfig().set("Markers.region." + getId().toString() + ".pos2", this.getEndingPoint());
DATA.getConfig().set("Markers.region." + getId().toString() + ".owner", this.getOwner().getUniqueId().toString());
DATA.getConfig().set("Markers.region." + getId().toString() + ".members", MEMBERS.stream().map(UUID::toString).collect(Collectors.toList()));
//DATA.getConfig().set("Markers.region." + getId().toString() + ".flags", FLAGS.stream().map(Enum::name).collect(Collectors.toList()));
for (Flag f : getFlags()) {
DATA.getConfig().set("Markers.region." + getId().toString() + ".flags." + f.getId() + ".allowed", f.isAllowed());
DATA.getConfig().set("Markers.region." + getId().toString() + ".flags." + f.getId() + ".plugin", f.isValid() ? f.getHost().getName() : "NA");
DATA.getConfig().set("Markers.region." + getId().toString() + ".flags." + f.getId() + ".message", f.getMessage());
}
DATA.saveConfig();
} else
throw new IOException("One or more locations were found null during the saving process.");
Expand Down Expand Up @@ -422,6 +423,10 @@ public OfflinePlayer getPlayer() {
return p;
}

public Player getOnline() {
return getPlayer().getPlayer();
}

public Optional<Region> getRegion() {
if (getPlayer().isOnline()) {
return Region.match(getPlayer().getPlayer().getLocation());
Expand Down Expand Up @@ -510,7 +515,11 @@ public void save() throws IOException {
DATA.getConfig().set("Markers.spawn." + getId().toString() + ".start", location());
DATA.getConfig().set("Markers.spawn." + getId().toString() + ".owner", this.getOwner().getUniqueId().toString());
DATA.getConfig().set("Markers.spawn." + getId().toString() + ".members", getMembers().stream().map(OfflinePlayer::getUniqueId).map(UUID::toString).collect(Collectors.toList()));
//DATA.getConfig().set("Markers.spawn." + getId().toString() + ".flags", FLAGS.stream().map(Enum::name).collect(Collectors.toList()));
for (Flag f : getFlags()) {
DATA.getConfig().set("Markers.spawn." + getId().toString() + ".flags." + f.getId() + ".allowed", f.isAllowed());
DATA.getConfig().set("Markers.spawn." + getId().toString() + ".flags." + f.getId() + ".plugin", f.isValid() ? f.getHost().getName() : "NA");
DATA.getConfig().set("Markers.spawn." + getId().toString() + ".flags." + f.getId() + ".message", f.getMessage());
}
DATA.saveConfig();
} else
throw new IOException("One or more locations were found null during the saving process.");
Expand Down Expand Up @@ -550,14 +559,23 @@ public static class Standard extends Region {

public Standard(Region cuboid) {
super(cuboid);
if (cuboid.getFlags().isEmpty()) {
FLAGS.addAll(RegionServicesManager.getInstance().getFlagManager().getDefault());
}
}

public Standard(Location point1, Location point2) {
super(point1, point2);
if (getFlags().isEmpty()) {
FLAGS.addAll(RegionServicesManager.getInstance().getFlagManager().getDefault());
}
}

public Standard(Location point1, Location point2, HUID id) {
super(point1, point2, id);
if (getFlags().isEmpty()) {
FLAGS.addAll(RegionServicesManager.getInstance().getFlagManager().getDefault());
}
}

public Standard forPlugin(Plugin plugin) {
Expand All @@ -573,14 +591,23 @@ public static class Spawn extends Region {

public Spawn(Region.Spawning cuboid) {
super(cuboid);
if (cuboid.getFlags().isEmpty()) {
FLAGS.addAll(RegionServicesManager.getInstance().getFlagManager().getDefault());
}
}

public Spawn(Location point1, Location point2) {
super(point1, point2);
if (getFlags().isEmpty()) {
FLAGS.addAll(RegionServicesManager.getInstance().getFlagManager().getDefault());
}
}

public Spawn(Location point1, Location point2, HUID id) {
super(point1, point2, id);
if (getFlags().isEmpty()) {
FLAGS.addAll(RegionServicesManager.getInstance().getFlagManager().getDefault());
}
}

public Location location() {
Expand Down Expand Up @@ -624,7 +651,11 @@ public void save() throws IOException {
DATA.getConfig().set("Markers.spawn." + getId().toString() + ".start", location());
DATA.getConfig().set("Markers.spawn." + getId().toString() + ".owner", this.getOwner().getUniqueId().toString());
DATA.getConfig().set("Markers.spawn." + getId().toString() + ".members", getMembers().stream().map(OfflinePlayer::getUniqueId).map(UUID::toString).collect(Collectors.toList()));
//DATA.getConfig().set("Markers.spawn." + getId().toString() + ".flags", FLAGS.stream().map(Enum::name).collect(Collectors.toList()));
for (Flag f : getFlags()) {
DATA.getConfig().set("Markers.spawn." + getId().toString() + ".flags." + f.getId() + ".allowed", f.isAllowed());
DATA.getConfig().set("Markers.spawn." + getId().toString() + ".flags." + f.getId() + ".plugin", f.isValid() ? f.getHost().getName() : "NA");
DATA.getConfig().set("Markers.spawn." + getId().toString() + ".flags." + f.getId() + ".message", f.getMessage());
}
DATA.saveConfig();
} else
throw new IOException("One or more locations were found null during the saving process.");
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/com/github/sanctum/labyrinth/data/RegionFlag.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.github.sanctum.labyrinth.library.Cuboid;
import com.github.sanctum.labyrinth.task.Schedule;
import java.util.function.Consumer;
import org.bukkit.plugin.Plugin;

public class RegionFlag extends Cuboid.Flag {
Expand All @@ -27,13 +26,6 @@ public Builder label(String id) {
return this;
}

public Builder envelope(Consumer<RegionService> serviceConsumer) {
serviceConsumer.accept(new RegionService() {

});
return this;
}

public Builder envelope(RegionService service) {
this.service = service;
return this;
Expand All @@ -60,4 +52,9 @@ public RegionFlag(Plugin plugin, String id, String message) {
super(plugin, id, message);
}

public RegionFlag setMessage(String message) {
this.message = message;
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.github.sanctum.labyrinth.library.Message;
import com.github.sanctum.labyrinth.task.Schedule;
import com.github.sanctum.labyrinth.task.Synchronous;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player;
Expand All @@ -14,6 +15,8 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityTargetLivingEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
Expand All @@ -36,6 +39,28 @@ public void onRespawn(PlayerRespawnEvent event) {
}
}

@EventHandler(priority = EventPriority.HIGHEST)
public void onBuild(BlockPlaceEvent e) {
if (Region.match(e.getBlock().getLocation()).isPresent()) {
RegionBuildEvent event = new RegionBuildEvent(e.getPlayer(), Region.match(e.getBlock().getLocation()).get(), e.getBlock());
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
e.setCancelled(true);
}
}
}

@EventHandler(priority = EventPriority.HIGHEST)
public void onBuild(BlockBreakEvent e) {
if (Region.match(e.getBlock().getLocation()).isPresent()) {
RegionDestroyEvent event = new RegionDestroyEvent(e.getPlayer(), Region.match(e.getBlock().getLocation()).get(), e.getBlock());
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
e.setCancelled(true);
}
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onFirstJoin(PlayerJoinEvent event) {
Region.Resident r = Region.Resident.get(event.getPlayer());
Expand All @@ -50,10 +75,10 @@ public void onFirstJoin(PlayerJoinEvent event) {
} else {
r.setSpawnTagged(false);
}
spawnListener(event.getPlayer()).repeatReal(5, 18);
moveListener(event.getPlayer()).repeatReal(5, 18);
}

public Synchronous spawnListener(Player p) {
public Synchronous moveListener(Player p) {
Region.Resident r = Region.Resident.get(p);
return Schedule.sync(() -> {
if (!r.getRegion().isPresent()) {
Expand Down Expand Up @@ -86,7 +111,7 @@ public Synchronous spawnListener(Player p) {
}).debug().cancelAfter(p);
}

@EventHandler
@EventHandler(priority = EventPriority.HIGHEST)
public void onEntityTarget(EntityTargetLivingEntityEvent e) {
if (e.getTarget() instanceof Player) {
Player target = (Player) e.getTarget();
Expand Down Expand Up @@ -167,6 +192,14 @@ public void onPlayerHit(EntityDamageByEntityEvent event) {
if (r.getRegion().isPresent()) {
Region region = r.getRegion().get();

RegionPVPEvent e = new RegionPVPEvent(p, target, region);
Bukkit.getPluginManager().callEvent(e);

if (e.isCancelled()) {
event.setCancelled(true);
return;
}

if (region instanceof Region.Spawn) {

Region.Spawn spawn = (Region.Spawn) region;
Expand Down Expand Up @@ -203,6 +236,14 @@ public void onPlayerHit(EntityDamageByEntityEvent event) {
if (r.getRegion().isPresent()) {
Region region = r.getRegion().get();

RegionPVPEvent e = new RegionPVPEvent(p, target, region);
Bukkit.getPluginManager().callEvent(e);

if (e.isCancelled()) {
event.setCancelled(true);
return;
}

if (region instanceof Region.Spawn) {

Region.Resident o = Region.Resident.get(p);
Expand Down

0 comments on commit 283862e

Please sign in to comment.