Skip to content

Commit

Permalink
added minecart boosting
Browse files Browse the repository at this point in the history
  • Loading branch information
imperzer0 committed Feb 10, 2023
1 parent e37715f commit 052dc59
Show file tree
Hide file tree
Showing 5 changed files with 306 additions and 3 deletions.
26 changes: 25 additions & 1 deletion src/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,28 @@ crafts:
spawner: on
spawner-eggs: on

enchantments.max_level: 900

enchantments:
max_level: 900


# Momentum: How far a cart will travel (def: 32)
# Speed: How fast the cart will travel (def: 8)
# Derail: Allow carts to derail on slopes/corners (def: true)
# Rideable: Apply properties to passenger carts.
# Explosive: Apply properties to explosive (tnt) carts.
# Hopper: Apply properties to hopper carts.
# Powered: Apply properties to powered (furnace) carts.
# Storage: Apply properties to storage (chest) carts.

bc:
properties:
momentum: 128
speed: 16
derail: true
carts:
rideable: true
explosive: false
hopper: false
powered: false
storage: true
1 change: 1 addition & 0 deletions src/me/imperzer0/essentials/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public void onEnable()
/// Listeners
manager.registerEvents(new SkinListener(), this);
manager.registerEvents(new EnchantmentsListener(), this);
manager.registerEvents(new BoostMinecartListener(), this);
if (config.getBoolean("commands.owner-kit", false)) manager.registerEvents(new OwnerEnchantedKitListener(), this);
if (config.getBoolean("commands.bag", false)) manager.registerEvents(new BagListener(), this);
if (config.getBoolean("commands.ret", false)) manager.registerEvents(new RetListener(), this);
Expand Down
13 changes: 12 additions & 1 deletion src/me/imperzer0/essentials/commands/Main.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package me.imperzer0.essentials.commands;

import me.imperzer0.essentials.listeners.BoostMinecartListener;
import me.imperzer0.essentials.utils.CommandUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.plugin.PluginDescriptionFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -33,8 +35,17 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N

if (args.length == 1 && args[0].equalsIgnoreCase("reload"))
{
PluginDescriptionFile pdfFile = me.imperzer0.essentials.Main.getInstance().getDescription();
sender.sendMessage("Reloading " + pdfFile.getName() + " version " + pdfFile.getVersion() + " ...");

me.imperzer0.essentials.Main.getInstance().reloadConfig();
loger.message(sender, ChatColor.GREEN + "[imperzer0-essentials] Config reloaded.");

BoostMinecartListener.load_config();
loger.message(sender, "[BoostMinecart] Speed: " + BoostMinecartListener.get_speed());
loger.message(sender, "[BoostMinecart] Momentum: " + BoostMinecartListener.get_momentum());
loger.message(sender, "[BoostMinecart] Carts: " + BoostMinecartListener.get_carts());

loger.message(sender, ChatColor.GREEN + "[" + pdfFile.getName() + "] Config reloaded.");
}
else
{
Expand Down
267 changes: 267 additions & 0 deletions src/me/imperzer0/essentials/listeners/BoostMinecartListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
package me.imperzer0.essentials.listeners;

import me.imperzer0.essentials.Main;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Minecart;
import org.bukkit.entity.Vehicle;
import org.bukkit.entity.minecart.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.vehicle.VehicleDestroyEvent;
import org.bukkit.event.vehicle.VehicleMoveEvent;
import org.bukkit.material.Rails;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Objects;
import java.util.UUID;
import java.util.logging.Level;

public class BoostMinecartListener implements Listener
{
private static final ArrayList<Cart> CL = new ArrayList<>();
private static int speed;
private static int momentum;
private static boolean derail;
private static boolean rideable;
private static boolean explosive;
private static boolean hopper;
private static boolean powered;
private static boolean storage;
private static String carts;

public BoostMinecartListener()
{
load_config();
}

public static int get_speed()
{
return speed;
}

public static int get_momentum()
{
return momentum;
}

public static String get_carts()
{
return carts;
}

public static void load_config()
{
try
{
speed = Integer.parseInt(Objects.requireNonNull(Main.getInstance().getConfig().getString("bc.properties.speed")));
momentum = Integer.parseInt(Objects.requireNonNull(Main.getInstance().getConfig().getString("bc.properties.momentum")));
}
catch (NumberFormatException var2)
{
Main.getInstance().getLogger().log(Level.SEVERE, "Configuration contains INVALID Integer(s), using Defaults!");
speed = 8;
momentum = 32;
return;
}

derail = Main.getInstance().getConfig().getBoolean("bc.properties.derail");

carts = "";
if (Main.getInstance().getConfig().getBoolean("bc.carts.rideable"))
{
rideable = true;
carts = carts + "[Rideable]";
}
else
{
rideable = false;
}

if (Main.getInstance().getConfig().getBoolean("bc.carts.explosive"))
{
explosive = true;
carts = carts + "[Explosive]";
}
else
{
explosive = false;
}

if (Main.getInstance().getConfig().getBoolean("bc.carts.hopper"))
{
hopper = true;
carts = carts + "[Hopper]";
}
else
{
hopper = false;
}

if (Main.getInstance().getConfig().getBoolean("bc.carts.powered"))
{
powered = true;
carts = carts + "[Powered]";
}
else
{
powered = false;
}

if (Main.getInstance().getConfig().getBoolean("bc.carts.storage"))
{
storage = true;
carts = carts + "[Storage]";
}
else
{
storage = false;
}
}

@EventHandler(ignoreCancelled = true)
public void onVehicleMove(@NotNull VehicleMoveEvent event)
{
Vehicle vehicle = event.getVehicle();
if (!((vehicle instanceof Minecart cart) &&
(!(cart instanceof RideableMinecart) || rideable) &&
(!(cart instanceof PoweredMinecart) || powered) &&
(!(cart instanceof ExplosiveMinecart) || explosive) &&
(!(cart instanceof HopperMinecart) || hopper) &&
(!(cart instanceof StorageMinecart) || storage)))
return;

Block block = cart.getLocation().getBlock();
if (!(block.getState().getData() instanceof Rails rail))
return;

cart.setMaxSpeed(speed);

if (block.getType() == Material.POWERED_RAIL)
{
boolean powered = (block.getData() & 8) != 0;
Vector v;
if (powered)
{
v = cart.getVelocity();
v.normalize();
v.multiply((double) momentum / 100.0);
}
else
{
v = new Vector(0, 0, 0);
}

cart.setVelocity(v);
}
else if (block.getType() == Material.ACTIVATOR_RAIL)
{
cart.setMaxSpeed((double) speed / 20.0);
}
else if (block.getType() == Material.DETECTOR_RAIL)
{
cart.setMaxSpeed(0.4);
}

if (derail) return;

int cnt;
Cart CR;
if (!rail.isCurve() && !rail.isOnSlope())
{
cnt = 0;

while (true)
{
if (cnt >= CL.size())
{
CL.add(new Cart(cart.getUniqueId(), Math.max(Math.max(Math.abs(cart.getVelocity().getX()), Math.abs(cart.getVelocity().getY())), Math.abs(cart.getVelocity().getZ())), cart.getMaxSpeed(), rail));
break;
}

CR = CL.get(cnt);
if (cart.getUniqueId() == CR.cart && (CR.rail.isCurve() || CR.rail.isOnSlope()))
{
cart.setMaxSpeed(CR.maxspeed);
Vector v = cart.getVelocity();
v.normalize();
v.multiply(CR.vel);
cart.setVelocity(v);
CL.get(cnt).rail = rail;
return;
}

if (cart.getUniqueId() == CR.cart)
{
Location nb = cart.getLocation().add(cart.getVelocity().normalize());
if (nb.getBlock().getState().getData() instanceof Rails nrl)
{
if (nrl.isCurve() || nrl.isOnSlope())
{
cart.setMaxSpeed(0.4);
return;
}
}

CL.get(cnt).maxspeed = cart.getMaxSpeed();
CL.get(cnt).vel = Math.max(Math.max(Math.abs(cart.getVelocity().getX()), Math.abs(cart.getVelocity().getY())), Math.abs(cart.getVelocity().getZ()));
CL.get(cnt).rail = rail;
return;
}

++cnt;
}
}
else
{
cart.setMaxSpeed(0.4);

for (cnt = 0; cnt < CL.size(); ++cnt)
{
CR = CL.get(cnt);
if (cart.getUniqueId() == CR.cart)
{
CL.get(cnt).rail = rail;
}
}
}
}

@EventHandler(ignoreCancelled = true)
public void onVehicleDestory(@NotNull VehicleDestroyEvent v)
{
if (derail) return;

Vehicle vehicle = v.getVehicle();
if (!(vehicle instanceof Minecart cart)) return;

for (int cnt = 0; cnt < CL.size(); ++cnt)
{
Cart CR = CL.get(cnt);
if (cart.getUniqueId() == CR.cart)
{
CL.remove(cnt);
return;
}
}
}

public static final class Cart
{
public UUID cart;
public double vel;
public double maxspeed;
public Rails rail;

public Cart(UUID c, double v, double ms, Rails r)
{
this.cart = c;
this.vel = v;
this.maxspeed = ms;
this.rail = r;
}
}
}
2 changes: 1 addition & 1 deletion src/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
main: me.imperzer0.essentials.Main
name: imperzer0-essentials
version: 1.17.4
version: 1.18
author: imperzer0
api-version: 1.19
load: POSTWORLD
Expand Down

0 comments on commit 052dc59

Please sign in to comment.