Skip to content

Commit

Permalink
Hotfix physics manager
Browse files Browse the repository at this point in the history
  • Loading branch information
sub-kek committed Oct 22, 2024
1 parent 8c0f71d commit a4de4c2
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 33 deletions.
2 changes: 2 additions & 0 deletions src/main/java/io/github/subkek/customdiscs/CustomDiscs.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public void onPacketSending(PacketEvent event) {
if (LegacyUtil.isCustomDisc(jukebox.getRecord()) ||
LegacyUtil.isCustomYouTubeDisc(jukebox.getRecord())) {
event.setCancelled(true);

PhysicsManager.getInstance().start(jukebox);
}
}
}
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/io/github/subkek/customdiscs/PhysicsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public class PhysicsManager {
private final CustomDiscs plugin = CustomDiscs.getPlugin();
private final Map<UUID, ParticleJukebox> jukeboxMap = new HashMap<>();
private final Map<UUID, PhysicsJukebox> jukeboxMap = new HashMap<>();

private static PhysicsManager instance;

Expand All @@ -22,25 +22,25 @@ public static PhysicsManager getInstance() {
return instance;
}

public ParticleJukebox get(Block block) {
public PhysicsJukebox get(Block block) {
UUID uuid = UUID.nameUUIDFromBytes(block.getLocation().toString().getBytes());
ParticleJukebox particleJukebox = jukeboxMap.get(uuid);
if (particleJukebox == null)
throw new IllegalStateException("This ParticleJukebox doesn't exists cannot get");
return particleJukebox;
PhysicsJukebox physicsJukebox = jukeboxMap.get(uuid);
if (physicsJukebox == null)
throw new IllegalStateException("This PhysicsJukebox doesn't exists cannot get");
return physicsJukebox;
}

public record NeedUpdate(boolean returnForced, boolean value) {
}

public NeedUpdate isNeedUpdate(Block block) {
UUID uuid = UUID.nameUUIDFromBytes(block.getLocation().toString().getBytes());
ParticleJukebox particleJukebox = jukeboxMap.get(uuid);
if (particleJukebox == null) {
PhysicsJukebox physicsJukebox = jukeboxMap.get(uuid);
if (physicsJukebox == null) {
CustomDiscs.debug("PhysicsManager return needUpdate false because ParticleJukebox is null");
return new NeedUpdate(true, false);
}
return new NeedUpdate(false, particleJukebox.isNeedUpdate());
return new NeedUpdate(false, physicsJukebox.isNeedUpdate());
}

private void discToHopper(Block block) {
Expand All @@ -59,8 +59,8 @@ private void discToHopper(Block block) {
private synchronized void stop(Block block) {
UUID uuid = UUID.nameUUIDFromBytes(block.getLocation().toString().getBytes());
if (jukeboxMap.containsKey(uuid)) {
ParticleJukebox particleJukebox = jukeboxMap.remove(uuid);
particleJukebox.task.cancel();
PhysicsJukebox physicsJukebox = jukeboxMap.remove(uuid);
physicsJukebox.task.cancel();

if (!block.getType().equals(Material.JUKEBOX)) return;

Expand All @@ -74,11 +74,11 @@ private synchronized void stop(Block block) {
public void start(Jukebox jukebox) {
UUID uuid = UUID.nameUUIDFromBytes(jukebox.getLocation().toString().getBytes());
if (jukeboxMap.containsKey(uuid)) return;
ParticleJukebox particleJukebox = new ParticleJukebox();
jukeboxMap.put(uuid, particleJukebox);
PhysicsJukebox physicsJukebox = new PhysicsJukebox();
jukeboxMap.put(uuid, physicsJukebox);

plugin.getFoliaLib().getScheduler().runAtLocationTimer(jukebox.getLocation(), task -> {
particleJukebox.setTask(task);
physicsJukebox.setTask(task);
if (task.isCancelled()) return;

if (!LavaPlayerManager.getInstance().isPlaying(jukebox.getBlock()) &&
Expand All @@ -94,12 +94,12 @@ public void start(Jukebox jukebox) {
jukebox.update();
}

particleJukebox.lastUpdateTick = -1; // Может быть... есть вариант получше? Я серьезно!
physicsJukebox.lastUpdateTick = -1; // Может быть... есть вариант получше? Я серьезно!
}, 1, 20);
}

@Data
public static final class ParticleJukebox {
public static final class PhysicsJukebox {
private boolean needUpdate = true;
private boolean updatedFirst = false;
private WrappedTask task;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static HopperHandler getInstance() {
return instance;
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onJukeboxInsertFromHopper(InventoryMoveItemEvent event) {
if (event.getDestination().getLocation() == null) return;
Block block = event.getDestination().getLocation().getBlock();
Expand All @@ -47,7 +47,7 @@ public void onJukeboxInsertFromHopper(InventoryMoveItemEvent event) {
PlayUtil.playLava(block, event.getItem());
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onJukeboxEjectToHopper(InventoryMoveItemEvent event) {
if (event.getSource().getLocation() == null) return;
Block block = event.getSource().getLocation().getBlock();
Expand All @@ -72,17 +72,17 @@ public void onPhysics(BlockPhysicsEvent event) {
PhysicsManager.NeedUpdate needUpdate = PhysicsManager.getInstance().isNeedUpdate(block);

boolean reallyNeed = false;
PhysicsManager.ParticleJukebox particleJukebox = null;
PhysicsManager.PhysicsJukebox physicsJukebox = null;
long time = block.getWorld().getTime();
if (!needUpdate.returnForced()) {
particleJukebox = PhysicsManager.getInstance().get(block);
reallyNeed = particleJukebox.getLastUpdateTick() == time;
physicsJukebox = PhysicsManager.getInstance().get(block);
reallyNeed = physicsJukebox.getLastUpdateTick() == time;
}

if (needUpdate.value() || reallyNeed) {
assert particleJukebox != null;
particleJukebox.setNeedUpdate(false);
particleJukebox.setLastUpdateTick(time);
assert physicsJukebox != null;
physicsJukebox.setNeedUpdate(false);
physicsJukebox.setLastUpdateTick(time);
CustomDiscs.debug("Updating physics on {0} by jukebox", event.getBlock().getType());
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private static ItemStack getItemStack(PlayerInteractEvent event, Player player)
return itemInvolvedInEvent;
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInsert(PlayerInteractEvent event) {
Block block = event.getClickedBlock();

Expand All @@ -63,7 +63,7 @@ public void onInsert(PlayerInteractEvent event) {
PlayUtil.playLava(block, event.getItem());
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEject(PlayerInteractEvent event) {
Player player = event.getPlayer();
Block block = event.getClickedBlock();
Expand All @@ -84,7 +84,7 @@ public void onEject(PlayerInteractEvent event) {
LavaPlayerManager.getInstance().stopPlaying(block);
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onJukeboxBreak(BlockBreakEvent event) {

Block block = event.getBlock();
Expand All @@ -95,7 +95,7 @@ public void onJukeboxBreak(BlockBreakEvent event) {
LavaPlayerManager.getInstance().stopPlaying(block);
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onJukeboxExplode(EntityExplodeEvent event) {
for (Block explodedBlock : event.blockList()) {
if (explodedBlock.getType() == Material.JUKEBOX) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static PlayerHandler getInstance() {
return instance;
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onClickJukebox(PlayerInteractEvent event) {
UUID playerUUID = event.getPlayer().getUniqueId();
if (!playersSelecting.containsKey(playerUUID)) return;
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/io/github/subkek/customdiscs/util/PlayUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public class PlayUtil {
public static void playStandard(Block block, ItemStack disc) {
plugin.discsPlayed++;

PhysicsManager.getInstance().start((Jukebox) block.getState());

ItemMeta discMeta = LegacyUtil.getItemMeta(disc);

String soundFileName = discMeta.getPersistentDataContainer()
Expand All @@ -38,8 +36,6 @@ public static void playStandard(Block block, ItemStack disc) {
public static void playLava(Block block, ItemStack disc) {
plugin.discsPlayed++;

PhysicsManager.getInstance().start((Jukebox) block.getState());

ItemMeta discMeta = LegacyUtil.getItemMeta(disc);

String soundLink = discMeta.getPersistentDataContainer()
Expand Down

0 comments on commit a4de4c2

Please sign in to comment.