A feature-rich Minecraft plugin which enhances gaming experience
git clone git://soophie.de/poppy
blob: 444bdff8acf0b7af258c42a330757f0de72d6b15 (4499 bytes)[doc][blame][raw]
1
package poppy.modules;3
import java.util.HashMap;4
import java.util.Map;5
import java.util.Map.Entry;7
import org.bukkit.Location;8
import org.bukkit.Material;9
import org.bukkit.block.Block;10
import org.bukkit.block.data.Bisected.Half;11
import org.bukkit.block.data.type.Stairs;12
import org.bukkit.entity.ArmorStand;13
import org.bukkit.entity.Entity;14
import org.bukkit.entity.EntityType;15
import org.bukkit.entity.Player;16
import org.bukkit.event.EventHandler;17
import org.bukkit.event.Listener;18
import org.bukkit.event.block.Action;19
import org.bukkit.event.block.BlockBreakEvent;20
import org.bukkit.event.entity.EntityDismountEvent;21
import org.bukkit.event.player.PlayerInteractEvent;23
import poppy.Utils;26
public class ChairModule implements Listener27
{28
private static final Material[] CHAIR_TYPE = new Material[]29
{30
Material.OAK_STAIRS,31
Material.SPRUCE_STAIRS,32
Material.BIRCH_STAIRS,33
Material.JUNGLE_STAIRS,34
Material.ACACIA_STAIRS,35
Material.DARK_OAK_STAIRS,36
Material.ACACIA_STAIRS,37
Material.CRIMSON_STAIRS,38
Material.WARPED_STAIRS39
};41
private Map<Block, ArmorStand> blockMap = new HashMap<>();43
public void onDisable()44
{45
for(ArmorStand armorStand : blockMap.values())46
{47
armorStand.eject();48
armorStand.remove();49
}50
}52
@EventHandler53
public void onPlayerInteract(PlayerInteractEvent event)54
{55
Player player = event.getPlayer();57
if(event.getAction() == Action.RIGHT_CLICK_BLOCK)58
{59
if(event.getClickedBlock() != null && !event.getPlayer().isSneaking())60
{61
Block block = event.getClickedBlock();63
if(isChair(block.getType()))64
{65
if(!isInRange(block, player))66
{67
return;68
}70
if(blockMap.containsKey(block))71
{72
return;73
}75
if(isCompatible(block))76
{77
ArmorStand armorStand = createMountableChair(block, player);79
if(armorStand == null)80
{81
return;82
}84
event.setCancelled(true);86
blockMap.put(block, armorStand);87
}88
}89
}90
}91
}93
@EventHandler94
public void onEntityDismount(EntityDismountEvent event)95
{96
if(event.getEntity() instanceof Player)97
{98
Entity entity = event.getDismounted();100
if(entity != null && entity instanceof ArmorStand)101
{102
if(blockMap.containsValue(entity))103
{104
for(Entry<Block, ArmorStand> entry : blockMap.entrySet())105
{106
if(entry.getValue() == entity)107
{108
blockMap.remove(entry.getKey());110
break;111
}112
}113
}115
Location location = entity.getLocation();116
location.add(0, 1, 0);118
for(Entity passenger : entity.getPassengers()) {119
location.setPitch(passenger.getLocation().getPitch());120
location.setYaw(passenger.getLocation().getYaw());121
passenger.teleport(location);122
}124
entity.remove();125
}126
}127
}129
@EventHandler130
public void onBlockBreak(BlockBreakEvent event)131
{132
Block block = event.getBlock();134
if(blockMap.containsKey(block))135
{136
ArmorStand armorStand = blockMap.get(block);137
armorStand.eject();139
blockMap.remove(block);140
}141
}143
private boolean isInRange(Block block, Player player)144
{145
return block.getLocation().add(0.5, 0.5, 0.5).distance(player.getLocation()) <= 2;146
}148
private boolean isChair(Material material)149
{150
for(Material chair : CHAIR_TYPE)151
{152
if(material == chair)153
{154
return true;155
}156
}158
return false;159
}161
private boolean isCompatible(Block block)162
{163
Block upperBlock = block.getWorld().getBlockAt(block.getLocation().add(0, 1, 0));165
if(!Utils.isAir(upperBlock.getType()))166
{167
return false;168
}170
Stairs stairs = (Stairs) block.getState().getBlockData();172
if(stairs.getHalf() != Half.BOTTOM)173
{174
return false;175
}177
if(stairs.isWaterlogged())178
{179
return false;180
}182
if(stairs.getShape() == Stairs.Shape.INNER_RIGHT || stairs.getShape() == Stairs.Shape.INNER_LEFT)183
{184
return false;185
}187
return true;188
}190
@SuppressWarnings("deprecation")191
private ArmorStand createMountableChair(Block block, Player player)192
{193
Stairs stairs = (Stairs) block.getState().getBlockData();195
Float yaw = Utils.convertFacingToYaw(stairs.getFacing());197
if(yaw == null)198
{199
return null;200
}202
Location location = block.getLocation().add(0.5, -0.4, 0.5);203
location.setYaw(yaw);205
ArmorStand armorStand = (ArmorStand) block.getWorld().spawnEntity(location, EntityType.ARMOR_STAND);206
armorStand.setPassenger((Entity) player);207
armorStand.setSmall(true);208
armorStand.setGravity(false);209
armorStand.setInvulnerable(true);210
armorStand.setVisible(false);212
return armorStand;213
}214
}