wip
All checks were successful
/ deploy (push) Successful in 1m16s

This commit is contained in:
Minecon724 2024-11-16 12:37:12 +01:00
parent e3e5f58f32
commit 7598aded28
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
12 changed files with 283 additions and 82 deletions

View file

@ -0,0 +1,18 @@
on: [push]
jobs:
deploy:
runs-on: docker
container: debian:sid
steps:
- name: Prepare for installation
run: apt update
- name: Install JDK
run: apt install --no-install-recommends -y openjdk-21-jdk-headless maven git nodejs
- name: Clone repository
run: git clone https://git.m724.eu/Minecon724/mutils.git .
- name: Build
run: mvn clean package
- name: Upload artifacts
uses: https://github.com/actions/upload-artifact@v3
with:
path: target/mutils-*.jar

View file

@ -0,0 +1,22 @@
package eu.m724.tweaks;
import eu.m724.tweaks.chat.ChatCommands;
import eu.m724.tweaks.chat.ChatManager;
import eu.m724.tweaks.door.DoorListener;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Objects;
public class TweaksPlugin extends JavaPlugin {
@Override
public void onEnable() {
ChatManager chatManager = new ChatManager(this);
chatManager.init();
ChatCommands chatCommands = new ChatCommands(chatManager);
Objects.requireNonNull(getCommand("chat")).setExecutor(chatCommands);
Objects.requireNonNull(getCommand("chatmanage")).setExecutor(chatCommands);
getServer().getPluginManager().registerEvents(new DoorListener(), this);
}
}

View file

@ -0,0 +1,20 @@
package eu.m724.tweaks.auth;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLoginEvent;
public class AuthListener implements Listener {
private final AuthManager authManager;
public AuthListener(AuthManager authManager) {
this.authManager = authManager;
}
@EventHandler
public void onPlayerLogin(PlayerLoginEvent event) {
Player player = event.getPlayer();
String hostname = event.getHostname();
}
}

View file

@ -0,0 +1,11 @@
package eu.m724.tweaks.auth;
import org.bukkit.plugin.Plugin;
public class AuthManager {
private final Plugin plugin;
public AuthManager(Plugin plugin) {
this.plugin = plugin;
}
}

View file

@ -1,4 +1,4 @@
package eu.m724.utils.chat; package eu.m724.tweaks.chat;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
@ -57,6 +57,8 @@ public class ChatCommands implements CommandExecutor {
component = new ComponentBuilder("Invalid password").color(ChatColor.RED) component = new ComponentBuilder("Invalid password").color(ChatColor.RED)
.create(); .create();
} }
} else {
authenticated = true;
} }
} else { } else {
component = new ComponentBuilder("No room named ").color(ChatColor.RED) component = new ComponentBuilder("No room named ").color(ChatColor.RED)
@ -79,16 +81,14 @@ public class ChatCommands implements CommandExecutor {
} else if (command.getName().equals("chatmanage")) { } else if (command.getName().equals("chatmanage")) {
Player player = (Player) sender; Player player = (Player) sender;
ChatRoom chatRoom = manager.getPlayerChatRoom(player); ChatRoom chatRoom = manager.getPlayerChatRoom(player);
boolean isOwner = player.equals(chatRoom.owner);
if (!chatRoom.owner.equals(player)) {
sender.sendMessage("You're not the owner of %s, please enter the room you want to make changes in".formatted(chatRoom.id));
return true;
}
if (args.length > 1) { if (args.length > 1) {
String action = args[0]; String action = args[0];
String argument = args[1]; String argument = args[1];
if (action.equals("create")) {
switch (action) {
case "create" -> {
try { try {
ChatRoom newRoom = manager.createChatRoom(argument, null, player); ChatRoom newRoom = manager.createChatRoom(argument, null, player);
sender.sendMessage("Created a chat room. Join it: /c " + newRoom.id); sender.sendMessage("Created a chat room. Join it: /c " + newRoom.id);
@ -101,9 +101,20 @@ public class ChatCommands implements CommandExecutor {
sender.sendMessage("Failed to create room"); sender.sendMessage("Failed to create room");
e.printStackTrace(); e.printStackTrace();
} }
} else if (action.equals("delete")) { }
case "delete" -> {
if (argument.equals(chatRoom.id)) {
if (isOwner) {
// TODO // TODO
} else if (action.equals("setowner")) { } else {
sender.sendMessage("You're not the owner of %s, please enter the room you want to make changes in".formatted(chatRoom.id));
}
} else {
sender.sendMessage("Pass %s as an argument to confirm".formatted(chatRoom.id));
}
}
case "setowner" -> {
if (isOwner) {
Player newOwner = Bukkit.getPlayer(argument); Player newOwner = Bukkit.getPlayer(argument);
if (newOwner != null && newOwner.isOnline()) { if (newOwner != null && newOwner.isOnline()) {
chatRoom.owner = newOwner; chatRoom.owner = newOwner;
@ -117,7 +128,12 @@ public class ChatCommands implements CommandExecutor {
} else { } else {
sender.sendMessage("Player must be online"); sender.sendMessage("Player must be online");
} }
} else if (action.equals("setpassword")) { } else {
sender.sendMessage("You're not the owner of %s, please enter the room you want to make changes in".formatted(chatRoom.id));
}
}
case "setpassword" -> {
if (isOwner) {
chatRoom.password = Arrays.stream(args).skip(1).collect(Collectors.joining(" ")).strip(); chatRoom.password = Arrays.stream(args).skip(1).collect(Collectors.joining(" ")).strip();
try { try {
manager.saveChatRoom(chatRoom); manager.saveChatRoom(chatRoom);
@ -126,7 +142,12 @@ public class ChatCommands implements CommandExecutor {
sender.sendMessage("Failed to change password"); sender.sendMessage("Failed to change password");
e.printStackTrace(); e.printStackTrace();
} }
} else if (action.equals("setcolor")) { } else {
sender.sendMessage("You're not the owner of %s, please enter the room you want to make changes in".formatted(chatRoom.id));
}
}
case "setcolor" -> {
if (isOwner) {
ChatColor newColor = ChatColor.of(argument); ChatColor newColor = ChatColor.of(argument);
if (newColor != null) { if (newColor != null) {
chatRoom.color = newColor; chatRoom.color = newColor;
@ -140,6 +161,13 @@ public class ChatCommands implements CommandExecutor {
} else { } else {
sender.sendMessage("Invalid color"); sender.sendMessage("Invalid color");
} }
} else {
sender.sendMessage("You're not the owner of %s, please enter the room you want to make changes in".formatted(chatRoom.id));
}
}
default -> {
sender.sendMessage("Actions: create, delete, setowner, setpassword, setcolor");
}
} }
} else if (args.length > 0) { } else if (args.length > 0) {
switch (args[0]) { switch (args[0]) {
@ -153,10 +181,11 @@ public class ChatCommands implements CommandExecutor {
sender.sendMessage("To change the password of room %s, pass the new password as an argument for this action.".formatted(chatRoom)); sender.sendMessage("To change the password of room %s, pass the new password as an argument for this action.".formatted(chatRoom));
case "setcolor" -> case "setcolor" ->
sender.sendMessage("To change the message color of room %s, pass the new color as an argument for this action. #hex or color name.".formatted(chatRoom)); sender.sendMessage("To change the message color of room %s, pass the new color as an argument for this action. #hex or color name.".formatted(chatRoom));
default -> sender.sendMessage("create, delete, setowner, setpassword"); default ->
sender.sendMessage("Actions: create, delete, setowner, setpassword, setcolor");
} }
} else { } else {
sender.sendMessage("create, delete, setowner, setpassword, setcolor"); sender.sendMessage("Actions: create, delete, setowner, setpassword, setcolor");
} }
} }

View file

@ -1,4 +1,4 @@
package eu.m724.utils.chat; package eu.m724.tweaks.chat;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;

View file

@ -1,4 +1,4 @@
package eu.m724.utils.chat; package eu.m724.tweaks.chat;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;

View file

@ -1,4 +1,4 @@
package eu.m724.utils.chat; package eu.m724.tweaks.chat;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
@ -6,6 +6,7 @@ import net.md_5.bungee.api.chat.ComponentBuilder;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -37,15 +38,18 @@ public class ChatRoom {
if (owner != null) if (owner != null)
builder = builder.append("\nOwner: ").color(ChatColor.GOLD) builder = builder.append("\nOwner: ").color(ChatColor.GOLD)
.append(owner.getName()).color(ChatColor.AQUA); .append(owner.getName()).color(ChatColor.AQUA);
if (!players.isEmpty()) {
builder = builder.append("\nOnline (%d): ".formatted(players.size())).color(ChatColor.GOLD); builder = builder.append("\nOnline (%d): ".formatted(players.size())).color(ChatColor.GOLD);
List<Player> playersList = players.stream().sorted().toList(); List<Player> playersList = new ArrayList<>(players);
builder = builder.append(playersList.removeFirst().getName()).color(ChatColor.GRAY); builder = builder.append(playersList.removeFirst().getName()).color(ChatColor.GRAY);
for (Player player : playersList) { for (Player player : playersList) {
builder = builder.append(", ").color(ChatColor.GRAY) builder = builder.append(", ").color(ChatColor.GRAY)
.append(player.getName()).color(ChatColor.AQUA); .append(player.getName()).color(ChatColor.AQUA);
} }
}
return builder.create(); return builder.create();
} }

View file

@ -1,4 +1,4 @@
package eu.m724.utils.chat; package eu.m724.tweaks.chat;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -37,7 +37,7 @@ public class ChatRoomLoader {
return 2; return 2;
} else if (!id.equals(id.toLowerCase())) { } else if (!id.equals(id.toLowerCase())) {
return 3; return 3;
} else if (id.chars().allMatch(Character::isLetterOrDigit)) { } else if (!id.chars().allMatch(Character::isLetterOrDigit)) {
return 4; return 4;
} }

View file

@ -0,0 +1,113 @@
package eu.m724.tweaks.door;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.type.Door;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockDamageEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.RayTraceResult;
import java.util.concurrent.ThreadLocalRandom;
public class DoorListener implements Listener {
@EventHandler
public void onBlockDamage(BlockDamageEvent event) {
Block block = event.getBlock();
if (!(block.getBlockData() instanceof Door door)) return;
World world = block.getLocation().getWorld();
Player player = event.getPlayer();
// TODO maybe it would be faster to check just player and the door
RayTraceResult result = player.rayTraceBlocks(5);
if (result == null) return;
Location hitLocation = result.getHitPosition().toLocation(world);
double distance = player.getEyeLocation().distanceSquared(hitLocation);
if (distance > 12) return;
Sound sound = block.getType() == Material.IRON_DOOR ? Sound.ENTITY_ZOMBIE_ATTACK_IRON_DOOR : Sound.ENTITY_ZOMBIE_ATTACK_WOODEN_DOOR;
float volume = player.isSneaking() ? 0.4f : 1f;
float pitch = player.getFallDistance() > 0 ? 1f : 1.5f;
if (player.hasPotionEffect(PotionEffectType.NAUSEA)) {
pitch = ThreadLocalRandom.current().nextFloat(0.5f, 0.7f);
}
PotionEffect weakness = player.getPotionEffect(PotionEffectType.WEAKNESS);
PotionEffect fatigue = player.getPotionEffect(PotionEffectType.MINING_FATIGUE);
int level = (weakness != null ? weakness.getAmplifier() : 0) + (fatigue != null ? fatigue.getAmplifier() : 0);
if (weakness != null || fatigue != null) {
volume /= level / 3f;
pitch /= level;
}
volume *= (float) ((10.0 - Math.min(distance - 2, 10.0)) / 10.0);
world.playSound(hitLocation, sound, volume, pitch);
world.spawnParticle(Particle.BLOCK, hitLocation, (int) (10 * volume), door);
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
// make sure the player is opening a door
if (!event.hasBlock()) return;
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
Block block = event.getClickedBlock();
if (!(block.getBlockData() instanceof Door door)) return;
// check if the doors are in like one line
boolean right = door.getHinge() == Door.Hinge.RIGHT;
Location location = block.getLocation();
BlockFace expectedFacing = door.getFacing();
switch (door.getFacing()) {
case NORTH -> location.add(right ? -1 : 1, 0, 0);
case SOUTH -> location.add(right ? 1 : -1, 0, 0);
case EAST -> location.add(0, 0, right ? -1 : 1);
case WEST -> location.add(0, 0, right ? 1 : -1);
}
// if not, check if the doors are a corner
if (location.getBlock().isEmpty()) {
location = block.getLocation();
switch (door.getFacing()) {
case NORTH -> {
location.add(right ? -1 : 1, 0, 1);
expectedFacing = right ? BlockFace.WEST : BlockFace.EAST;
}
case SOUTH -> {
location.add(right ? 1 : -1, 0, -1);
expectedFacing = right ? BlockFace.EAST : BlockFace.WEST;
}
case EAST -> {
location.add(-1, 0, right ? -1 : 1);
expectedFacing = right ? BlockFace.NORTH : BlockFace.SOUTH;
}
case WEST -> {
location.add(1, 0, right ? 1 : -1);
expectedFacing = right ? BlockFace.SOUTH : BlockFace.NORTH;
}
}
}
// if there's a second door placed correctly
if (location.getBlock().getType() == block.getType()) {
Door nextDoor = (Door) location.getBlock().getBlockData();
if (nextDoor.getHinge() == door.getHinge()) return;
if (nextDoor.getFacing() != expectedFacing) return;
// ! because it seems door is actually opening after this event
nextDoor.setOpen(!door.isOpen());
location.getBlock().setBlockData(nextDoor);
}
}
}

View file

@ -1,16 +0,0 @@
package eu.m724.utils;
import eu.m724.utils.chat.ChatCommands;
import eu.m724.utils.chat.ChatManager;
import org.bukkit.plugin.java.JavaPlugin;
public class UtilsPlugin extends JavaPlugin {
@Override
public void onEnable() {
ChatManager chatManager = new ChatManager(this);
chatManager.init();
ChatCommands chatCommands = new ChatCommands(chatManager);
getCommand("chat").setExecutor(chatCommands);
}
}

View file

@ -1,7 +1,7 @@
name: mUtils724 name: Tweaks724
version: ${project.version} version: ${project.version}
main: eu.m724.utils.UtilsPlugin main: eu.m724.tweaks.TweaksPlugin
api-version: 1.21.1 api-version: 1.21.1
commands: commands: