added tazer

This commit is contained in:
ImproperIssues
2023-07-06 13:38:30 -07:00
parent 6568427499
commit 314655ca76
9 changed files with 181 additions and 5 deletions

View File

@@ -4,6 +4,7 @@ import fun.ogre.ogredupealias.commands.commands.*;
import fun.ogre.ogredupealias.data.Config; import fun.ogre.ogredupealias.data.Config;
import fun.ogre.ogredupealias.events.*; import fun.ogre.ogredupealias.events.*;
import fun.ogre.ogredupealias.plugin.custom.forging.CraftingKeys; import fun.ogre.ogredupealias.plugin.custom.forging.CraftingKeys;
import fun.ogre.ogredupealias.plugin.custom.items.CustomItems;
import fun.ogre.ogredupealias.plugin.funitems.AdminUtility; import fun.ogre.ogredupealias.plugin.funitems.AdminUtility;
import fun.ogre.ogredupealias.plugin.funitems.Pickler; import fun.ogre.ogredupealias.plugin.funitems.Pickler;
import fun.ogre.ogredupealias.plugin.funitems.PotatoCannon; import fun.ogre.ogredupealias.plugin.funitems.PotatoCannon;
@@ -27,6 +28,7 @@ public final class OgreDupeAlias extends JavaPlugin {
instance = this; instance = this;
this.init(); this.init();
this.initConfig(); this.initConfig();
CustomItems.init();
CraftingKeys.initRecipes(); CraftingKeys.initRecipes();
} }
@@ -51,6 +53,8 @@ public final class OgreDupeAlias extends JavaPlugin {
pm.registerEvents(new AdminUtility(), this); pm.registerEvents(new AdminUtility(), this);
pm.registerEvents(new Pickler(), this); pm.registerEvents(new Pickler(), this);
pm.registerEvents(new SPBEventListener(), this); pm.registerEvents(new SPBEventListener(), this);
// event for custom items
pm.registerEvents(new CustomItems(), this);
// Commands // Commands
getCommand("forcefield").setExecutor(new ForceFieldCommand()); getCommand("forcefield").setExecutor(new ForceFieldCommand());

View File

@@ -64,6 +64,10 @@ public class GiveCustomCommand implements TabExecutor {
p.getInventory().addItem(ItemPresets.PICKLER); p.getInventory().addItem(ItemPresets.PICKLER);
p.sendMessage(Text.ofAll("&bGiven you a Pickler")); p.sendMessage(Text.ofAll("&bGiven you a Pickler"));
} }
case "Tazer" -> {
p.getInventory().addItem(ItemPresets.TAZER);
p.sendMessage(Text.ofAll("&bGiven you a Tazer"));
}
} }
} }
catch (Exception ex) { catch (Exception ex) {
@@ -86,7 +90,8 @@ public class GiveCustomCommand implements TabExecutor {
"VoidCharm", "VoidCharm",
"Spleefer", "Spleefer",
"PotatoCannon", "PotatoCannon",
"LaserPointer" "LaserPointer",
"Tazer"
}) })
.build(); .build();
} }

View File

@@ -153,4 +153,14 @@ public abstract class ItemPresets {
.lore(Text.color("&2▪ &aRight-Click:&7 Pickle-ify Someone")) .lore(Text.color("&2▪ &aRight-Click:&7 Pickle-ify Someone"))
.customModelData(1111) .customModelData(1111)
.build(); .build();
public static ItemStack TAZER = ItemBuilder.create()
.material(Material.NETHERITE_AXE)
.name(Text.color("&7{&b&lTazer&7}"))
.unbreakable(true)
.enchant(Enchantment.LURE, 1)
.flag(ItemFlag.HIDE_ENCHANTS, ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_UNBREAKABLE)
.customModelData(1111)
.build();
} }

View File

@@ -0,0 +1,27 @@
package fun.ogre.ogredupealias.plugin.custom.items;
import org.bukkit.inventory.ItemStack;
public abstract class CustomItem {
private final ItemStack item;
public CustomItem(ItemStack item) {
this.item = item;
}
public abstract CustomItemInteractionCallback getCallback();
public void registerThis() {
ItemStack item = getItem();
CustomItemInteractionCallback callback = getCallback();
if (item != null && callback != null) {
CustomItems.register(item, callback);
}
}
public ItemStack getItem() {
return item;
}
}

View File

@@ -0,0 +1,11 @@
package fun.ogre.ogredupealias.plugin.custom.items;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
@FunctionalInterface
public interface CustomItemInteractionCallback {
void handleInteraction(Player player, ItemStack item, PlayerInteractEvent event);
}

View File

@@ -0,0 +1,42 @@
package fun.ogre.ogredupealias.plugin.custom.items;
import fun.ogre.ogredupealias.plugin.custom.items.customitems.TazerItem;
import fun.ogre.ogredupealias.utils.ItemUtils;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
import java.util.Map;
public final class CustomItems implements Listener {
private static final Map<String, CustomItemInteractionCallback> callbackList = new HashMap<>();
public static void init() {
register(new TazerItem());
}
public static ItemStack register(ItemStack item, CustomItemInteractionCallback interactionCallback) {
callbackList.put(ItemUtils.nbtOf(item), interactionCallback);
return item;
}
public static ItemStack register(CustomItem item) {
item.registerThis();
return register(item.getItem(), item.getCallback());
}
public static void onInteract(ItemStack item, PlayerInteractEvent event) {
String nbt = ItemUtils.nbtOf(item);
if (callbackList.containsKey(nbt)) {
callbackList.get(nbt).handleInteraction(event.getPlayer(), item, event);
}
}
@EventHandler
private void onPlayerInteraction(PlayerInteractEvent e) {
onInteract(e.getItem(), e);
}
}

View File

@@ -0,0 +1,70 @@
package fun.ogre.ogredupealias.plugin.custom.items.customitems;
import fun.ogre.ogredupealias.plugin.ItemPresets;
import fun.ogre.ogredupealias.plugin.custom.items.CustomItem;
import fun.ogre.ogredupealias.plugin.custom.items.CustomItemInteractionCallback;
import fun.ogre.ogredupealias.utils.Randomizer;
import fun.ogre.ogredupealias.utils.RaycastUtils;
import fun.ogre.ogredupealias.utils.SoundPlayer;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.Sound;
import org.bukkit.entity.LivingEntity;
import org.bukkit.util.Vector;
import java.util.function.Predicate;
public class TazerItem extends CustomItem {
public TazerItem() {
super(ItemPresets.TAZER);
}
@Override
public CustomItemInteractionCallback getCallback() {
return (player, item, event) -> {
if (!event.getAction().name().contains("RIGHT_CLICK")) return;
Location loc = player.getEyeLocation();
Vector vec = player.getLocation().getDirection().normalize();
Location target = RaycastUtils.raycast(loc, vec, 64.0, point -> {
boolean hitBlock = !point.getBlock().isPassable();
boolean hitEntity = point.getWorld().getNearbyEntities(point, 2, 2, 2).stream().anyMatch(e -> e != player && !e.isDead() && e instanceof LivingEntity);
return hitBlock || hitEntity;
});
int maxSections = 10;
int delta = 5;
double maxDist = loc.distance(target);
double sectionDist = maxDist / maxSections;
Location prevLoc = loc.clone();
Predicate<Location> hitCondition = (point) -> {
Particle.DustOptions dust = new Particle.DustOptions(Color.AQUA, 0.7F);
point.getWorld().spawnParticle(Particle.REDSTONE, point, 1, 0, 0, 0, 0, dust);
point.getWorld().getNearbyEntities(point, 2, 2, 2).stream()
.filter(e -> e != player && !e.isDead() && e instanceof LivingEntity)
.forEach(entity -> {
((LivingEntity) entity).damage(6, player);
entity.setFireTicks(100);
SoundPlayer zap = new SoundPlayer(entity.getLocation(), Sound.BLOCK_LAVA_EXTINGUISH, 1, 1.5F);
zap.playWithin(20);
});
return false;
};
for (int i = 0; i < maxSections - 1; i ++) {
prevLoc = RaycastUtils.raycast(prevLoc, vec, sectionDist, 0.2, hitCondition);
vec = randomizeVector(vec, delta);
SoundPlayer zap = new SoundPlayer(prevLoc, Sound.ENTITY_BEE_HURT, 1, 10);
zap.playWithin(20);
}
RaycastUtils.raycast(prevLoc, target, 0.2, hitCondition);
};
}
private Vector randomizeVector(Vector vec, int delta) {
return vec.clone().add(new Vector(Randomizer.rand(-delta, delta), Randomizer.rand(-delta, delta), Randomizer.rand(-delta, delta))).normalize();
}
}

View File

@@ -50,8 +50,7 @@ public class Randomizer<T> {
* @return random * @return random
*/ */
public static int rand(int max) { public static int rand(int max) {
if (max <= 0) throw new IllegalArgumentException("max cannot be less than 1!"); return (int)(Math.random() * max);
return (int) Math.ceil(Math.random() * max);
} }
/** /**
@@ -61,8 +60,7 @@ public class Randomizer<T> {
* @return random * @return random
*/ */
public static int rand(int min, int max) { public static int rand(int min, int max) {
if (max <= 0 || min <= 0) throw new IllegalArgumentException("max or min cannot be less than 1!");
if (max <= min) throw new IllegalArgumentException("max cannot be less than or equal to min!"); if (max <= min) throw new IllegalArgumentException("max cannot be less than or equal to min!");
return min + (int) Math.floor(Math.random() * (max - min + 1)); return min + (int)(Math.random() * (max - min + 1));
} }
} }

View File

@@ -47,6 +47,15 @@ public class Text {
return builder(""); return builder("");
} }
public static String removeColors(String msg) {
String s = msg;
while (s.length() >= 2 && s.contains("§")) {
int index = s.indexOf("§");
s = s.replaceAll(s.substring(index, index + 2), "");
}
return s;
}
public static class TextBuilder { public static class TextBuilder {
private String s; private String s;