From 7d6bd4454329711c039ba767538b3196b5f5408d Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Mon, 22 Jul 2024 06:02:55 +0200 Subject: [PATCH] Adding initial commit. --- pom.xml | 136 ++++++++++++++++++ .../me/sergiotarxz/bv/BetterVillager.java | 81 +++++++++++ src/main/resources/plugin.yml | 9 ++ 3 files changed, 226 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/me/sergiotarxz/bv/BetterVillager.java create mode 100644 src/main/resources/plugin.yml diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..b4b505a --- /dev/null +++ b/pom.xml @@ -0,0 +1,136 @@ + + 4.0.0 + me.sergiotarxz.bv + BetterVillager + + 0.1-SNAPSHOT + jar + + + + BetterVillager + Example of a Spigot plugin using Maven + http://sgray.me/project/spigotplugin + + + + Zlib License + http://opensource.org/licenses/Zlib + Copyright (c) 2014 Shaila Gray + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source + distribution. + + + + + + Shaila Gray + http://sgray.me + + + + + + SGray + me.sergiotarxz.bv.BetterVillager + UTF-8 + + + + + org.spigotmc + spigot-api + 1.20-R0.1-SNAPSHOT + + + de.tr7zw + item-nbt-api + 2.13.1 + + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/groups/public/ + + + codemc-repo + https://repo.codemc.io/repository/maven-public/ + default + + + + + src/main/java + clean install + + + src/main/resources + + true + + plugin.yml + + + + src/main/resources + + false + + **/*.java + plugin.yml + + + + + + maven-compiler-plugin + 3.1 + + -Xlint:unchecked + 17 + 17 + + + + org.apache.maven.plugins + maven-shade-plugin + 3.6.0 + + + shade + package + + shade + + + + + + + de.tr7zw.changeme.nbtapi + me.sergiotarxz.bv.nbt + + + + + + + diff --git a/src/main/java/me/sergiotarxz/bv/BetterVillager.java b/src/main/java/me/sergiotarxz/bv/BetterVillager.java new file mode 100644 index 0000000..deb6751 --- /dev/null +++ b/src/main/java/me/sergiotarxz/bv/BetterVillager.java @@ -0,0 +1,81 @@ +package me.sergiotarxz.bv; + +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.event.entity.*; +import org.bukkit.entity.*; +import org.bukkit.event.player.*; +import org.bukkit.event.EventHandler; +import java.util.logging.Level; +import org.bukkit.event.Listener; +import org.bukkit.persistence.*; +import java.util.Set; +import java.util.List; +import java.util.Iterator; +import java.util.UUID; +import org.bukkit.NamespacedKey; +import org.bukkit.metadata.MetadataValue; +import de.tr7zw.changeme.nbtapi.*; +import de.tr7zw.changeme.nbtapi.iface.*; + +public class BetterVillager extends JavaPlugin implements Listener { + @Override + public void onDisable() { + // Don't log disabling, Spigot does that for you automatically! + } + + @Override + public void onEnable() { + if (!NBT.preloadApi()) { + getLogger().warning("NBT-API wasn't initialized properly, disabling the plugin"); + getPluginLoader().disablePlugin(this); + return; + } + getServer().getPluginManager().registerEvents(this, this); + } + + + /** Event handler for when a player right-clicks on an entity. */ + @EventHandler + public void onEntityInteraction(PlayerInteractEntityEvent event) { + if (event == null || event.isCancelled()) { + return; + } + Entity entity = event.getRightClicked(); + Player player = event.getPlayer(); + UUID playerUUID = player.getUniqueId(); + if (!(entity instanceof Villager)) { + return; + } + Villager villager = (Villager) entity; + getLogger().log(Level.INFO, "Listing villager keys"); + // {Target:[I;-595241863,-91010251,-1338114026,1482075547],Type:"major_positive",Value:20} + NBT.modify(villager, nbt -> { + ReadWriteNBTCompoundList gossips = nbt.getCompoundList("Gossips"); + ReadWriteNBT currentPlayerGossip = null; + int maxValueGossip = 0; + for (int i = 0; i < gossips.size(); i++) { + ReadWriteNBT gossip = gossips.get(i); + if (!gossip.getString("Type").equals("major_positive")) { + continue; + } + int value = gossip.getInteger("Value").intValue(); + if (maxValueGossip < value) { + maxValueGossip = value; + } + if (playerUUID.equals(gossip.getUUID("Target"))) { + currentPlayerGossip = gossip; + } + getLogger().log(Level.INFO, gossip.toString()); + } + if (maxValueGossip <= 0) { + return; + } + if (currentPlayerGossip == null) { + currentPlayerGossip = gossips.addCompound(); + } + currentPlayerGossip.setUUID("Target", playerUUID); + currentPlayerGossip.setInteger("Value", new Integer(maxValueGossip)); + currentPlayerGossip.setString("Type", "major_positive"); + }); + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..10b45ba --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,9 @@ +name: ${name} +version: ${version} +description: ${description} +author: ${author} +website: ${url} + +main: ${mainClass} + +permissions: {}