Skip to content

Commit efcd93e

Browse files
Merge pull request #9 from ParallelMC/bits-and-bobs
Bits and bobs - Door Knocking, Speedy Minecarts, and Safeguarding Trophies/Cosmetics
2 parents 119c65a + 85d1132 commit efcd93e

File tree

7 files changed

+260
-2
lines changed

7 files changed

+260
-2
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>parallelmc</groupId>
88
<artifactId>parallelutils</artifactId>
9-
<version>1.8.1</version>
9+
<version>1.9.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Parallelutils</name>

src/main/java/parallelmc/parallelutils/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class Constants {
44

5-
public static final Version VERSION = new Version(1, 8, 1);
5+
public static final Version VERSION = new Version(1, 9, 0);
66
public static final String PLUGIN_NAME = "ParallelUtils";
77
public static final String DEFAULT_WORLD = "world";
88
}

src/main/java/parallelmc/parallelutils/Parallelutils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import parallelmc.parallelutils.commands.ParallelCommand;
1010
import parallelmc.parallelutils.commands.ParallelHelpCommand;
1111
import parallelmc.parallelutils.commands.ParallelTestCommand;
12+
import parallelmc.parallelutils.modules.bitsandbobs.BitsAndBobs;
1213
import parallelmc.parallelutils.modules.custommobs.CustomMobs;
1314
import parallelmc.parallelutils.modules.customtrees.ParallelTrees;
1415
import parallelmc.parallelutils.modules.discordintegration.DiscordIntegration;
@@ -242,6 +243,14 @@ public void onEnable() {
242243
e.printStackTrace();
243244
}
244245

246+
try {
247+
BitsAndBobs bitsandbobs = new BitsAndBobs();
248+
bitsandbobs.onEnable();
249+
} catch (Exception e) {
250+
Parallelutils.log(Level.SEVERE, "Error while enabling module BitsAndBobs!");
251+
e.printStackTrace();
252+
}
253+
245254
// TODO: Make this not horrible
246255
try {
247256
ParallelModule flags = getModule("ParallelFlags");
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package parallelmc.parallelutils.modules.bitsandbobs;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.plugin.Plugin;
5+
import org.bukkit.plugin.PluginManager;
6+
import parallelmc.parallelutils.Constants;
7+
import parallelmc.parallelutils.ParallelModule;
8+
import parallelmc.parallelutils.Parallelutils;
9+
import parallelmc.parallelutils.modules.bitsandbobs.minimodules.DoorKnocker;
10+
import parallelmc.parallelutils.modules.bitsandbobs.minimodules.KeepSpecialItems;
11+
import parallelmc.parallelutils.modules.bitsandbobs.minimodules.SpeedyMinecarts;
12+
13+
import java.util.logging.Level;
14+
15+
public class BitsAndBobs implements ParallelModule {
16+
@Override
17+
public void onEnable() {
18+
PluginManager manager = Bukkit.getPluginManager();
19+
Plugin plugin = manager.getPlugin(Constants.PLUGIN_NAME);
20+
21+
if (plugin == null) {
22+
Parallelutils.log(Level.SEVERE, "Unable to enable BitsAndBobs. Plugin " + Constants.PLUGIN_NAME + " does not exist!");
23+
return;
24+
}
25+
26+
Parallelutils puPlugin = (Parallelutils) plugin;
27+
28+
if (!puPlugin.registerModule("BitsAndBobs", this)) {
29+
Parallelutils.log(Level.SEVERE, "Unable to register module BitsAndBobs! Module may already be registered. Quitting...");
30+
return;
31+
}
32+
33+
//manager.registerEvents(new DoorKnocker(), plugin);
34+
manager.registerEvents(new KeepSpecialItems(), plugin);
35+
//manager.registerEvents(new SpeedyMinecarts(), plugin);
36+
}
37+
38+
@Override
39+
public void onDisable() {
40+
41+
}
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package parallelmc.parallelutils.modules.bitsandbobs.minimodules;
2+
3+
import org.bukkit.GameMode;
4+
import org.bukkit.Location;
5+
import org.bukkit.Material;
6+
import org.bukkit.Sound;
7+
import org.bukkit.block.Block;
8+
import org.bukkit.entity.Player;
9+
import org.bukkit.event.EventHandler;
10+
import org.bukkit.event.Listener;
11+
import org.bukkit.event.block.Action;
12+
import org.bukkit.event.player.PlayerInteractEvent;
13+
14+
public class DoorKnocker implements Listener {
15+
16+
@EventHandler
17+
public void onDoorKnock(PlayerInteractEvent event) {
18+
Player player = event.getPlayer();
19+
if (player.getInventory().getItemInMainHand().getType() == Material.AIR) {
20+
if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
21+
if (!player.getGameMode().equals(GameMode.CREATIVE)) {
22+
Block block = event.getClickedBlock();
23+
24+
if (block == null) return;
25+
26+
// case for iron door
27+
if (block.getType().name().contains("IRON_DOOR")) {
28+
Location location = block.getLocation();
29+
location.getWorld().playSound(location, Sound.ENTITY_ZOMBIE_ATTACK_IRON_DOOR, 1, 1);
30+
// case for all other door types (as of right now, wooden doors)
31+
} else if (block.getType().name().contains("_DOOR")) {
32+
Location location = block.getLocation();
33+
location.getWorld().playSound(location, Sound.ENTITY_ZOMBIE_ATTACK_WOODEN_DOOR, 1, 1);
34+
}
35+
}
36+
37+
}
38+
}
39+
}
40+
41+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package parallelmc.parallelutils.modules.bitsandbobs.minimodules;
2+
3+
import net.minecraft.nbt.NBTTagCompound;
4+
import org.bukkit.Material;
5+
import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack;
6+
import org.bukkit.event.EventHandler;
7+
import org.bukkit.event.Listener;
8+
import org.bukkit.event.entity.PlayerDeathEvent;
9+
import org.bukkit.event.player.PlayerRespawnEvent;
10+
import org.bukkit.inventory.ItemStack;
11+
12+
import java.util.ArrayList;
13+
import java.util.HashMap;
14+
import java.util.UUID;
15+
16+
17+
public class KeepSpecialItems implements Listener {
18+
19+
private final HashMap<UUID, ArrayList<ItemStack>> specialItemsLogger = new HashMap<>();
20+
21+
private final String[] SPECIAL_ITEMS = {"CustomHat", "CustomTrophy"};
22+
23+
@EventHandler
24+
public void onPlayerDeath(PlayerDeathEvent event) {
25+
// Creates an empty list for all items to be prevented from dropping
26+
ArrayList<ItemStack> preventedDrops = new ArrayList<>();
27+
28+
for (ItemStack item : event.getDrops()) {
29+
// TODO: This probably doesn't need to be dependent on item type. Just checking the tag is a bit more futureproof
30+
if (item.getType() == Material.PAPER) {
31+
// TODO: Try to change this code to use item.getItemMeta().getPersistentDataContainer()
32+
net.minecraft.world.item.ItemStack nmsItem = CraftItemStack.asNMSCopy(item); // does this even work lol
33+
// Grabs the NMS items compound and checks if it's null
34+
NBTTagCompound compound = (nmsItem.hasTag()) ? nmsItem.getTag() : new NBTTagCompound();
35+
36+
if (compound == null) continue;
37+
38+
if (compound.hasKey("CustomHat")) {
39+
preventedDrops.add(item);
40+
}
41+
} else if (item.getType() == Material.PLAYER_HEAD) {
42+
net.minecraft.world.item.ItemStack nmsItem = CraftItemStack.asNMSCopy(item); // does this even work lol
43+
// Grabs the NMS items compound and checks if it's null
44+
NBTTagCompound compound = (nmsItem.hasTag()) ? nmsItem.getTag() : new NBTTagCompound();
45+
46+
if (compound == null) continue;
47+
48+
if (compound.hasKey("CustomTrophy")) {
49+
preventedDrops.add(item);
50+
}
51+
}
52+
}
53+
// If the list of special drops has items in it, add a new player entry to the logger hashmap and remove the
54+
// dropped items
55+
if (!preventedDrops.isEmpty()) {
56+
// Gets the UUID of the player
57+
UUID uuid = event.getEntity().getUniqueId();
58+
specialItemsLogger.put(uuid, preventedDrops);
59+
60+
// Removes each special item from the original list of dropped items
61+
for (ItemStack item : preventedDrops) {
62+
event.getDrops().remove(item);
63+
}
64+
}
65+
}
66+
67+
@EventHandler
68+
public void onPlayerRespawn(PlayerRespawnEvent event) {
69+
// If the special items hashmap contains the player, then give them their special items back and remove them
70+
// from the hashmap
71+
if (specialItemsLogger.containsKey(event.getPlayer().getUniqueId())) {
72+
ArrayList<ItemStack> items = specialItemsLogger.get(event.getPlayer().getUniqueId());
73+
for (ItemStack item : items) {
74+
event.getPlayer().getInventory().addItem(item);
75+
}
76+
// Removes the player from the hashmap
77+
specialItemsLogger.remove(event.getPlayer().getUniqueId());
78+
}
79+
}
80+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package parallelmc.parallelutils.modules.bitsandbobs.minimodules;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.Location;
5+
import org.bukkit.Material;
6+
import org.bukkit.entity.Minecart;
7+
import org.bukkit.event.EventHandler;
8+
import org.bukkit.event.Listener;
9+
import org.bukkit.event.vehicle.VehicleMoveEvent;
10+
import org.bukkit.plugin.Plugin;
11+
import org.bukkit.plugin.PluginManager;
12+
import org.bukkit.scheduler.BukkitRunnable;
13+
import org.bukkit.util.Vector;
14+
import parallelmc.parallelutils.Constants;
15+
import parallelmc.parallelutils.Parallelutils;
16+
17+
import java.util.HashMap;
18+
import java.util.logging.Level;
19+
20+
public class SpeedyMinecarts implements Listener {
21+
22+
private static final double DEFAULT_MINECART_SPEED = 0.4d;
23+
private static final int SPEED_BOOST_SECONDS = 3;
24+
private final HashMap<Minecart, Integer> minecartList = new HashMap<>();
25+
private final Plugin plugin;
26+
27+
public SpeedyMinecarts() {
28+
PluginManager manager = Bukkit.getPluginManager();
29+
plugin = manager.getPlugin(Constants.PLUGIN_NAME);
30+
if (plugin == null) {
31+
Parallelutils.log(Level.SEVERE, "Unable to enable SpeedyMinecarts. Plugin " + Constants.PLUGIN_NAME
32+
+ " does not exist!");
33+
}
34+
}
35+
36+
@EventHandler
37+
public void onMinecartMove(VehicleMoveEvent event) {
38+
if (event.getVehicle() instanceof Minecart minecart) {
39+
Location cartlocation = minecart.getLocation();
40+
41+
// Checks if the block below the minecart is a powered rail
42+
if (minecart.getWorld().getBlockAt(cartlocation).getType() == Material.POWERED_RAIL) {
43+
44+
// Checks if the block below the rail is a redstone block
45+
Location blockbelowRail = cartlocation.add(0, -1, 0);
46+
47+
if (minecart.getWorld().getBlockAt(blockbelowRail).getType() == Material.REDSTONE_BLOCK) {
48+
49+
Vector cartVelocity = minecart.getVelocity();
50+
// *SHOULD* set the minecart max speed to 20 m/s (default is 8 m/s)
51+
minecart.setMaxSpeed(DEFAULT_MINECART_SPEED * 2.5);
52+
cartVelocity.multiply(2.5d);
53+
54+
// Checks if the minecart is NOT in the map - if it's not, it'll add it to the map
55+
// and remove the extra speed in 3 seconds IF no extra redstone block is driven over
56+
if (!minecartList.containsKey(minecart)) {
57+
58+
// Adds the minecart to the hashmap
59+
minecartList.put(minecart, SPEED_BOOST_SECONDS);
60+
// Counts down the speed boost timer from 3 seconds to 0 seconds
61+
new BukkitRunnable() {
62+
@Override
63+
public void run() {
64+
int secondsRemaining = minecartList.get(minecart);
65+
// If the timer runs out, remove the minecart from the hashmap, reset its speed, and
66+
// cancel the runnable
67+
if (secondsRemaining == 0) {
68+
minecartList.remove(minecart);
69+
minecart.setMaxSpeed(DEFAULT_MINECART_SPEED);
70+
cancel();
71+
} else {
72+
secondsRemaining--;
73+
minecartList.put(minecart, secondsRemaining);
74+
}
75+
}
76+
}.runTaskTimer(plugin, 20L, 20L);
77+
}
78+
// If another redstone block is driven over, the boost timer is reset to 3 seconds
79+
else if (minecartList.containsKey(minecart)) {
80+
minecartList.put(minecart, SPEED_BOOST_SECONDS);
81+
}
82+
}
83+
}
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)