Initial commit

This commit is contained in:
Torrie Fischer 2022-05-02 07:35:22 +02:00
commit d1e86ac898
6 changed files with 210 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
target
test-server

54
pom.xml Normal file
View File

@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>gg.malloc.coins</groupId>
<artifactId>malloc-coins</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>codemc-repo</id>
<url>https://repo.codemc.org/repository/maven-public/</url>
<layout>default</layout>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.18.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>de.tr7zw</groupId>
<artifactId>item-nbt-api-plugin</artifactId>
<version>2.9.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.MilkBowl</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.7</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,71 @@
package gg.malloc.coins;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.SoundCategory;
import org.bukkit.Sound;
import net.milkbowl.vault.economy.Economy;
import de.tr7zw.nbtapi.NBTItem;
import de.tr7zw.nbtapi.NBTCompound;
public class CoinPickupHandler implements Listener {
Economy m_vault;
public CoinPickupHandler(Economy vault) {
m_vault = vault;
}
@EventHandler
public void onInteract(PlayerInteractEvent evt) {
if (evt.getItem() != null) {
NBTItem nbt = new NBTItem(evt.getItem());
System.out.println(nbt);
if (nbt.hasKey("malloc")) {
NBTCompound mallocData = nbt.getCompound("malloc");
if (mallocData.hasKey("coinValue")) {
evt.setCancelled(true);
int coinValue = mallocData.getInteger("coinValue") * evt.getItem().getAmount();
m_vault.depositPlayer(evt.getPlayer(), coinValue);
evt.getPlayer().getWorld().playSound(evt.getPlayer(), Sound.BLOCK_CHAIN_PLACE, SoundCategory.PLAYERS, (float)1.0, (float)1.0);
evt.getPlayer().getInventory().setItem(evt.getHand(), null);
evt.setCancelled(true);
}
}
}
}
@EventHandler
public void onItemCraftPrepare(PrepareItemCraftEvent evt) {
for(ItemStack item : evt.getInventory().getMatrix()) {
if (item != null) {
NBTItem nbt = new NBTItem(item);
if (nbt.hasKey("malloc")) {
evt.getInventory().setResult(null);
return;
}
}
}
}
@EventHandler
public void onItemPickup(PlayerPickupItemEvent evt) {
NBTItem nbt = new NBTItem(evt.getItem().getItemStack());
System.out.println(nbt);
if (nbt.hasKey("malloc")) {
NBTCompound mallocData = nbt.getCompound("malloc");
if (mallocData.hasKey("coinValue")) {
evt.setCancelled(true);
int coinValue = mallocData.getInteger("coinValue") * evt.getItem().getItemStack().getAmount();
m_vault.depositPlayer(evt.getPlayer(), coinValue);
evt.getPlayer().getWorld().playSound(evt.getPlayer(), Sound.BLOCK_CHAIN_PLACE, SoundCategory.PLAYERS, (float)1.0, (float)1.0);
evt.getItem().remove();
}
}
}
}

View File

@ -0,0 +1,40 @@
package gg.malloc.coins;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.Material;
import de.tr7zw.nbtapi.NBTItem;
import de.tr7zw.nbtapi.NBTCompound;
public class CoinsCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
PlayerInventory inventory = player.getInventory();
ItemStack itemStack = new ItemStack(Material.IRON_NUGGET, 64);
ItemMeta coinMeta = itemStack.getItemMeta();
coinMeta.setCustomModelData(93197);
coinMeta.setDisplayName("Grist");
// TODO: Lore
itemStack.setItemMeta(coinMeta);
NBTItem nbt = new NBTItem(itemStack);
nbt.addCompound("malloc").setInteger("coinValue", 1);
inventory.addItem(nbt.getItem());
sender.sendMessage("You received 64 grist");
return true;
} else {
sender.sendMessage("You must be a player.");
return false;
}
}
}

View File

@ -0,0 +1,33 @@
package gg.malloc.coins;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.RegisteredServiceProvider;
public class Plugin extends JavaPlugin {
Economy m_vault;
@Override
public void onEnable() {
if (setupVault()) {
getLogger().info("Malloc Coins registered");
getServer().getPluginManager().registerEvents(new CoinPickupHandler(m_vault), this);
getCommand("coins").setExecutor(new CoinsCommand());
} else {
getLogger().info("Unable to register Malloc Coins: no vaul!");
}
}
private boolean setupVault() {
if (getServer().getPluginManager().getPlugin("Vault") == null) {
return false;
}
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
if (rsp == null) {
return false;
}
m_vault = rsp.getProvider();
return m_vault != null;
}
}

View File

@ -0,0 +1,10 @@
name: Malloc-Coins
version: 1.0
api-version: 1.18
main: gg.malloc.coins.Plugin
depend:
- NBTAPI
- Vault
commands:
coins:
description: Gives you 64 coins