mirror of
https://github.com/SimplexDevelopment/FreedomNetworkSuite.git
synced 2025-06-26 19:44:27 +00:00
Implementation Updates
This commit is contained in:
@ -1,10 +1,21 @@
|
||||
package me.totalfreedom.datura;
|
||||
|
||||
import me.totalfreedom.base.CommonsBase;
|
||||
import me.totalfreedom.datura.punishment.Cager;
|
||||
import me.totalfreedom.datura.punishment.Halter;
|
||||
import me.totalfreedom.datura.punishment.Locker;
|
||||
import me.totalfreedom.datura.sql.MySQL;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class Datura extends JavaPlugin
|
||||
{
|
||||
private final MySQL sql = new MySQL("localhost", 3011, "master");
|
||||
private final Halter halter = new Halter();
|
||||
private final Locker locker = new Locker();
|
||||
private final Cager cager = new Cager();
|
||||
|
||||
|
||||
@Override
|
||||
public void onEnable()
|
||||
{
|
||||
@ -12,5 +23,15 @@ public class Datura extends JavaPlugin
|
||||
.getRegistrations()
|
||||
.getModuleRegistry()
|
||||
.addModule(this);
|
||||
|
||||
CommonsBase.getInstance().getRegistrations().getServiceRegistry().register(this, locker);
|
||||
CommonsBase.getInstance().getRegistrations().getServiceRegistry().register(this, cager);
|
||||
|
||||
Bukkit.getPluginManager().registerEvents(halter, this);
|
||||
}
|
||||
|
||||
public MySQL getSQL()
|
||||
{
|
||||
return sql;
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,11 @@
|
||||
package me.totalfreedom.datura.perms;
|
||||
|
||||
import me.totalfreedom.base.CommonsBase;
|
||||
import me.totalfreedom.datura.Datura;
|
||||
import me.totalfreedom.datura.user.SimpleUserData;
|
||||
import me.totalfreedom.security.Node;
|
||||
import me.totalfreedom.user.User;
|
||||
import me.totalfreedom.user.UserData;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -27,6 +30,7 @@ public class FreedomUser implements User
|
||||
private final Map<Node, PermissionAttachment> bukkitAttachments = new HashMap<>();
|
||||
private final Component displayName;
|
||||
private final String NOT_ONLINE = "Player is not online";
|
||||
private final UserData userData;
|
||||
|
||||
public FreedomUser(Player player)
|
||||
{
|
||||
@ -34,6 +38,30 @@ public class FreedomUser implements User
|
||||
this.permissions = new HashSet<>();
|
||||
this.displayName = player.displayName();
|
||||
|
||||
Datura datura = CommonsBase.getInstance()
|
||||
.getRegistrations()
|
||||
.getModuleRegistry()
|
||||
.getModule(Datura.class)
|
||||
.getModule();
|
||||
|
||||
UserData data = SimpleUserData.fromSQL(datura.getSQL(), uuid.toString());
|
||||
|
||||
if (data == null)
|
||||
{
|
||||
data = new SimpleUserData(player);
|
||||
}
|
||||
|
||||
this.userData = data;
|
||||
|
||||
CommonsBase.getInstance()
|
||||
.getRegistrations()
|
||||
.getUserRegistry()
|
||||
.registerUserData(this, userData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserData getUserData() {
|
||||
return userData;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -197,9 +225,11 @@ public class FreedomUser implements User
|
||||
@Override
|
||||
public void setOp(boolean value)
|
||||
{
|
||||
if (value) {
|
||||
if (value)
|
||||
{
|
||||
permissions().add(DefaultNodes.OP);
|
||||
} else {
|
||||
} else
|
||||
{
|
||||
permissions().remove(DefaultNodes.OP);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,166 @@
|
||||
package me.totalfreedom.datura.punishment;
|
||||
|
||||
import me.totalfreedom.base.CommonsBase;
|
||||
import me.totalfreedom.service.Service;
|
||||
import me.totalfreedom.utils.Shaper;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.BlockBreakEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.DoubleUnaryOperator;
|
||||
|
||||
public class Cager extends Service
|
||||
{
|
||||
private final Set<UUID> cagedPlayers;
|
||||
private final Map<UUID, Location> cageLocations;
|
||||
|
||||
public Cager()
|
||||
{
|
||||
super("cage_service");
|
||||
this.cagedPlayers = new HashSet<>();
|
||||
this.cageLocations = new HashMap<>();
|
||||
Bukkit.getPluginManager().registerEvents(new CageListener(), CommonsBase.getInstance());
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will cage the player using {@link #createCage(Location, Material)}.
|
||||
* <p>This will also add the returned location to the {@link #cageLocations} map.
|
||||
*
|
||||
* @param uuid The UUID of the player to cage.
|
||||
*/
|
||||
public void cagePlayer(UUID uuid)
|
||||
{
|
||||
Player player = Bukkit.getPlayer(uuid);
|
||||
if (player == null) return;
|
||||
|
||||
cagedPlayers.add(uuid);
|
||||
cageLocations.put(uuid, createCage(player.getLocation(), Material.GLASS));
|
||||
}
|
||||
|
||||
public void cagePlayer(UUID uuid, Material material)
|
||||
{
|
||||
Player player = Bukkit.getPlayer(uuid);
|
||||
if (player == null) return;
|
||||
|
||||
cagedPlayers.add(uuid);
|
||||
cageLocations.put(uuid, createCage(player.getLocation(), material));
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will uncage the player by removing them from the {@link #cagedPlayers} set.
|
||||
*
|
||||
* @param uuid The UUID of the player to uncage.
|
||||
*/
|
||||
public void uncagePlayer(UUID uuid)
|
||||
{
|
||||
cagedPlayers.remove(uuid);
|
||||
Location location = cageLocations.get(uuid);
|
||||
|
||||
createCage(location, Material.AIR); // Remove the cage (set all blocks to air).
|
||||
|
||||
cageLocations.remove(uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will check to make sure each caged player remains within their cage.
|
||||
* We use
|
||||
* <p>
|
||||
* <code>{@link Location#distanceSquared(Location)} * {@link Math#pow(double, double)}</code>
|
||||
* <p>
|
||||
* to check if the player is outside the cage.
|
||||
*/
|
||||
@Override
|
||||
public void tick()
|
||||
{
|
||||
for (UUID uuid : cagedPlayers)
|
||||
{
|
||||
Player player = Bukkit.getPlayer(uuid);
|
||||
if (player == null) continue;
|
||||
|
||||
Location cageLocation = getCageLocation(player);
|
||||
|
||||
final boolean inside;
|
||||
if (!player.getWorld().equals(cageLocation.getWorld()))
|
||||
{
|
||||
inside = false;
|
||||
} else
|
||||
{
|
||||
inside = player.getLocation().distanceSquared(cageLocation) > (Math.pow(2.5, 2.0));
|
||||
}
|
||||
|
||||
if (!inside)
|
||||
{
|
||||
player.teleport(cageLocation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method returns whether the player is caged.
|
||||
* <p>This method requires the player to be online to execute properly.</p>
|
||||
*
|
||||
* @param player The player to check.
|
||||
* @return Whether the player is caged.
|
||||
*/
|
||||
public Location getCageLocation(Player player)
|
||||
{
|
||||
return cageLocations.get(player.getUniqueId());
|
||||
}
|
||||
|
||||
/**
|
||||
* This method generates a cube centered around the passed location,
|
||||
* made of the provided material. This method returns the passed location object.
|
||||
* We use the {@link Shaper} class to generate the cube, which allows us to define
|
||||
* custom shapes using {@link DoubleUnaryOperator}s.
|
||||
*
|
||||
* @param location The location to center the cube around.
|
||||
* @param material The material to use for the cube.
|
||||
* @return The center location of the cube (the passed location).
|
||||
* @see Shaper
|
||||
* @see DoubleUnaryOperator
|
||||
*/
|
||||
public Location createCage(Location location, Material material)
|
||||
{
|
||||
Shaper shaper = new Shaper(location.getWorld(), 0.0, 4.0);
|
||||
List<Location> cubed = new LinkedList<>();
|
||||
cubed.addAll(shaper.generate(5, t -> t, t -> 4.0, t -> t));
|
||||
cubed.addAll(shaper.generate(5, t -> t, t -> 0.0, t -> t));
|
||||
cubed.addAll(shaper.generate(5, t -> 0.0, t -> t, t -> t));
|
||||
cubed.addAll(shaper.generate(5, t -> 4.0, t -> t, t -> t));
|
||||
cubed.addAll(shaper.generate(5, t -> t, t -> t, t -> 0.0));
|
||||
cubed.addAll(shaper.generate(5, t -> t, t -> t, t -> 4.0));
|
||||
|
||||
for (Location l : cubed)
|
||||
{
|
||||
location.getWorld().getBlockAt(l).setType(material);
|
||||
}
|
||||
|
||||
return location.clone(); // Return the passed location as that is the center of the cube.
|
||||
}
|
||||
|
||||
private final class CageListener implements Listener
|
||||
{
|
||||
@EventHandler
|
||||
public void blockBreakEvent(BlockBreakEvent event)
|
||||
{
|
||||
if (cagedPlayers.contains(event.getPlayer().getUniqueId()))
|
||||
{
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void playerLeaveEvent(PlayerQuitEvent event) {
|
||||
if (cagedPlayers.contains(event.getPlayer().getUniqueId()))
|
||||
{
|
||||
uncagePlayer(event.getPlayer().getUniqueId());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package me.totalfreedom.datura.punishment;
|
||||
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerMoveEvent;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Halter implements Listener
|
||||
{
|
||||
private final Set<UUID> haltedPlayers;
|
||||
|
||||
public Halter()
|
||||
{
|
||||
this.haltedPlayers = new HashSet<>();
|
||||
}
|
||||
|
||||
public void halt(final UUID uuid)
|
||||
{
|
||||
this.haltedPlayers.add(uuid);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void playerMove(PlayerMoveEvent event)
|
||||
{
|
||||
if (haltedPlayers.contains(event.getPlayer().getUniqueId()))
|
||||
{
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package me.totalfreedom.datura.punishment;
|
||||
|
||||
import me.totalfreedom.base.CommonsBase;
|
||||
import me.totalfreedom.service.Service;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.SplittableRandom;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Locker extends Service
|
||||
{
|
||||
private final Set<UUID> lockedPlayers = new HashSet<>();
|
||||
|
||||
public Locker()
|
||||
{
|
||||
super("locker-service");
|
||||
}
|
||||
|
||||
public void lock(UUID uuid)
|
||||
{
|
||||
lockedPlayers.add(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick()
|
||||
{
|
||||
lockedPlayers.removeIf(uuid -> !CommonsBase.getInstance().getServer().getOfflinePlayer(uuid).isOnline());
|
||||
|
||||
for (UUID uuid : lockedPlayers)
|
||||
{
|
||||
Player player = Bukkit.getPlayer(uuid);
|
||||
if (player == null) continue;
|
||||
|
||||
lockingMethod(player);
|
||||
}
|
||||
}
|
||||
|
||||
private void lockingMethod(@NotNull Player player)
|
||||
{
|
||||
double x = player.getLocation().getX();
|
||||
double z = player.getLocation().getZ();
|
||||
|
||||
if ((x / z % 0.001) < 1)
|
||||
{
|
||||
player.setVelocity(new Vector(x % 12, 0, z % 12));
|
||||
}
|
||||
|
||||
player.setWalkSpeed(0.0f);
|
||||
player.setFlySpeed(0.0f);
|
||||
player.setAllowFlight(false);
|
||||
player.setFlying(false);
|
||||
player.setInvulnerable(true);
|
||||
player.setCollidable(false);
|
||||
player.setGliding(false);
|
||||
player.setGlowing(true);
|
||||
player.setSilent(true);
|
||||
player.setCanPickupItems(false);
|
||||
player.setInvisible(true);
|
||||
|
||||
player.openInventory(Bukkit.createInventory(null, 54));
|
||||
player.closeInventory(InventoryCloseEvent.Reason.UNKNOWN);
|
||||
player.teleport(player.getLocation().clone());
|
||||
|
||||
final SplittableRandom random = new SplittableRandom();
|
||||
player.getEyeLocation().add(new Vector(
|
||||
random.nextDouble(-1.0, 1.0),
|
||||
random.nextDouble(-1.0, 1.0),
|
||||
random.nextDouble(-1.0, 1.0)
|
||||
));
|
||||
}
|
||||
}
|
@ -0,0 +1,200 @@
|
||||
package me.totalfreedom.datura.user;
|
||||
|
||||
import me.totalfreedom.base.CommonsBase;
|
||||
import me.totalfreedom.datura.perms.FreedomUser;
|
||||
import me.totalfreedom.security.Group;
|
||||
import me.totalfreedom.sql.SQL;
|
||||
import me.totalfreedom.user.User;
|
||||
import me.totalfreedom.user.UserData;
|
||||
import me.totalfreedom.utils.FreedomLogger;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class SimpleUserData implements UserData
|
||||
{
|
||||
private final UUID uuid;
|
||||
private final String username;
|
||||
private final User user;
|
||||
private Group group;
|
||||
private long playtime;
|
||||
private boolean frozen;
|
||||
private boolean canInteract;
|
||||
private boolean caged;
|
||||
|
||||
public SimpleUserData(final Player player)
|
||||
{
|
||||
this.uuid = player.getUniqueId();
|
||||
this.username = player.getName();
|
||||
this.user = new FreedomUser(player);
|
||||
}
|
||||
|
||||
private SimpleUserData(
|
||||
final UUID uuid,
|
||||
final String username,
|
||||
final User user,
|
||||
final Group group,
|
||||
final long playtime,
|
||||
final boolean frozen,
|
||||
final boolean canInteract,
|
||||
final boolean caged)
|
||||
{
|
||||
this.uuid = uuid;
|
||||
this.username = username;
|
||||
this.user = user;
|
||||
this.group = group;
|
||||
this.playtime = playtime;
|
||||
this.frozen = frozen;
|
||||
this.canInteract = canInteract;
|
||||
this.caged = caged;
|
||||
}
|
||||
|
||||
public static SimpleUserData fromSQL(SQL sql, String uuid)
|
||||
{
|
||||
return sql.executeQuery("SELECT * FROM users WHERE UUID = ?", uuid)
|
||||
.thenApplyAsync(result ->
|
||||
{
|
||||
try
|
||||
{
|
||||
if (result.next())
|
||||
{
|
||||
String g = result.getString("group");
|
||||
|
||||
UUID u = UUID.fromString(uuid);
|
||||
String username = result.getString("username");
|
||||
|
||||
Player player = Bukkit.getPlayer(u);
|
||||
|
||||
if (player == null)
|
||||
throw new IllegalStateException("Player should be online but they are not!");
|
||||
|
||||
User user = new FreedomUser(player);
|
||||
Group group = CommonsBase.getInstance()
|
||||
.getRegistrations()
|
||||
.getGroupRegistry()
|
||||
.getGroup(g);
|
||||
long playtime = result.getLong("playtime");
|
||||
boolean frozen = result.getBoolean("frozen");
|
||||
boolean canInteract = result.getBoolean("canInteract");
|
||||
boolean caged = result.getBoolean("caged");
|
||||
return new SimpleUserData(u, username, user, group, playtime, frozen, canInteract, caged);
|
||||
}
|
||||
} catch (SQLException ex)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("An error occurred while trying to retrieve user data for UUID ")
|
||||
.append(uuid)
|
||||
.append(" from the database.")
|
||||
.append("\nCaused by: ")
|
||||
.append(ExceptionUtils.getRootCauseMessage(ex))
|
||||
.append("\nStack trace: ")
|
||||
.append(ExceptionUtils.getStackTrace(ex));
|
||||
|
||||
FreedomLogger.getLogger("Datura")
|
||||
.error(sb.toString());
|
||||
}
|
||||
|
||||
Player player = Bukkit.getPlayer(UUID.fromString(uuid));
|
||||
if (player == null) throw new IllegalStateException("Player should be online but they are not!");
|
||||
return new SimpleUserData(player);
|
||||
}, CommonsBase.getInstance()
|
||||
.getExecutor()
|
||||
.getAsync())
|
||||
.join();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull UUID getUniqueId()
|
||||
{
|
||||
return uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUsername()
|
||||
{
|
||||
return username;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getUser()
|
||||
{
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Group getGroup()
|
||||
{
|
||||
return group;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setGroup(@Nullable Group group)
|
||||
{
|
||||
this.group = group;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getPlaytime()
|
||||
{
|
||||
return playtime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPlaytime(long playtime)
|
||||
{
|
||||
this.playtime = playtime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPlaytime(long playtime)
|
||||
{
|
||||
this.playtime += playtime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetPlaytime()
|
||||
{
|
||||
this.playtime = 0L;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFrozen()
|
||||
{
|
||||
return frozen;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFrozen(boolean frozen)
|
||||
{
|
||||
this.frozen = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canInteract()
|
||||
{
|
||||
return canInteract;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInteractionState(boolean canInteract)
|
||||
{
|
||||
this.canInteract = canInteract;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCaged()
|
||||
{
|
||||
return caged;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCaged(boolean caged)
|
||||
{
|
||||
this.caged = caged;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user