Skip to content

Commit

Permalink
refactor: move a lot of methods to visibility modifier (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
Snabeldier authored Feb 5, 2025
1 parent bb07555 commit 1b8eb9a
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/main/java/minevalley/core/api/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import minevalley.core.api.regions.structures.Street;
import minevalley.core.api.regions.utils.Area;
import minevalley.core.api.regions.utils.Boundary;
import minevalley.core.api.regions.utils.FakeBlock;
import minevalley.core.api.utils.FakeBlock;
import minevalley.core.api.server.Server;
import minevalley.core.api.team.Team;
import minevalley.core.api.timing.Reminder;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/minevalley/core/api/CoreServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import minevalley.core.api.regions.structures.Street;
import minevalley.core.api.regions.utils.Area;
import minevalley.core.api.regions.utils.Boundary;
import minevalley.core.api.regions.utils.FakeBlock;
import minevalley.core.api.utils.FakeBlock;
import minevalley.core.api.server.Server;
import minevalley.core.api.team.Team;
import minevalley.core.api.timing.Reminder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,74 @@
package minevalley.core.api.modifiers;

import minevalley.core.api.users.OnlineUser;
import org.jetbrains.annotations.Contract;

import javax.annotation.Nonnull;
import java.util.function.Function;

@SuppressWarnings("unused")
public interface VisibilityModifier {

/**
* Defines who can see this object
* Shows this object to everyone who can see it.
* <p>
* <b>Note:</b>
* <ul>
* <li>Objects are by default visible when created (no need to call this method, if the object was not hidden actively before)</li>
* <li>Hidden objects can be modified (lines of holograms, name of npc, ...). Changes will be visible after calling show()</li>
* </ul>
*
* @throws IllegalStateException if the object is already visible
* @see #isVisible()
*/
void show() throws IllegalStateException;

/**
* Hide this object from everyone who can see it.
* <p>
* <b>Note:</b>
* <ul>
* <li>Hiding objects removes them completely and for everyone from the map</li>
* <li>Hiding is not permanent: Objects can be made visible again by calling {@link #show()}</li>
* <li>Hidden objects can be modified (lines of holograms, name of npc, ...). Changes will be visible after calling show()</li>
* </ul>
*
* @throws IllegalStateException if the object is already hidden
* @see #isVisible()
*/
void hide() throws IllegalStateException;

/**
* Returns whether this object is visible
*
* @return true if this object is visible, false otherwise
*/
@Contract(pure = true)
boolean isVisible();

/**
* Defines who can see this object.
* <p>
* <b>Note:</b>
* <ul>
* <li>Setting the visibility replaces the previous visibility function</li>
* <li>Objects are only visible if {@link #isVisible()} is true (which is the default state)</li>
* </ul>
* <br>
* <b>Default:</b> Everyone can see this object
*
* @param function defines whether a specific user can see this object
* @param visibility defines whether a specific user can see this object
* @throws IllegalArgumentException if visibility is null
* @see #isVisible()
*/
void setVisibility(@Nonnull Function<OnlineUser, Boolean> visibility) throws IllegalArgumentException;

/**
* Sets the visibility of this object to everyone.
*/
void setVisibility(Function<OnlineUser, Boolean> function);
default void setVisibleToEveryone() {
setVisibility(user -> true);
}

/**
* Updates the visibility of this object
Expand All @@ -29,6 +83,7 @@ public interface VisibilityModifier {
* <b>Note:</b> This simply calls the function defined in {@link #setVisibility(Function)} for this user
*
* @param user to update the visibility for
* @throws IllegalArgumentException if user is null
*/
void updateVisibility(OnlineUser user);
void updateVisibility(@Nonnull OnlineUser user) throws IllegalArgumentException;
}
2 changes: 0 additions & 2 deletions src/main/java/minevalley/core/api/npc/NPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ default void zoomIn(OnlineUser user) {

Location getLocation();

void remove();

boolean isNameTagHidden();

void hideNameTag(boolean hidden);
Expand Down

This file was deleted.

11 changes: 3 additions & 8 deletions src/main/java/minevalley/core/api/utils/CarBarrier.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package minevalley.core.api.utils;

import minevalley.core.api.modifiers.VisibilityModifier;

@SuppressWarnings("unused")
public interface CarBarrier {
public interface CarBarrier extends VisibilityModifier {

/**
* Opens the barrier.
Expand All @@ -16,11 +18,4 @@ public interface CarBarrier {
* @throws IllegalStateException if the barrier is already closed or removed.
*/
void close() throws IllegalStateException;

/**
* Removes the barrier.
*
* @throws IllegalStateException if the barrier is already removed.
*/
void remove() throws IllegalStateException;
}
27 changes: 27 additions & 0 deletions src/main/java/minevalley/core/api/utils/FakeBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package minevalley.core.api.utils;

import minevalley.core.api.modifiers.VisibilityModifier;
import org.bukkit.Material;
import org.bukkit.block.Block;

import javax.annotation.Nonnull;

@SuppressWarnings("unused")
public interface FakeBlock extends VisibilityModifier {

/**
* Get the block this fake block is placed on
*
* @return the block this fake block is placed on
*/
@Nonnull
Block getBlock();

/**
* Get the material of this fake block
*
* @return the material of this fake block
*/
@Nonnull
Material getMaterial();
}
35 changes: 33 additions & 2 deletions src/main/java/minevalley/core/api/utils/Hologram.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,52 @@
@SuppressWarnings("unused")
public interface Hologram extends LocationModifier, VisibilityModifier {

/**
* Returns the lines of the hologram.
*
* @return the lines of the hologram.
*/
String[] getLines();

/**
* Returns the line at the specified index.
*
* @param line the index of the line.
* @return the line at the specified index.
*/
default String getLine(int line) {
return getLines()[line];
}

/**
* Adds a line to the hologram.
*
* @param text the text of the line.
*/
default void addLine(String text) {
addLineAfter(getLines().length - 1, text);
}

/**
* Adds a line to the hologram at the specified index.
*
* @param line the index of the line.
* @param text the text of the line.
*/
void addLineAfter(int line, String text);

/**
* Changes the line at the specified index.
*
* @param line the index of the line.
* @param text the new text of the line.
*/
void changeLine(int line, String text);

/**
* Removes the line at the specified index.
*
* @param line the index of the line.
*/
void removeLine(int line);

void delete();
}

0 comments on commit 1b8eb9a

Please sign in to comment.