Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
<source>14</source>
<target>14</target>
</configuration>
</plugin>
</plugins>
Expand All @@ -39,6 +39,12 @@
<version>3.2.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>5.2.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>

<repositories>
Expand All @@ -50,5 +56,9 @@
<id>rosewood-repo</id>
<url>https://repo.rosewooddev.io/repository/public/</url>
</repository>
<repository>
<id>dmulloy2-repo</id>
<url>https://repo.dmulloy2.net/repository/public/</url>
</repository>
</repositories>
</project>
70 changes: 70 additions & 0 deletions src/main/java/com/comphenix/packetwrapper/AbstractPacket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.comphenix.packetwrapper;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer;
import com.google.common.base.Objects;
import org.bukkit.entity.Player;

public abstract class AbstractPacket {
// The packet we will be modifying
protected PacketContainer handle;

/**
* Constructs a new strongly typed wrapper for the given packet.
*
* @param handle - handle to the raw packet data.
* @param type - the packet type.
*/
protected AbstractPacket(PacketContainer handle, PacketType type) {
// Make sure we're given a valid packet
if (handle == null)
throw new IllegalArgumentException("Packet handle cannot be NULL.");
if (!Objects.equal(handle.getType(), type))
throw new IllegalArgumentException(handle.getHandle()
+ " is not a packet of type " + type);

this.handle = handle;
}

/**
* Retrieve a handle to the raw packet data.
*
* @return Raw packet data.
*/
public PacketContainer getHandle() {
return handle;
}

/**
* Send the current packet to the given receiver.
*
* @param receiver - the receiver.
* @throws RuntimeException If the packet cannot be sent.
*/
public void sendPacket(Player receiver) {
ProtocolLibrary.getProtocolManager().sendServerPacket(receiver, getHandle());
}

/**
* Send the current packet to all online players.
*/
public void broadcastPacket() {
ProtocolLibrary.getProtocolManager().broadcastServerPacket(getHandle());
}

/**
* Simulate receiving the current packet from the given sender.
*
* @param sender - the sender.
* @throws RuntimeException if the packet cannot be received.
*/
public void receivePacket(Player sender) {
try {
ProtocolLibrary.getProtocolManager().receiveClientPacket(sender,
getHandle());
} catch (Exception e) {
throw new RuntimeException("Cannot receive packet.", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* PacketWrapper - ProtocolLib wrappers for Minecraft packets
* Copyright (C) dmulloy2 <http://dmulloy2.net>
* Copyright (C) Kristian S. Strangeland
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.comphenix.packetwrapper;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.wrappers.WrappedDataValue;
import java.util.List;
import org.bukkit.World;
import org.bukkit.entity.Entity;

public class WrapperPlayServerEntityMetadata extends AbstractPacket {
public static final PacketType TYPE =
PacketType.Play.Server.ENTITY_METADATA;

public WrapperPlayServerEntityMetadata() {
super(new PacketContainer(TYPE), TYPE);
handle.getModifier().writeDefaults();
}

public WrapperPlayServerEntityMetadata(PacketContainer packet) {
super(packet, TYPE);
}

/**
* Retrieve Entity ID.
* <p>
* Notes: entity's ID
*
* @return The current Entity ID
*/
public int getEntityID() {
return handle.getIntegers().read(0);
}

/**
* Set Entity ID.
*
* @param value - new value.
*/
public void setEntityID(int value) {
handle.getIntegers().write(0, value);
}

/**
* Retrieve the entity of the painting that will be spawned.
*
* @param world - the current world of the entity.
* @return The spawned entity.
*/
public Entity getEntity(World world) {
return handle.getEntityModifier(world).read(0);
}

/**
* Retrieve the entity of the painting that will be spawned.
*
* @param event - the packet event.
* @return The spawned entity.
*/
public Entity getEntity(PacketEvent event) {
return getEntity(event.getPlayer().getWorld());
}

/**
* Retrieve Metadata.
*
* @return The current Metadata
*/
public List<WrappedDataValue> getMetadata() {
return handle.getDataValueCollectionModifier().read(0);
}

/**
* Set Metadata.
*
* @param value - new value.
*/
public void setMetadata(List<WrappedDataValue> value) {
handle.getDataValueCollectionModifier().write(0, value);
}
}
7 changes: 7 additions & 0 deletions src/main/java/io/github/hielkemaps/serverplugin/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.hielkemaps.serverplugin;

import com.comphenix.protocol.ProtocolLibrary;
import dev.jorel.commandapi.CommandAPI;
import io.github.hielkemaps.serverplugin.commands.*;
import io.github.hielkemaps.serverplugin.events.EventListener;
Expand All @@ -9,6 +10,7 @@
import net.kyori.adventure.text.format.TextColor;
import org.black_ixx.playerpoints.PlayerPoints;
import org.black_ixx.playerpoints.PlayerPointsAPI;
import io.github.hielkemaps.serverplugin.events.PacketListener;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Sound;
Expand Down Expand Up @@ -56,6 +58,8 @@ public void onEnable() {
pointsAPI = PlayerPoints.getInstance().getAPI();

loadConfig();

ProtocolLibrary.getProtocolManager().addPacketListener(new PacketListener(this));
}

public static void loadConfig() {
Expand All @@ -77,6 +81,9 @@ public static void loadConfig() {
if (!disabledCommands.contains("highscores")) new Highscores();
else CommandAPI.unregister("highscores");

if(!disabledCommands.contains("player_visibility")) new PlayerVisibility();
else CommandAPI.unregister("player_visibility");

Bukkit.getOnlinePlayers().forEach(CommandAPI::updateRequirements);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.github.hielkemaps.serverplugin;

public enum PlayerVisibilityOption {

VISIBLE, INVISIBLE, GHOST;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.hielkemaps.serverplugin.commands;

import dev.jorel.commandapi.CommandAPICommand;
import io.github.hielkemaps.serverplugin.PlayerVisibilityOption;
import io.github.hielkemaps.serverplugin.wrapper.PlayerManager;

public class PlayerVisibility {

public PlayerVisibility() {
new CommandAPICommand("player_visibility")
.withAliases("playervisibility", "pv")

.withSubcommand(new CommandAPICommand("visible")
.executesPlayer((p, args) -> {
PlayerManager.getPlayer(p.getUniqueId()).setVisibilityOption(PlayerVisibilityOption.VISIBLE);
}))

.withSubcommand(new CommandAPICommand("invisible")
.executesPlayer((p, args) -> {
PlayerManager.getPlayer(p.getUniqueId()).setVisibilityOption(PlayerVisibilityOption.INVISIBLE);
}))

.withSubcommand(new CommandAPICommand("ghost")
.executesPlayer((p, args) -> {
PlayerManager.getPlayer(p.getUniqueId()).setVisibilityOption(PlayerVisibilityOption.GHOST);
}))

.register();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import dev.jorel.commandapi.CommandAPI;
import io.github.hielkemaps.serverplugin.Main;
import io.github.hielkemaps.serverplugin.PlayerVisibilityOption;
import io.github.hielkemaps.serverplugin.wrapper.PlayerManager;
import io.github.hielkemaps.serverplugin.wrapper.PlayerWrapper;
import java.util.Collection;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
Expand All @@ -16,9 +20,6 @@
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import java.util.Collection;
import java.util.Set;

public class EventListener implements Listener {
Set<Material> flowerpots;

Expand Down Expand Up @@ -59,6 +60,30 @@ public void onJoin(PlayerJoinEvent e) {
onlinePlayers.forEach(CommandAPI::updateRequirements);
}
}

PlayerWrapper player = PlayerManager.getPlayer(e.getPlayer().getUniqueId());

for (Player other : Bukkit.getOnlinePlayers()) {
if (!other.getUniqueId().equals(e.getPlayer().getUniqueId())) {
PlayerWrapper wrapper = PlayerManager.getPlayer(other.getUniqueId());

// If the other player has players invisible, hide the new player
if (wrapper.getVisibilityOption() == PlayerVisibilityOption.INVISIBLE) {
other.hidePlayer(Main.getInstance(), e.getPlayer());
} else {
other.showPlayer(Main.getInstance(), e.getPlayer());
}

// If the new player has players invisible, hide the other player
if (player.getVisibilityOption() == PlayerVisibilityOption.INVISIBLE) {
e.getPlayer().hidePlayer(Main.getInstance(), other);
} else {
e.getPlayer().showPlayer(Main.getInstance(), other);
}

// Don't need to handle ghost players here, as it is handled by the PacketListener
}
}
}

@EventHandler
Expand Down
Loading