9
0
mirror of https://github.com/HibiscusMC/HibiscusCommons.git synced 2025-12-19 15:09:26 +00:00

feat: move HMCC nms methods to Hibiscus Commons

This commit is contained in:
LoJoSho
2023-12-23 12:11:57 -06:00
parent c9398bfb34
commit 583c8e1528
4 changed files with 168 additions and 6 deletions

View File

@@ -17,6 +17,12 @@ public final class HibiscusCommonsPlugin extends HibiscusPlugin {
public void onStart() {
instance = this;
if (!NMSHandlers.isVersionSupported()) {
getLogger().severe("This version is not supported! Consider switching versions?");
getServer().getPluginManager().disablePlugin(this);
return;
}
// Detects if a user is running a paper server
if (ServerUtils.hasClass("com.destroystokyo.paper.PaperConfig") || ServerUtils.hasClass("io.papermc.paper.configuration.Configuration")) {
onPaper = true;
@@ -24,12 +30,6 @@ public final class HibiscusCommonsPlugin extends HibiscusPlugin {
//getServer().getPluginManager().registerEvents(new PaperPlayerGameListener(), this);
}
if (!NMSHandlers.isVersionSupported()) {
getLogger().severe("This version is not supported! Consider switching versions?");
getServer().getPluginManager().disablePlugin(this);
return;
}
// Plugin startup logic
Hooks.setup();

View File

@@ -3,7 +3,10 @@ package me.lojosho.hibiscuscommons;
import com.jeff_media.updatechecker.UpdateCheckSource;
import com.jeff_media.updatechecker.UpdateChecker;
import lombok.Getter;
import me.lojosho.hibiscuscommons.nms.NMSHandlers;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
public abstract class HibiscusPlugin extends JavaPlugin {
@@ -36,6 +39,15 @@ public abstract class HibiscusPlugin extends JavaPlugin {
public final void onEnable() {
super.onEnable();
Plugin hibiscusCommons = Bukkit.getPluginManager().getPlugin("HibiscusCommons");
if (hibiscusCommons == null || !hibiscusCommons.isEnabled()) {
getLogger().severe("");
getLogger().severe("HibiscusCommons is required to be enabled to run this plugin!");
getLogger().severe("");
getServer().getPluginManager().disablePlugin(this);
return;
}
if (bstats > 0) {
Metrics metrics = new Metrics(this, bstats);
}

View File

@@ -1,6 +1,12 @@
package me.lojosho.hibiscuscommons.nms;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
import java.util.List;
public interface NMSHandler {
@@ -8,6 +14,29 @@ public interface NMSHandler {
Entity getEntity(int entityId);
void slotUpdate(
Player player,
int slot
);
void equipmentSlotUpdate(
int entityId,
org.bukkit.inventory.EquipmentSlot slot,
ItemStack item,
List<Player> sendTo
);
void equipmentSlotUpdate(
int entityId,
HashMap<EquipmentSlot, ItemStack> equipment,
List<Player> sendTo
);
void hideNPCName(
Player player,
String NPCName
);
default boolean getSupported () {
return false;
}

View File

@@ -1,9 +1,34 @@
package me.lojosho.hibiscuscommons.nms.v1_20_R3;
import com.mojang.datafixers.util.Pair;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket;
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
import net.minecraft.network.protocol.game.ClientboundSetPlayerTeamPacket;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerPlayerConnection;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.scores.PlayerTeam;
import net.minecraft.world.scores.Team;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_20_R3.CraftEquipmentSlot;
import org.bukkit.craftbukkit.v1_20_R3.CraftServer;
import org.bukkit.craftbukkit.v1_20_R3.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_20_R3.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_20_R3.scoreboard.CraftScoreboard;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class NMSHandler implements me.lojosho.hibiscuscommons.nms.NMSHandler {
@Override
public int getNextEntityId() {
return net.minecraft.world.entity.Entity.nextEntityId();
@@ -25,6 +50,102 @@ public class NMSHandler implements me.lojosho.hibiscuscommons.nms.NMSHandler {
return null;
}
@Override
public void equipmentSlotUpdate(
int entityId,
org.bukkit.inventory.EquipmentSlot slot,
ItemStack item,
List<Player> sendTo
) {
EquipmentSlot nmsSlot = null;
net.minecraft.world.item.ItemStack nmsItem = null;
// Converting EquipmentSlot and ItemStack to NMS ones.
nmsSlot = CraftEquipmentSlot.getNMS(slot);
nmsItem = CraftItemStack.asNMSCopy(item);
if (nmsSlot == null) return;
Pair<EquipmentSlot, net.minecraft.world.item.ItemStack> pair = new Pair<>(nmsSlot, nmsItem);
List<Pair<EquipmentSlot, net.minecraft.world.item.ItemStack>> pairs = Collections.singletonList(pair);
ClientboundSetEquipmentPacket packet = new ClientboundSetEquipmentPacket(entityId, pairs);
for (Player p : sendTo) sendPacket(p, packet);
}
@Override
public void equipmentSlotUpdate(
int entityId,
HashMap<org.bukkit.inventory.EquipmentSlot, ItemStack> equipment,
List<Player> sendTo
) {
List<Pair<EquipmentSlot, net.minecraft.world.item.ItemStack>> pairs = new ArrayList<>();
for (org.bukkit.inventory.EquipmentSlot slot : equipment.keySet()) {
EquipmentSlot nmsSlot = CraftEquipmentSlot.getNMS(slot);
net.minecraft.world.item.ItemStack nmsItem = CraftItemStack.asNMSCopy(equipment.get(slot));
Pair<EquipmentSlot, net.minecraft.world.item.ItemStack> pair = new Pair<>(nmsSlot, nmsItem);
pairs.add(pair);
}
ClientboundSetEquipmentPacket packet = new ClientboundSetEquipmentPacket(entityId, pairs);
for (Player p : sendTo) sendPacket(p, packet);
}
@Override
public void slotUpdate(
Player player,
int slot
) {
int index = 0;
ServerPlayer player1 = ((CraftPlayer) player).getHandle();
if (index < Inventory.getSelectionSize()) {
index += 36;
} else if (index > 39) {
index += 5; // Off hand
} else if (index > 35) {
index = 8 - (index - 36);
}
ItemStack item = player.getInventory().getItem(slot);
Packet packet = new ClientboundContainerSetSlotPacket(player1.inventoryMenu.containerId, player1.inventoryMenu.incrementStateId(), index, CraftItemStack.asNMSCopy(item));
sendPacket(player, packet);
}
@Override
public void hideNPCName(Player player, String NPCName) {
//Creating the team
PlayerTeam team = new PlayerTeam(((CraftScoreboard) Bukkit.getScoreboardManager().getMainScoreboard()).getHandle(), NPCName);
//Setting name visibility
team.setNameTagVisibility(Team.Visibility.NEVER);
//Remove the Team (i assume so if it exists)
ClientboundSetPlayerTeamPacket removeTeamPacket = ClientboundSetPlayerTeamPacket.createRemovePacket(team);
sendPacket(player, removeTeamPacket);
//Creating the Team
ClientboundSetPlayerTeamPacket createTeamPacket = ClientboundSetPlayerTeamPacket.createAddOrModifyPacket(team, true);
sendPacket(player, createTeamPacket);
//Adding players to the team (You have to use the NPC's name, and add it to a list)
ClientboundSetPlayerTeamPacket createPlayerTeamPacket = ClientboundSetPlayerTeamPacket.createMultiplePlayerPacket(team, new ArrayList<String>() {{
add(NPCName);
}}, ClientboundSetPlayerTeamPacket.Action.ADD);
sendPacket(player, createPlayerTeamPacket);
}
public void sendPacket(Player player, Packet packet) {
ServerPlayer serverPlayer = ((CraftPlayer) player).getHandle();
ServerPlayerConnection connection = serverPlayer.connection;
connection.send(packet);
}
@Override
public boolean getSupported() {
return true;