forked from PaperMC/paperweight-test-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added working custom item manager, which replaces the crossbow regist…
…ry entry with a custom class by working in conjunction with the paper bootstrapper.
- Loading branch information
1 parent
792bfdd
commit 858df72
Showing
19 changed files
with
527 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/net/slqmy/firework_wars_plugin/FireworkWarsPluginBootstrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 0 additions & 82 deletions
82
src/main/java/net/slqmy/firework_wars_plugin/items/FireworkRifleItem.java
This file was deleted.
Oops, something went wrong.
109 changes: 109 additions & 0 deletions
109
src/main/java/net/slqmy/firework_wars_plugin/items/guns/BaseGunItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.