Skip to content

Commit

Permalink
added default implementations to make implementations simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
NonSwag committed Apr 3, 2024
1 parent 19af86e commit 442cdf2
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 252 deletions.
2 changes: 1 addition & 1 deletion api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "net.thenextlvl.protect"
version = "2.0.2-pre2"
version = "2.0.2-pre4"

java {
withSourcesJar()
Expand Down
15 changes: 15 additions & 0 deletions api/src/main/java/net/thenextlvl/protect/area/Area.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import core.annotation.MethodsReturnNotNullByDefault;
import net.thenextlvl.protect.flag.FlagProvider;
import org.bukkit.World;
import org.jetbrains.annotations.NotNull;

/**
* The Area interface represents an area inside a world.
Expand Down Expand Up @@ -45,4 +46,18 @@ public interface Area extends Container, FlagProvider, Comparable<Area> {
* @return the world associated with this Area
*/
World getWorld();

/**
* {@inheritDoc}
* <p>
* Compares this Area object with the specified Area object for order.
* The comparison is based on the priority of the areas.
*
* @param area the Area object to be compared
* @return a negative or positive integer or zero
*/
@Override
default int compareTo(@NotNull Area area) {
return Integer.compare(getPriority(), area.getPriority());
}
}
35 changes: 27 additions & 8 deletions api/src/main/java/net/thenextlvl/protect/area/AreaProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;

import java.util.Comparator;
import java.util.Optional;
import java.util.stream.Stream;

Expand All @@ -33,31 +34,39 @@ public interface AreaProvider {
* @param world the world to retrieve areas from
* @return a stream of areas in the given world
*/
Stream<Area> getAreas(World world);
default Stream<Area> getAreas(World world) {
return getAreas().filter(area -> area.getWorld().equals(world));
}

/**
* Retrieves a stream of areas at the given location.
*
* @param location the location to retrieve areas from
* @return a stream of areas at the given location
*/
Stream<Area> getAreas(Location location);
default Stream<Area> getAreas(Location location) {
return getAreas(location.getWorld()).filter(area -> area.contains(location));
}

/**
* Retrieves a stream of areas that contain the given block.
*
* @param block the block to check for containment
* @return a stream of areas containing the given block
*/
Stream<Area> getAreas(Block block);
default Stream<Area> getAreas(Block block) {
return getAreas(block.getLocation());
}

/**
* Retrieves a stream of areas that contain the given entity.
*
* @param entity the entity to check for containment
* @return a stream of areas containing the given entity
*/
Stream<Area> getAreas(Entity entity);
default Stream<Area> getAreas(Entity entity) {
return getAreas(entity.getLocation());
}

/**
* Retrieves the global area of the given world.
Expand All @@ -73,29 +82,39 @@ public interface AreaProvider {
* @param location the location to retrieve the area from
* @return the area at the given location
*/
Area getArea(Location location);
default Area getArea(Location location) {
return getAreas(location)
.max(Comparator.comparingInt(Area::getPriority))
.orElseGet(() -> getArea(location.getWorld()));
}

/**
* Retrieves the area with the highest priority that contains the given block.
*
* @param block the block to retrieve the area from
* @return the area that contains the given block
*/
Area getArea(Block block);
default Area getArea(Block block) {
return getArea(block.getLocation());
}

/**
* Retrieves the area with the highest priority that contains the given entity.
*
* @param entity the entity to check for containment
* @return the area that contains the given entity
*/
Area getArea(Entity entity);
default Area getArea(Entity entity) {
return getArea(entity.getLocation());
}

/**
* Retrieves an optional Area with the given name.
*
* @param name the name of the Area to retrieve
* @return an Optional containing the Area if found, or an empty Optional otherwise
*/
Optional<Area> getArea(@NamePattern String name);
default Optional<Area> getArea(@NamePattern String name) {
return getAreas().filter(area -> area.getName().equals(name)).findAny();
}
}
8 changes: 6 additions & 2 deletions api/src/main/java/net/thenextlvl/protect/area/Container.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@ public interface Container {
* @param block the block to check
* @return true if the block is contained within the container, false otherwise
*/
boolean contains(Block block);
default boolean contains(Block block) {
return contains(block.getLocation());
}

/**
* Determines if the given entity is contained within the container.
*
* @param entity the entity to check
* @return true if the entity is contained within the container, false otherwise
*/
boolean contains(Entity entity);
default boolean contains(Entity entity) {
return contains(entity.getLocation());
}

/**
* Retrieves a list of entities that are contained within this container.
Expand Down
25 changes: 25 additions & 0 deletions api/src/main/java/net/thenextlvl/protect/area/GlobalArea.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
package net.thenextlvl.protect.area;

import core.annotation.MethodsReturnNotNullByDefault;
import core.annotation.ParametersAreNotNullByDefault;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

import java.util.List;

/**
* The GlobalArea interface represents the area of an entire world.
*/
@MethodsReturnNotNullByDefault
@ParametersAreNotNullByDefault
public interface GlobalArea extends Area {

@Override
default List<Entity> getEntities() {
return getWorld().getEntities();
}

@Override
default List<Player> getPlayers() {
return getWorld().getPlayers();
}

@Override
default boolean contains(Location location) {
return getWorld().equals(location.getWorld());
}
}
89 changes: 84 additions & 5 deletions api/src/main/java/net/thenextlvl/protect/area/RegionizedArea.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
package net.thenextlvl.protect.area;

import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
import com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat;
import com.sk89q.worldedit.function.operation.ForwardExtentCopy;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.session.ClipboardHolder;
import core.annotation.MethodsReturnNotNullByDefault;
import core.annotation.ParametersAreNotNullByDefault;
import core.annotation.TypesAreNotNullByDefault;
import net.thenextlvl.protect.event.AreaSchematicDeleteEvent;
import net.thenextlvl.protect.event.AreaSchematicLoadEvent;
import net.thenextlvl.protect.schematic.SchematicHolder;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
* The RegionizedArea interface represents an area that is bound to a region.
Expand All @@ -25,17 +44,77 @@ public interface RegionizedArea<T extends Region> extends Area, SchematicHolder
T getRegion();

/**
* Redefines the region associated with this area.
* Sets the region associated with this {@link RegionizedArea}.
*
* @param region the new region to redefine
* @return true if the redefinition is successful, false otherwise
* @param region the region to set
* @return true if the region was successfully set, false otherwise
*/
boolean redefine(T region);
boolean setRegion(T region);

/**
* Checks if the area is too big.
*
* @return true if the area is too big, false otherwise
*/
boolean isTooBig();
default boolean isTooBig() {
return getRegion().getVolume() >= 1_000_000_000;
}

@Override
default boolean deleteSchematic() {
return getSchematic().exists() && (new AreaSchematicDeleteEvent<>(this).callEvent() && getSchematic().delete());
}

@Override
@SuppressWarnings("ResultOfMethodCallIgnored")
default void saveSchematic() throws IOException, WorldEditException {
var file = getSchematic().getAbsoluteFile();
file.getParentFile().mkdirs();
file.createNewFile();
try (var editSession = WorldEdit.getInstance().newEditSession(new BukkitWorld(getWorld()));
var writer = BuiltInClipboardFormat.FAST.getWriter(new FileOutputStream(getSchematic()))) {
var clipboard = new BlockArrayClipboard(getRegion());
var extent = new ForwardExtentCopy(editSession, getRegion(), clipboard, getRegion().getMinimumPoint());
extent.setCopyingEntities(true);
extent.setCopyingBiomes(true);
Operations.complete(extent);
writer.write(clipboard);
}
}

@Override
default boolean loadSchematic() throws IOException, WorldEditException {
if (!getSchematic().isFile()) return false;
var event = new AreaSchematicLoadEvent<>(this);
if (!event.callEvent()) return false;
var world = new BukkitWorld(getWorld());
try (EditSession editSession = WorldEdit.getInstance().newEditSession(world)) {
var clipboard = BuiltInClipboardFormat.FAST.getReader(new FileInputStream(getSchematic())).read();
world.getEntities(getRegion()).forEach(com.sk89q.worldedit.entity.Entity::remove);
var operation = new ClipboardHolder(clipboard).createPaste(editSession).to(getRegion().getMinimumPoint()).
copyBiomes(true).copyEntities(true).ignoreAirBlocks(false).build();
Operations.complete(operation);
event.getSuccessListeners().forEach(consumer -> consumer.accept(this));
return true;
}
}

@Override
default boolean contains(Location location) {
return getRegion().contains(location.getBlockX(), location.getBlockY(), location.getBlockZ());
}

@Override
default List<Entity> getEntities() {
return getWorld().getEntities().stream()
.filter(this::contains)
.toList();
}

@Override
default List<Player> getPlayers() {
return getWorld().getPlayers().stream()
.filter(this::contains)
.toList();
}
}
48 changes: 29 additions & 19 deletions api/src/main/java/net/thenextlvl/protect/flag/FlagProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,54 @@
* The FlagProvider interface represents an object that can store and retrieve flags and their associated states.
*/
public interface FlagProvider {

/**
* Sets the state of a flag.
* Retrieves the map of all flags and their associated state.
*
* @param flag The flag to set the state of.
* @param state The state to be set for the flag.
* @param <T> The type of the flag state.
* @return a map of flags and their associated state
*/
<T> void setFlag(@NotNull Flag<T> flag, T state);
@NotNull Map<Flag<?>, @Nullable Object> getFlags();

/**
* Retrieves the state of a flag.
* Sets the map of all flags and their associated state.
*
* @param flag The flag to retrieve the state of.
* @param <T> The type of the flag state.
* @return The state of the flag.
* @param flags A map of flags and their associated states.
*/
<T> T getFlag(@NotNull Flag<T> flag);
void setFlags(@NotNull Map<Flag<?>, @Nullable Object> flags);

/**
* Retrieves the map of all flags and their associated state.
* Sets the state of a flag.
*
* @return a map of flags and their associated state
* @param flag The flag to set the state of.
* @param state The state to be set for the flag.
* @param <T> The type of the flag state.
*/
@NotNull Map<Flag<?>, @Nullable Object> getFlags();
default <T> void setFlag(@NotNull Flag<T> flag, T state) {
getFlags().put(flag, state);
}

/**
* Sets the map of all flags and their associated state.
* Retrieves the state of a flag.
*
* @param flags A map of flags and their associated states.
* @param flag The flag to retrieve the state of.
* @param <T> The type of the flag state.
* @return The state of the flag.
*/
void setFlags(@NotNull Map<Flag<?>, @Nullable Object> flags);
@SuppressWarnings("unchecked")
default <T> T getFlag(@NotNull Flag<T> flag) {
return (T) getFlags().getOrDefault(flag, flag.defaultValue());
}

/**
* Checks if the flag has a state defined.
*
* @param flag The flag to check the state of.
* @param <T> The type of the flag.
* @param <T> The type of the flag.
* @return true if a state is defined, true otherwise.
*/
<T> boolean hasFlag(@NotNull Flag<T> flag);
default <T> boolean hasFlag(@NotNull Flag<T> flag) {
return getFlags().containsKey(flag);
}

/**
* Removes a flag from the FlagProvider.
Expand All @@ -57,5 +65,7 @@ public interface FlagProvider {
* @param <T> The type of the flag value.
* @return true if the flag was successfully removed, false otherwise.
*/
<T> boolean removeFlag(@NotNull Flag<T> flag);
default <T> boolean removeFlag(@NotNull Flag<T> flag) {
return getFlags().remove(flag) != null;
}
}
Loading

0 comments on commit 442cdf2

Please sign in to comment.