Skip to content

Commit

Permalink
Merge pull request #3 from mineserv-top/dev
Browse files Browse the repository at this point in the history
Update to new structure
  • Loading branch information
AlphaMSq authored Dec 25, 2023
2 parents e568b43 + c17dab3 commit 7b82c8c
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 111 deletions.
110 changes: 62 additions & 48 deletions src/main/java/io/greitan/mineserv/GeyserVoice.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,29 @@ public void reload() {
*/
public Boolean connect(Boolean force) {
if (isConnected && !force) return true;

if (Objects.nonNull(host) && Objects.nonNull(serverKey))
{

if (Objects.nonNull(host) && Objects.nonNull(serverKey)) {
String link = "http://" + host + ":" + port;

// Create MCCommPacket object.
MCCommPacket mCCommPacket = new MCCommPacket();
mCCommPacket.PacketType = 0; // Assign the appropriate PacketType

// Create request data object.
LoginPacket loginPacket = new LoginPacket();
loginPacket.loginKey = serverKey;
loginPacket.LoginKey = serverKey;

isConnected = Network.sendPostRequest(link, loginPacket);
if (isConnected)
{
mCCommPacket.PacketData = loginPacket; // Assign the LoginPacket to PacketData

isConnected = Network.sendPostRequest(link, mCCommPacket);
if (isConnected) {
Logger.info(Language.getMessage(lang, "plugin-connect-connect"));
return true;
}
else
{
} else {
Logger.warn(Language.getMessage(lang, "plugin-connect-disconnect"));
return false;
}
}
else
{
} else {
Logger.warn(Language.getMessage(lang, "plugin-connect-invalid-data"));
return false;
}
Expand All @@ -119,25 +120,30 @@ public Boolean connect(Boolean force) {
* @param player The player to bind.
* @return True if the binding was successful, otherwise false.
*/
public Boolean bind(String playerKey, Player player)
{
if(!isConnected || Objects.isNull(host) || Objects.isNull(serverKey) ) return false;
public Boolean bind(String playerKey, Player player) {
if (!isConnected || Objects.isNull(host) || Objects.isNull(serverKey)) return false;
String link = "http://" + host + ":" + port;

getConfig().set("config.players."+player.getName(), playerKey);
getConfig().set("config.players." + player.getName(), playerKey);
saveConfig();


// Create MCCommPacket object.
MCCommPacket mCCommPacket = new MCCommPacket();
mCCommPacket.PacketType = 1; // Assign the appropriate PacketType

// Create request data object.
BindingPacket bindingPacket = new BindingPacket();
bindingPacket.playerId = player.getEntityId();
bindingPacket.gamertag = player.getName();
bindingPacket.playerKey = playerKey;
bindingPacket.loginKey = serverKey;

boolean bindStatus = Network.sendPostRequest(link, bindingPacket);

bindingPacket.PlayerId = player.getEntityId();
bindingPacket.Gamertag = player.getName();
bindingPacket.PlayerKey = playerKey;
bindingPacket.LoginKey = serverKey;

mCCommPacket.PacketData = bindingPacket; // Assign the BindingPacket to PacketData

boolean bindStatus = Network.sendPostRequest(link, mCCommPacket);

playerBinds.put(player.getName(), bindStatus);

return bindStatus;
}

Expand All @@ -148,17 +154,23 @@ public Boolean bind(String playerKey, Player player)
* @param player The player to disconnect.
* @return True if the disconnection was successful, otherwise false.
*/
public Boolean disconnectPlayer(Player player){
if(!isConnected || Objects.isNull(host) || Objects.isNull(serverKey) ) return false;
public Boolean disconnectPlayer(Player player) {
if (!isConnected || Objects.isNull(host) || Objects.isNull(serverKey)) return false;
String link = "http://" + host + ":" + port;


// Create MCCommPacket object.
MCCommPacket mCCommPacket = new MCCommPacket();
mCCommPacket.PacketType = 5; // Assign the appropriate PacketType

// Create request data object.
DisconnectPlayerPacket disconnectPlayerPacket = new DisconnectPlayerPacket();
disconnectPlayerPacket.loginKey = serverKey;
disconnectPlayerPacket.playerId = player.getEntityId();

boolean disconnectStatus = Network.sendPostRequest(link, disconnectPlayerPacket);

disconnectPlayerPacket.LoginKey = serverKey;
disconnectPlayerPacket.PlayerId = player.getEntityId();

mCCommPacket.PacketData = disconnectPlayerPacket; // Assign the DisconnectPlayerPacket to PacketData

boolean disconnectStatus = Network.sendPostRequest(link, mCCommPacket);

return disconnectStatus;
}

Expand All @@ -170,21 +182,23 @@ public Boolean disconnectPlayer(Player player){
* @param voiceEffects Voice effects setting.
* @return True if settings were updated successfully, otherwise false.
*/
public Boolean updateSettings(int proximityDistance, Boolean proximityToggle, Boolean voiceEffects){
if(!isConnected || Objects.isNull(host) || Objects.isNull(serverKey) ) return false;
public Boolean updateSettings(int proximityDistance, Boolean proximityToggle, Boolean voiceEffects) {
if (!isConnected || Objects.isNull(host) || Objects.isNull(serverKey)) return false;
String link = "http://" + host + ":" + port;

// Create server settings data object.
ServerSettings serverSettings = new ServerSettings();
serverSettings.proximityDistance = proximityDistance;
serverSettings.proximityToggle = proximityToggle;
serverSettings.voiceEffects = voiceEffects;


// Create MCCommPacket object.
MCCommPacket mCCommPacket = new MCCommPacket();
mCCommPacket.PacketType = 3; // Assign the appropriate PacketType

// Create request data object.
UpdateSettingsPacket updateSettingsPacket = new UpdateSettingsPacket();
updateSettingsPacket.loginKey = serverKey;
updateSettingsPacket.settings = serverSettings;

return Network.sendPostRequest(link, updateSettingsPacket);
updateSettingsPacket.LoginKey = serverKey;
updateSettingsPacket.ProximityDistance = proximityDistance;
updateSettingsPacket.ProximityToggle = proximityToggle;
updateSettingsPacket.VoiceEffects = voiceEffects;

mCCommPacket.PacketData = updateSettingsPacket; // Assign the UpdateSettingsPacket to PacketData

return Network.sendPostRequest(link, mCCommPacket);
}
}
43 changes: 19 additions & 24 deletions src/main/java/io/greitan/mineserv/network/Payloads.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,41 @@
import java.util.List;

public class Payloads {
public static class MCCommPacket {
public int PacketType = 0;
public Object PacketData = null;
}

public static class LoginPacket {
public int type = 0;
public String loginKey = "";
public String LoginKey = "";
}

public static class BindingPacket {
public int type = 1;
public String loginKey = "";
public int playerId = 0;
public String playerKey = "";
public String gamertag = "";
public String LoginKey = "";
public int PlayerId = 0;
public String PlayerKey = "";
public String Gamertag = "";
}

public static class UpdatePlayersPacket {
public int type = 2;
public String loginKey = "";
public List<PlayerData> players;
public String LoginKey = "";
public List<PlayerData> Players;
}

public static class UpdateSettingsPacket {
public int type = 3;
public String loginKey = "";
public ServerSettings settings = new ServerSettings();
public String LoginKey = "";
public int ProximityDistance = 30;
public boolean ProximityToggle = true;
public boolean VoiceEffects = true;
}

public static class GetSettingsPacket {
public int type = 4;
public String loginKey = "";
public String LoginKey = "";
}

public static class DisconnectPlayerPacket {
public int type = 5;
public String loginKey = "";
public int playerId = 0;
}

public static class ServerSettings {
public int proximityDistance = 30;
public boolean proximityToggle = true;
public boolean voiceEffects = true;
public String LoginKey = "";
public int PlayerId = 0;
}

public static class PlayerData {
Expand Down
76 changes: 37 additions & 39 deletions src/main/java/io/greitan/mineserv/tasks/PositionsTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,33 @@ public class PositionsTask extends BukkitRunnable {
private final GeyserVoice plugin;
private boolean isConnected = false;

public PositionsTask(GeyserVoice plugin)
{
public PositionsTask(GeyserVoice plugin) {
this.plugin = plugin;
}

@Override
public void run()
{
public void run() {
isConnected = plugin.isConnected();
String host = plugin.getHost();
int port = plugin.getPort();
String serverKey = plugin.getServerKey();
String link = "http://" + host + ":" + port;

if(isConnected){
if (isConnected) {
if (host != null && serverKey != null) {
UpdatePlayersPacket updatePlayersPacket = new UpdatePlayersPacket();
updatePlayersPacket.loginKey = serverKey;
updatePlayersPacket.players = getPlayerDataList();
updatePlayersPacket.LoginKey = serverKey;
updatePlayersPacket.Players = getPlayerDataList();

Network.sendPostRequest(link, updatePlayersPacket);
}
}
}

public List<PlayerData> getPlayerDataList()
{
public List<PlayerData> getPlayerDataList() {
List<PlayerData> playerDataList = new ArrayList<>();

for (Player player : Bukkit.getServer().getOnlinePlayers())
{
for (Player player : Bukkit.getServer().getOnlinePlayers()) {
Location headLocation = player.getEyeLocation();

LocationData locationData = new LocationData();
Expand All @@ -64,62 +60,64 @@ public List<PlayerData> getPlayerDataList()
playerData.DimensionId = getDimensionId(player);
playerData.Location = locationData;
playerData.Rotation = player.getLocation().getYaw();

if (player.getWorld().getEnvironment() == World.Environment.NORMAL) {
playerData.CaveDensity = getCaveDensity(player);
} else {
playerData.CaveDensity = 0.0;
}
playerData.IsDead = player.isDead();
playerData.InWater = player.isInWater();

playerDataList.add(playerData);
}

return playerDataList;
}

public double getCaveDensity(Player player)
{
if (!isConnected)
{
public double getCaveDensity(Player player) {
if (!isConnected) {
return 0.0;
}

String[] caveBlocks = {
"STONE",
"DIORITE",
"GRANITE",
"DEEPSLATE",
"TUFF"
};

int block1 = Arrays.asList(caveBlocks).contains(getBlockType(getRelativeLocation(player.getLocation(), 0, 1, 0))) ? 1 : 0;
int block2 = Arrays.asList(caveBlocks).contains(getBlockType(getRelativeLocation(player.getLocation(), -1, 0, 0))) ? 1 : 0;
int block3 = Arrays.asList(caveBlocks).contains(getBlockType(getRelativeLocation(player.getLocation(), 1, 0, 0))) ? 1 : 0;
int block4 = Arrays.asList(caveBlocks).contains(getBlockType(getRelativeLocation(player.getLocation(), 0, 0, 1))) ? 1 : 0;
int block5 = Arrays.asList(caveBlocks).contains(getBlockType(getRelativeLocation(player.getLocation(), 0, 0, -1))) ? 1 : 0;
int block6 = Arrays.asList(caveBlocks).contains(getBlockType(getRelativeLocation(player.getLocation(), 0, -1, 0))) ? 1 : 0;

return (block1 + block2 + block3 + block4 + block5 + block6) / 6.0;

int blockCount = 0;
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
for (int z = -1; z <= 1; z++) {
Location relativeLoc = getRelativeLocation(player.getLocation(), x, y, z);
if (Arrays.asList(caveBlocks).contains(getBlockType(relativeLoc))) {
blockCount++;
}
}
}
}

return blockCount / 27.0; // Total blocks checked
}

private Location getRelativeLocation(Location base, double x, double y, double z)
{

private Location getRelativeLocation(Location base, double x, double y, double z) {
return new Location(base.getWorld(), base.getX() + x, base.getY() + y, base.getZ() + z);
}

private String getBlockType(Location location)
{

private String getBlockType(Location location) {
return location.getBlock().getType().toString();
}

private String getDimensionId(Player player)
{
private String getDimensionId(Player player) {
String worldName = player.getWorld().getName();
return worldName.equals("world") ? "minecraft:overworld" :
worldName.equals("world_nether") ? "minecraft:nether" :
worldName.equals("world_the_end") ? "minecraft:the_end" :
"minecraft:unknown";
return switch (worldName) {
case "world" -> "minecraft:overworld";
case "world_nether" -> "minecraft:nether";
case "world_the_end" -> "minecraft:the_end";
default -> "minecraft:unknown";
};
}
}

0 comments on commit 7b82c8c

Please sign in to comment.