implemented new multiblock for summon head
This commit is contained in:
parent
350858d221
commit
f0e8ee791a
@ -1,76 +1,84 @@
|
||||
package org.kapelle.hardcore_revive;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Particle;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.Sign;
|
||||
import org.bukkit.block.Skull;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.BlockPlaceEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.SkullMeta;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.util.BlockVector;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.kapelle.multiblock.MultiblockListener;
|
||||
import org.kapelle.multiblock.MultiblockStructure;
|
||||
import org.kapelle.multiblock.block_component.IBlockComponent;
|
||||
import org.kapelle.multiblock.block_component.MultipleBlocks;
|
||||
import org.kapelle.multiblock.block_component.SingleBlock;
|
||||
|
||||
public class SummonHead implements Listener {
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class SummonHead{
|
||||
private final JavaPlugin plugin;
|
||||
|
||||
public SummonHead(JavaPlugin plugin){
|
||||
this.plugin = plugin;
|
||||
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onBlockPlace(BlockPlaceEvent event){
|
||||
if(event.getBlockPlaced().getType() == Material.SOUL_CAMPFIRE && !event.isCancelled()){
|
||||
if (isValidMultiblock(event.getBlockPlaced().getLocation())){
|
||||
summonHead(event.getBlockPlaced().getLocation());
|
||||
new MultiblockListener(plugin, getMultiblockStructure(), new BlockVector(0, 0, 0)) {
|
||||
@Override
|
||||
protected void callback(Location coreBlockLocation) {
|
||||
summonHead(coreBlockLocation);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a block type is a sign that is standing (not mounted on a wall)
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
private boolean isStandingSign(Material block){
|
||||
return block == Material.SPRUCE_SIGN
|
||||
|| block == Material.ACACIA_SIGN
|
||||
|| block == Material.BIRCH_SIGN
|
||||
|| block == Material.CRIMSON_SIGN
|
||||
|| block == Material.DARK_OAK_SIGN
|
||||
|| block == Material.JUNGLE_SIGN
|
||||
|| block == Material.OAK_SIGN
|
||||
|| block == Material.WARPED_SIGN;
|
||||
}
|
||||
private MultiblockStructure getMultiblockStructure(){
|
||||
IBlockComponent sign = new MultipleBlocks(new HashSet<Material>(){{
|
||||
add(Material.SPRUCE_SIGN);
|
||||
add(Material.ACACIA_SIGN);
|
||||
add(Material.CRIMSON_SIGN);
|
||||
add(Material.JUNGLE_SIGN);
|
||||
add(Material.OAK_SIGN);
|
||||
add(Material.WARPED_SIGN);
|
||||
}});
|
||||
|
||||
/**
|
||||
* Checks if the last placed block is a valid multiblock.
|
||||
* The valid structure is from top to bottom: Standing sign, soul sand, soul campfire, soul sand
|
||||
* @param lastBlockLocation Exprected to be the soul campfire
|
||||
* @return if the structure is valid
|
||||
*/
|
||||
private boolean isValidMultiblock(Location lastBlockLocation){
|
||||
return lastBlockLocation.clone().add(new Vector(0,-1,0)).getBlock().getType() == Material.SOUL_SAND
|
||||
&& lastBlockLocation.clone().add(new Vector(0,1,0)).getBlock().getType() == Material.SOUL_SAND
|
||||
&& isStandingSign(lastBlockLocation.clone().add(new Vector(0,2,0)).getBlock().getType());
|
||||
IBlockComponent hayBale = new SingleBlock(Material.HAY_BLOCK);
|
||||
|
||||
IBlockComponent pumpkin = new MultipleBlocks(new HashSet<Material>(){{
|
||||
add(Material.JACK_O_LANTERN);
|
||||
add(Material.CARVED_PUMPKIN);
|
||||
}});
|
||||
|
||||
return new MultiblockStructure(Arrays.asList(
|
||||
// Bottom layer
|
||||
Arrays.asList(
|
||||
Arrays.asList(hayBale)
|
||||
),
|
||||
|
||||
// Mid Layer
|
||||
Arrays.asList(
|
||||
Arrays.asList(pumpkin)
|
||||
),
|
||||
|
||||
// Top layer
|
||||
Arrays.asList(
|
||||
Arrays.asList(sign)
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the head summoning ritual.
|
||||
* @param lastBlockLocation The location of the core block of the multiblock structure. In this case
|
||||
* @param coreBlockLocation The location of the core block of the multiblock structure. In this case
|
||||
* the soul campfire.
|
||||
*/
|
||||
private void summonHead(Location lastBlockLocation){
|
||||
Block topBlock = lastBlockLocation.clone().add(new Vector(0,2,0)).getBlock();
|
||||
private void summonHead(Location coreBlockLocation){
|
||||
Location signBlock = coreBlockLocation.clone().add(new Vector(0,2,0));
|
||||
Location pumpkin = coreBlockLocation.clone().add(new Vector(0,1,0));
|
||||
|
||||
String spiritName = getSignText((Sign) topBlock.getState());
|
||||
String spiritName = getSignText((Sign) signBlock.getBlock().getState());
|
||||
Player spirit = plugin.getServer().getPlayerExact(spiritName);
|
||||
|
||||
if (spirit == null) {
|
||||
@ -79,10 +87,11 @@ public class SummonHead implements Listener {
|
||||
}
|
||||
|
||||
// Remove sign
|
||||
topBlock.setType(Material.AIR);
|
||||
signBlock.getBlock().setType(Material.AIR);
|
||||
pumpkin.getBlock().setType(Material.AIR);
|
||||
|
||||
// Spawn particle at sign
|
||||
topBlock.getWorld().spawnParticle(Particle.SPELL_WITCH,topBlock.getLocation(),20);
|
||||
pumpkin.getWorld().spawnParticle(Particle.SPELL_WITCH,pumpkin,20);
|
||||
|
||||
// Prepare head to be spawned
|
||||
ItemStack head = new ItemStack(Material.PLAYER_HEAD);
|
||||
@ -92,11 +101,7 @@ public class SummonHead implements Listener {
|
||||
head.setItemMeta(headMeta);
|
||||
|
||||
// Spawn head at the position of the sign
|
||||
lastBlockLocation.getWorld().dropItem(topBlock.getLocation(),head);
|
||||
|
||||
// Remove soul campfire
|
||||
// FIXME: removing the campfire raises an execption somewhere else. But i think its fine. Maybe removing a tile entity works different
|
||||
lastBlockLocation.getBlock().setType(Material.AIR);
|
||||
pumpkin.getWorld().dropItem(pumpkin,head);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user