Skip to content

Commit

Permalink
Added working custom item manager, which replaces the crossbow regist…
Browse files Browse the repository at this point in the history
…ry entry with a custom class by working in conjunction with the paper bootstrapper.
  • Loading branch information
rolyPolyVole committed Aug 13, 2024
1 parent 792bfdd commit 858df72
Show file tree
Hide file tree
Showing 19 changed files with 527 additions and 236 deletions.
8 changes: 4 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import org.gradle.api.JavaVersion
import xyz.jpenilla.resourcefactory.bukkit.BukkitPluginYaml

plugins {
java
Expand All @@ -8,7 +7,7 @@ plugins {
`maven-publish`

id("io.papermc.paperweight.userdev") version "1.7.2"
id("xyz.jpenilla.resource-factory-bukkit-convention") version "1.1.1"
id("xyz.jpenilla.resource-factory-paper-convention") version "1.1.2"
id("xyz.jpenilla.run-paper") version "2.3.0"

id("io.github.goooler.shadow") version "8.1.7"
Expand Down Expand Up @@ -46,6 +45,7 @@ val projectAuthors = listOfNotNull(mainProjectAuthor, mainestProjectAuthor)

val topLevelDomain = "net"
val projectNameString = rootProject.name
val bootstrapperNameString = rootProject.name + "Bootstrapper"

group = topLevelDomain + groupStringSeparator + mainProjectAuthor.lowercase() + groupStringSeparator + snakeCase(projectNameString)
version = "1.0.0"
Expand Down Expand Up @@ -94,13 +94,13 @@ tasks {
}
}

bukkitPluginYaml {
paperPluginYaml {
authors = projectAuthors

main = projectGroupString + groupStringSeparator + pascalCase(projectNameString)
apiVersion = paperApiVersion

load = BukkitPluginYaml.PluginLoadOrder.STARTUP
bootstrapper = projectGroupString + groupStringSeparator + pascalCase(bootstrapperNameString)
}

publishing {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
package net.slqmy.firework_wars_plugin;

import net.slqmy.firework_wars_plugin.util.PersistentDataManager;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;

import dev.jorel.commandapi.CommandAPI;
import dev.jorel.commandapi.CommandAPIBukkitConfig;
import net.slqmy.firework_wars_plugin.arena.manager.ArenaManager;
Expand All @@ -18,19 +9,30 @@
import net.slqmy.firework_wars_plugin.game.GameManager;
import net.slqmy.firework_wars_plugin.items.manager.CustomItemManager;
import net.slqmy.firework_wars_plugin.language.LanguageManager;
import net.slqmy.firework_wars_plugin.util.PersistentDataManager;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.logging.Logger;

public final class FireworkWarsPlugin extends JavaPlugin implements Listener {
private static FireworkWarsPlugin instance;
public static Logger LOGGER;

private final CustomItemManager customItemManager;
private PlayerDataManager playerDataManager;
private LanguageManager languageManager;
private ArenaManager arenaManager;
private GameManager gameManager;
private CustomItemManager customItemManager;
private PersistentDataManager pdcManager;

public static FireworkWarsPlugin getInstance() {
return instance;
}

public PlayerDataManager getPlayerDataManager() {
return this.playerDataManager;
}
Expand All @@ -55,8 +57,11 @@ public PersistentDataManager getPdcManager() {
return this.pdcManager;
}

public FireworkWarsPlugin() {
public FireworkWarsPlugin(CustomItemManager customItemManager) {
instance = this;
LOGGER = getLogger();

this.customItemManager = customItemManager;
}

@Override
Expand All @@ -73,8 +78,10 @@ public void onEnable() {
this.languageManager = new LanguageManager(this);
this.arenaManager = new ArenaManager(this);
this.gameManager = new GameManager(this);
this.customItemManager = new CustomItemManager(this);
this.pdcManager = new PersistentDataManager(this);
this.pdcManager = new PersistentDataManager();

customItemManager.setPlugin(this);
customItemManager.registerCustomItems();

new SetLanguageCommand(this);
new ArenaCommand(this);
Expand All @@ -91,8 +98,8 @@ public void onDisable() {

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
ItemStack item1 = customItemManager.getItem("firework_rifle_ammo").getItem(event.getPlayer());
ItemStack item2 = customItemManager.getItem("firework_rifle").getItem(event.getPlayer());
ItemStack item1 = customItemManager.getItem("firework_shotgun_ammo").getItem(event.getPlayer());
ItemStack item2 = customItemManager.getItem("firework_shotgun").getItem(event.getPlayer());

event.getPlayer().getInventory().addItem(item1, item2);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.slqmy.firework_wars_plugin;

import io.papermc.paper.plugin.bootstrap.BootstrapContext;
import io.papermc.paper.plugin.bootstrap.PluginBootstrap;
import io.papermc.paper.plugin.bootstrap.PluginProviderContext;
import io.papermc.paper.registry.event.RegistryEvents;
import net.slqmy.firework_wars_plugin.items.manager.CustomItemManager;
import net.slqmy.firework_wars_plugin.util.ReflectUtil;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

@SuppressWarnings({"UnstableApiUsage", "unused"})
public final class FireworkWarsPluginBootstrapper implements PluginBootstrap {
private final ReflectUtil reflectUtil = new ReflectUtil();
private final CustomItemManager customItemManager = new CustomItemManager(reflectUtil);

@Override
public void bootstrap(@NotNull BootstrapContext context) {
reflectUtil.useLogger(context.getLogger());

context.getLifecycleManager().registerEventHandler(RegistryEvents.GAME_EVENT.freeze(), event -> {
customItemManager.registerNMSItems();
});
}

@Override
public @NotNull JavaPlugin createPlugin(@NotNull PluginProviderContext context) {
return new FireworkWarsPlugin(customItemManager);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package net.slqmy.firework_wars_plugin.data.player;

import com.google.gson.Gson;
import net.slqmy.firework_wars_plugin.FireworkWarsPlugin;
import org.bukkit.entity.Player;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
Expand All @@ -9,12 +13,6 @@
import java.util.Map.Entry;
import java.util.UUID;

import org.bukkit.entity.Player;

import com.google.gson.Gson;

import net.slqmy.firework_wars_plugin.FireworkWarsPlugin;

public class PlayerDataManager {

private final FireworkWarsPlugin plugin;
Expand Down Expand Up @@ -88,8 +86,8 @@ public void save() {

writer.write(json);

writer.close();
writer.flush();
writer.close();
} catch (IOException exception) {
exception.printStackTrace();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package net.slqmy.firework_wars_plugin.items.guns;

import io.papermc.paper.event.entity.EntityLoadCrossbowEvent;
import net.slqmy.firework_wars_plugin.FireworkWarsPlugin;
import net.slqmy.firework_wars_plugin.game.FireworkWarsGame;
import net.slqmy.firework_wars_plugin.items.manager.AbstractItem;
import net.slqmy.firework_wars_plugin.items.nms.CustomCrossbow;
import net.slqmy.firework_wars_plugin.util.ItemBuilder;
import net.slqmy.firework_wars_plugin.util.Keys;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.EntityShootBowEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.CrossbowMeta;
import org.bukkit.inventory.meta.FireworkMeta;

public abstract class BaseGunItem extends AbstractItem {
protected final String ammoId;

public BaseGunItem(FireworkWarsPlugin plugin, String itemId, String ammoId) {
super(plugin, itemId, Material.CROSSBOW);

this.ammoId = ammoId;
}

protected ItemBuilder<CrossbowMeta> getItemBuilder() {
return new ItemBuilder<CrossbowMeta>(plugin, itemMaterial)
.setEnchanted(true)
.setUnbreakable(true);
}

protected ItemStack getCustomCrossbow() {
CustomCrossbow crossbow = (CustomCrossbow) plugin.getCustomItemManager().getNMSItem("crossbow");
ItemStack itemStack = crossbow.getDefaultInstance().asBukkitCopy();

itemStack.setItemMeta(new ItemStack(Material.CROSSBOW).getItemMeta());
return itemStack;
}

protected void modifyMeta(CrossbowMeta meta) {
pdcManager.setStringValue(meta, isItemKey, itemId);
pdcManager.setStringValue(meta, Keys.GUN_ACCEPTED_AMMO_ID, ammoId);
}

protected ItemStack createFirework(Color color, int stars) {
ItemStack firework = new ItemStack(Material.FIREWORK_ROCKET);
FireworkMeta fireworkMeta = (FireworkMeta) firework.getItemMeta();

addFireworkStars(fireworkMeta, color, stars);
firework.setItemMeta(fireworkMeta);

return firework;
}

protected void addFireworkStars(FireworkMeta meta, Color color, int amount) {
for (int i = 0; i < amount; i++) {
meta.addEffect(FireworkEffect
.builder()
.withColor(Color.WHITE)
.withFade(color)
.withFlicker()
.build());
}
}

protected abstract void onCrossbowLoad(Player player, FireworkWarsGame game, EntityLoadCrossbowEvent event);
protected abstract void onCrossbowShoot(Player player, FireworkWarsGame game, EntityShootBowEvent event);

@EventHandler
public void onCrossbowLoad(EntityLoadCrossbowEvent event) {
if (!isValidCustomItem(event.getCrossbow())) {
return;
}

if (!(event.getEntity() instanceof Player player)) {
return;
}

FireworkWarsGame game = plugin.getGameManager().getFireworkWarsGame(player);

if (game == null) {
return;
}

onCrossbowLoad(player, game, event);
}

@EventHandler
public void onCrossbowShoot(EntityShootBowEvent event) {
if (!isValidCustomItem(event.getBow())) {
return;
}

if (!(event.getEntity() instanceof Player player)) {
return;
}

FireworkWarsGame game = plugin.getGameManager().getFireworkWarsGame(player);

if (game == null) {
return;
}

onCrossbowShoot(player, game, event);
}
}
Loading

0 comments on commit 858df72

Please sign in to comment.