-
Notifications
You must be signed in to change notification settings - Fork 5
/
SkullBlockListener.java
84 lines (70 loc) · 3 KB
/
SkullBlockListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* Skulls
* Copyright 2022 Kiran Hart
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package ca.tweetzy.skulls.listeners;
import ca.tweetzy.flight.comp.NBTEditor;
import ca.tweetzy.flight.comp.enums.CompMaterial;
import ca.tweetzy.flight.utils.PlayerUtil;
import ca.tweetzy.skulls.Skulls;
import ca.tweetzy.skulls.api.interfaces.PlacedSkull;
import ca.tweetzy.skulls.api.interfaces.Skull;
import ca.tweetzy.skulls.hooks.WorldGuardHook;
import ca.tweetzy.skulls.impl.PlacedSkullLocation;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;
import java.util.UUID;
public final class SkullBlockListener implements Listener {
@EventHandler()
public void onSkullPlace(final BlockPlaceEvent event) {
if (event.isCancelled()) return;
if (!WorldGuardHook.isAllowedPlace(event.getPlayer(), event.getBlock())) {
event.setBuild(false);
event.setCancelled(true);
return;
}
final ItemStack item = PlayerUtil.getHand(event.getPlayer());
if (!NBTEditor.contains(item, "Skulls:ID")) {
return;
}
final Block block = event.getBlockPlaced();
if (block.getType() == CompMaterial.PLAYER_HEAD.parseMaterial() || block.getType() == CompMaterial.PLAYER_WALL_HEAD.parseMaterial()) {
final int skullId = Integer.parseInt(NBTEditor.getString(item, "Skulls:ID"));
Skulls.getSkullManager().addPlacedSkull(new PlacedSkullLocation(UUID.randomUUID(), skullId, block.getLocation()));
}
}
@EventHandler()
public void onSkullBreak(final BlockBreakEvent event) {
if (event.isCancelled()) return;
if (!WorldGuardHook.isAllowedBreak(event.getPlayer(), event.getBlock())) {
event.setCancelled(true);
return;
}
final Block block = event.getBlock();
if (block.getType() == CompMaterial.PLAYER_HEAD.parseMaterial() || block.getType() == CompMaterial.PLAYER_WALL_HEAD.parseMaterial()) {
if (!Skulls.getSkullManager().getPlacedSkulls().containsKey(block.getLocation())) return;
final PlacedSkull placedSkull = Skulls.getSkullManager().getPlacedSkulls().get(block.getLocation());
Skulls.getSkullManager().removePlacedSkull(placedSkull);
event.setDropItems(false);
final Skull skull = Skulls.getSkullManager().getSkull(placedSkull.getSkullId());
block.getWorld().dropItemNaturally(block.getLocation(), skull.getItemStack());
}
}
}