Updates 💯

This commit is contained in:
Paul Reilly
2023-05-18 23:15:17 -05:00
parent 1ecff910ec
commit 47fcda6462
30 changed files with 546 additions and 66 deletions

View File

@ -41,4 +41,9 @@ public class CommonsBase extends JavaPlugin
{
return executor;
}
public EventBus getEventBus()
{
return eventBus;
}
}

View File

@ -0,0 +1,35 @@
package me.totalfreedom.data;
import me.totalfreedom.security.ban.Ban;
import me.totalfreedom.security.ban.BanID;
import me.totalfreedom.sql.SQL;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
public class BanRegistry
{
private final List<Ban> bansList = new ArrayList<>();
public boolean addBan(Ban ban) {
return bansList.add(ban);
}
public boolean removeBan(Ban ban) {
return bansList.remove(ban);
}
@Nullable
public Ban getBan(BanID banID)
{
for (Ban ban : bansList)
{
if (ban.getBanID().matches(banID))
{
return ban;
}
}
return null;
}
}

View File

@ -2,10 +2,25 @@ package me.totalfreedom.data;
import me.totalfreedom.config.Configuration;
import java.util.HashSet;
import java.util.Set;
import java.util.HashMap;
import java.util.Map;
public class ConfigRegistry
{
Set<Configuration> configurationSet = new HashSet<>();
private final Map<String, Configuration> configurationList = new HashMap<>();
public void register(String name, Configuration configuration)
{
configurationList.put(name, configuration);
}
public void unregister(String name)
{
configurationList.remove(name);
}
public Configuration getConfiguration(String name)
{
return configurationList.get(name);
}
}

View File

@ -1,6 +1,6 @@
package me.totalfreedom.data;
import me.totalfreedom.security.Group;
import me.totalfreedom.security.perm.Group;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import java.util.ArrayList;

View File

@ -24,6 +24,16 @@ public class EventBus extends Service
eventSet.add(event);
}
public <T extends FEvent> T getEvent(Class<T> eventClass)
{
FEvent e = eventSet.stream()
.filter(event -> event.getEventClass().equals(eventClass))
.findFirst()
.orElse(null);
return eventClass.cast(e);
}
public <T extends FEvent> EventSubscription<T> subscribe(Class<T> eventClass, Callback<T> callback)
{
Context<T> eventContext = () -> eventSet.stream()

View File

@ -1,36 +1,15 @@
package me.totalfreedom.event;
import me.totalfreedom.api.Context;
import java.util.function.Supplier;
public final class EventSubscription<T extends FEvent>
public record EventSubscription<T extends FEvent>(T event, Callback<T> callback)
{
private final T event;
private final Callback<T> callback;
public EventSubscription(T event, Callback<T> callback)
{
this.event = event;
this.callback = callback;
}
public T getEvent()
{
return event;
}
public boolean cancel()
{
return getEvent().cancel();
return event().cancel();
}
public boolean isCancelled()
{
return getEvent().isCancelled();
}
public Callback<T> getCallback() {
return callback;
return event().isCancelled();
}
}

View File

@ -1,17 +1,29 @@
package me.totalfreedom.event;
import me.totalfreedom.api.Context;
public abstract class FEvent
{
private boolean isCancelled;
private boolean triggered;
protected FEvent()
{
this.isCancelled = false;
}
public abstract void call(Callback<FEvent> callback);
public void ping()
{
this.triggered = true;
}
public void reset()
{
this.triggered = false;
}
boolean shouldCall()
{
return triggered;
}
public boolean cancel()
{

View File

@ -1,5 +1,7 @@
package me.totalfreedom.event;
import com.sun.source.tree.ContinueTree;
import java.util.ArrayList;
import java.util.List;
@ -20,6 +22,11 @@ class SubscriptionBox<T extends FEvent>
}
public void tick() {
subscriptions.forEach(s -> s.getCallback().call(s.getEvent()));
subscriptions.forEach(s -> {
if (!s.event().shouldCall()) return;
s.callback().call(s.event());
s.event().reset();
});
}
}

View File

@ -6,6 +6,7 @@ import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
@ -113,7 +114,7 @@ public class ContextProvider
return new Location(toWorld(split[0]), toDouble(split[1]), toDouble(split[2]), toDouble(split[3]));
}
private @Nullable Component toComponent(String string)
private @NotNull Component toComponent(String string)
{
return Component.text(string);
}

View File

@ -0,0 +1,78 @@
package me.totalfreedom.security.ban;
import org.jetbrains.annotations.Nullable;
import java.time.Instant;
import java.util.UUID;
/**
* Represents a physical ban entry. This is used to store information about a ban,
* such as the player who was banned, the reason for why they were banned, the individual who issued the ban,
* when the ban expires, and when the ban was created.
* <br>
* Ban information is stored in the Database with the {@link BanID} as the PRIMARY KEY.
*/
public interface Ban
{
/**
* Gets the ID of this ban. This is an object which represents a string prefixed with either a T or a P,
* and suffixed with a 6-10 digit numerical code. This is used to identify the ban in the database, and for
* easier ban referencing.
*
* @return The ID of this ban.
*/
BanID getBanID();
/**
* Gets the UUID of the player who was banned. This is formatted as a UUID
* which allows us to retrieve the particular player instance, if applicable, and also
* have a true identifier to check against user logins.
*
* @return The UUID of the player who was banned.
*/
UUID getOffenderID();
/**
* Gets the reason that the player was banned for. Typically, the default reason is "You are banned!".
* We've forced implementations to require a reason, as it's important to know why a player was banned.
*
* @return The reason that the player was banned for.
*/
String getReason();
/**
* Gets the username of the individual who issued the ban. This is not a reliable way to store data, but
* in our case, we should not need to interact with the ban issuer from the code. This is simply for
* reference purposes.
*
* @return The username of the individual who issued the ban.
*/
String getBanIssuer();
/**
* Gets the {@link Instant} which this ban was created.
*
* @return The ban's creation time.
*/
Instant getCreationTime();
/**
* Gets the {@link Instant} which this ban is due to expire, if applicable.
* This method is annotated as {@link Nullable}, as permanent bans do not have an expiry date.
*
* @return The ban's expiry time, or null if the ban is permanent.
*/
@Nullable
Instant getExpiry();
/**
* Checks if the ban has expired. This will return false if:
* <ul>
* <li>The {@link Instant} returned by {@link #getExpiry()} is null.</li>
* <li>The {@link Instant} returned by {@link #getExpiry()} is after the current time.</li>
* </ul>
*
* @return True if the ban has expired, false otherwise.
*/
boolean isExpired();
}

View File

@ -0,0 +1,52 @@
package me.totalfreedom.security.ban;
/**
* Represents an ID for a ban. These are formatted either as:
* <p>
* P-00129381
* <br>
* T-00128381
* <br>
* </p>
* Where P marks a ban as permanent, and T marks a ban as temporary.
*/
public interface BanID
{
/**
* This method returns the full Ban ID.
*
* @return The actual ID.
*/
String getID();
/**
* This method returns the ban type denominator character for the Ban ID.
* This would either be T or P, where T = temporary and P = permanent.
*
* @return The ban type denominator character for the Ban ID.
*/
char getIDPrefix();
/**
* Gets the numerical tag of this ban ID.
* This would be the numerical section of the full Ban ID.
*
* @return The numerical tag of this ban ID.
*/
int getNumericalTag();
/**
* Checks the prefix of the Ban ID to see whether if it is permanent.
*
* @return true if the Ban ID is prefixed with a P, false otherwise.
*/
boolean isPermanent();
default boolean matches(BanID other) {
if (other == null) {
return false;
}
return (getIDPrefix() == other.getIDPrefix())
&& (getNumericalTag() == other.getNumericalTag());
}
}

View File

@ -1,4 +1,4 @@
package me.totalfreedom.security;
package me.totalfreedom.security.perm;
import net.kyori.adventure.text.Component;

View File

@ -1,4 +1,4 @@
package me.totalfreedom.security;
package me.totalfreedom.security.perm;
import org.bukkit.permissions.Permission;

View File

@ -1,4 +1,4 @@
package me.totalfreedom.security;
package me.totalfreedom.security.perm;
public interface NodeBuilder
{

View File

@ -1,4 +1,4 @@
package me.totalfreedom.security;
package me.totalfreedom.security.perm;
public enum NodeType
{

View File

@ -1,6 +1,5 @@
package me.totalfreedom.security;
package me.totalfreedom.security.perm;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permissible;
import java.util.Set;

View File

@ -1,6 +1,6 @@
package me.totalfreedom.user;
import me.totalfreedom.security.PermissionHolder;
import me.totalfreedom.security.perm.PermissionHolder;
import net.kyori.adventure.text.Component;
public interface User extends PermissionHolder

View File

@ -1,6 +1,6 @@
package me.totalfreedom.user;
import me.totalfreedom.security.Group;
import me.totalfreedom.security.perm.Group;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

View File

@ -0,0 +1,14 @@
package me.totalfreedom.utils;
public class MICheck
{
public double maintainabilityIndex(double fileLength, double vocabulary, double cyclomaticComplexity, double linesOfCode) {
double halsteadVolume = fileLength * (Math.log(vocabulary) / Math.log(2));
double maintainabilityIndexUnbounded = 171 - 5.2 * Math.log(halsteadVolume) - 0.23 * cyclomaticComplexity - 16.2 * Math.log(linesOfCode);
return Math.max(0, maintainabilityIndexUnbounded * 100 / 171);
}
public double complexity(double decisionPoints) {
return decisionPoints + 1;
}
}