Skip to content

Commit

Permalink
Made Laser Functional
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadk953 committed Feb 25, 2024
1 parent 93539e3 commit 1ace52d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/poixpixelcustom/PoixpixelCustom.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.poixpixelcustom.commands.*;
import com.poixpixelcustom.listeners.EntityListener;
import com.poixpixelcustom.listeners.GuiListener;
import com.poixpixelcustom.listeners.LaserPointerListener;
import com.poixpixelcustom.tasks.Board;
import com.poixpixelcustom.tasks.ButterflyTask;
import com.poixpixelcustom.tasks.LaserPointerTask;
import com.poixpixelcustom.utils.ConfigHandler;
import com.poixpixelcustom.utils.CustomRecipes;

Expand All @@ -26,8 +28,10 @@ public class PoixpixelCustom extends JavaPlugin {
private static Chat chat = null;

private static final Logger log = Logger.getLogger("Minecraft");

private BukkitTask butterflyTask;
private BukkitTask scoreboardTask;
private BukkitTask laserPointerTask;

/**
* Called when the plugin is enabled
Expand Down Expand Up @@ -78,6 +82,7 @@ private void setup() {
*/
getServer().getPluginManager().registerEvents(new EntityListener(), this);
getServer().getPluginManager().registerEvents(new GuiListener(), this);
getServer().getPluginManager().registerEvents(new LaserPointerListener(), this);

/*
* Register Commands
Expand All @@ -100,6 +105,7 @@ private void setup() {
*/
butterflyTask = getServer().getScheduler().runTaskTimer(this, ButterflyTask.getInstance(), 0, 1);
scoreboardTask = getServer().getScheduler().runTaskTimer(this, Board.getInstance(), 0, 20);
laserPointerTask = getServer().getScheduler().runTaskTimer(this, LaserPointerTask.getInstance(), 0, 1);

/*
* Register Custom Recipes
Expand All @@ -117,6 +123,9 @@ private void disablePlugin() {
if (scoreboardTask != null && !scoreboardTask.isCancelled()) {
scoreboardTask.cancel();
}
if (laserPointerTask != null && !laserPointerTask.isCancelled()) {
laserPointerTask.cancel();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.poixpixelcustom.listeners;

import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.RayTraceResult;

public final class LaserPointerListener implements Listener {

@EventHandler
public void onClick(final PlayerInteractEvent event) {
if (event.getHand() != EquipmentSlot.HAND || event.getAction() != Action.RIGHT_CLICK_AIR)
return;


Player player = event.getPlayer();
ItemStack hand = player.getItemInHand();
int distance = 100;

if (!player.hasPermission("poixpixelcustom.laser.use")) {
player.sendMessage(ChatColor.LIGHT_PURPLE + "[Laser]" + ChatColor.WHITE + " You do not have permission to use the laser!");
return;
}

if (hand.hasItemMeta() && hand.getItemMeta().getDisplayName().equals(ChatColor.WHITE + "Laser Pointer")) {
RayTraceResult result = player.rayTraceBlocks(distance);

if (result != null && result.getHitBlock() != null && result.getHitBlock().isSolid())
player.getWorld().createExplosion(result.getHitBlock().getLocation(), 5F, true);
else
player.sendMessage(ChatColor.LIGHT_PURPLE + "[Laser]" + ChatColor.WHITE + " Target is too far or not a solid block!");
}
}
}
42 changes: 42 additions & 0 deletions src/main/java/com/poixpixelcustom/tasks/LaserPointerTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.poixpixelcustom.tasks;

import org.bukkit.*;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;

public final class LaserPointerTask implements Runnable {

private static final LaserPointerTask instance = new LaserPointerTask();

private LaserPointerTask() {
}

@Override
public void run() {
int length = 5;
double particleDistance = 0.5;

for (Player online : Bukkit.getOnlinePlayers()) {
ItemStack hand = online.getItemInHand();

if (hand.hasItemMeta() && hand.getItemMeta().getDisplayName().equals(ChatColor.WHITE + "Laser Pointer")) {
Location location = online.getLocation().add(0, 1, 0);

for (double waypoint = 1; waypoint < length; waypoint += particleDistance) {
Vector vector = location.getDirection().multiply(waypoint);
location.add(vector);

if (location.getBlock().getType() != Material.AIR)
break;

location.getWorld().spawnParticle(Particle.REDSTONE, location, 1, new Particle.DustOptions(Color.YELLOW, 0.75F));
}
}
}
}

public static LaserPointerTask getInstance() {
return instance;
}
}
2 changes: 2 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ permissions:
default: true
poixpixelcustom.explodingentity.use:
default: true
poixpixelcustom.laser.use:
default: false

# Bstats
bStats:
Expand Down

0 comments on commit 1ace52d

Please sign in to comment.