Skip to content

Commit

Permalink
Fixed bugs related to discs not going to into hoppers. Particles now …
Browse files Browse the repository at this point in the history
…show for the entire duration of a custom song.
  • Loading branch information
Navoei committed Apr 2, 2023
1 parent b48f14b commit a70445f
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ mod_id=customdiscsplugin
# Target an older API to make it compatible with older versions of Simple Voice Chat
voicechat_api_version=2.3.3

plugin_version=2.3
plugin_version=2.3.1
maven_group=me.Navoei.customdiscsplugin
archives_base_name=custom-discs
15 changes: 15 additions & 0 deletions src/main/java/me/Navoei/customdiscsplugin/CustomDiscs.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,26 @@
import me.Navoei.customdiscsplugin.event.JukeBox;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Hopper;
import org.bukkit.block.Jukebox;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;

import javax.annotation.Nullable;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Objects;

import static me.Navoei.customdiscsplugin.PlayerManager.getLengthSeconds;

public final class CustomDiscs extends JavaPlugin {

public static final String PLUGIN_ID = "CustomDiscs";
Expand Down Expand Up @@ -75,9 +86,12 @@ public void onPacketSending(PacketEvent event) {
if (!jukebox.getRecord().hasItemMeta()) return;

if (jukebox.getRecord().getItemMeta().getPersistentDataContainer().has(new NamespacedKey(CustomDiscs.getInstance(), "customdisc"), PersistentDataType.STRING)) {
jukebox.stopPlaying();
event.setCancelled(true);
}

//Spawn particles if there isnt any music playing at this location.
ParticleManager.start(jukebox);
}
}
});
Expand All @@ -95,4 +109,5 @@ public void onDisable() {
public static CustomDiscs getInstance() {
return instance;
}

}
46 changes: 46 additions & 0 deletions src/main/java/me/Navoei/customdiscsplugin/HopperManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.Jukebox;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;

import java.nio.file.Path;
import java.util.Arrays;
import java.util.Objects;

public class HopperManager implements Listener {
Expand Down Expand Up @@ -51,9 +58,48 @@ public void onJukeboxEjectToHopper(InventoryMoveItemEvent event) {

if (event.getSource().getLocation() == null) return;
if (!event.getSource().getType().equals(InventoryType.JUKEBOX)) return;
if (event.getItem().getItemMeta() == null) return;
if (!isCustomMusicDisc(event.getItem())) return;

event.setCancelled(playerManager.isAudioPlayerPlaying(event.getSource().getLocation()));

}

public void discToHopper(Block block) {

if (block == null) return;
if (!block.getLocation().getChunk().isLoaded()) return;
if (!block.getType().equals(Material.JUKEBOX)) return;
if (!block.getRelative(BlockFace.DOWN).getType().equals(Material.HOPPER)) return;

Block hopperBlock = block.getRelative(BlockFace.DOWN);
org.bukkit.block.Hopper hopper = (org.bukkit.block.Hopper) hopperBlock.getState();

Jukebox jukebox = (Jukebox) block.getState();

InventoryMoveItemEvent event = new InventoryMoveItemEvent(jukebox.getInventory(), jukebox.getRecord(), hopper.getInventory(), false);
Bukkit.getPluginManager().callEvent(event);

if (!event.isCancelled()) {
if (!Arrays.toString(hopper.getInventory().getContents()).contains("null")) return;

hopper.getInventory().setItem(hopper.getInventory().firstEmpty(), jukebox.getRecord());

block.setType(Material.AIR);
block.setType(Material.JUKEBOX);
}

}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onChunkLoad(ChunkLoadEvent event) {
for (BlockState blockState : event.getChunk().getTileEntities()) {
if (blockState instanceof Jukebox jukebox) {
if (!PlayerManager.instance().isAudioPlayerPlaying(blockState.getLocation()) && !jukebox.isPlaying()) {
discToHopper(blockState.getBlock());
}
}
}
}

private boolean isCustomMusicDisc (ItemStack item) {
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/me/Navoei/customdiscsplugin/ParticleManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package me.Navoei.customdiscsplugin;

import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.block.Jukebox;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.HashMap;

public class ParticleManager extends BukkitRunnable {

static PlayerManager playerManager = PlayerManager.instance();
static HashMap<Location, ParticleManager> locationParticleManager = new HashMap<>();

public static void start(Jukebox jukebox) {
ParticleManager particleManager = new ParticleManager();
particleManager.jukebox = jukebox;
if (locationParticleManager.containsKey(jukebox.getLocation())) return;
locationParticleManager.put(jukebox.getLocation(), particleManager);
locationParticleManager.get(jukebox.getLocation()).runTaskTimer(CustomDiscs.getInstance(), 0, 20);
}

//private float seconds;
private Jukebox jukebox;


@Override
public void run() {

if (!playerManager.isAudioPlayerPlaying(jukebox.getLocation())) {
locationParticleManager.remove(jukebox.getLocation());
cancel();
} else {
//if (!jukebox.isPlaying()) {
jukebox.getLocation().getWorld().spawnParticle(Particle.NOTE, jukebox.getLocation().add(0.5, 1.1, 0.5), 1);
//}
}
}

}
3 changes: 3 additions & 0 deletions src/main/java/me/Navoei/customdiscsplugin/PlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public void playLocationalAudio(VoicechatServerApi api, Path soundFilePath, Bloc
}

audioPlayer.setOnStopped(() -> {

Bukkit.getScheduler().runTask(CustomDiscs.getInstance(), () -> HopperManager.instance().discToHopper(block));

playerMap.remove(id);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.Navoei.customdiscsplugin.event;

import me.Navoei.customdiscsplugin.CustomDiscs;
import me.Navoei.customdiscsplugin.ParticleManager;
import me.Navoei.customdiscsplugin.PlayerManager;
import me.Navoei.customdiscsplugin.VoicePlugin;
import net.kyori.adventure.text.Component;
Expand Down

0 comments on commit a70445f

Please sign in to comment.