Block morph rewrite

This commit is contained in:
samczsun 2017-02-11 18:29:27 -05:00 committed by cnr
parent 0befc7d967
commit e7dbe48937
2 changed files with 237 additions and 241 deletions

View File

@ -3,18 +3,10 @@ package mineplex.core.gadget.gadgets.morph;
import java.util.ArrayList;
import java.util.List;
import mineplex.core.common.util.F;
import mineplex.core.common.util.MapUtil;
import mineplex.core.common.util.UtilBlock;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilServer;
import mineplex.core.disguise.disguises.DisguiseCat;
import mineplex.core.disguise.disguises.DisguiseChicken;
import mineplex.core.gadget.event.GadgetBlockEvent;
import mineplex.core.gadget.gadgets.morph.managers.UtilMorph;
import mineplex.core.itemstack.ItemStackFactory;
import mineplex.core.recharge.Recharge;
import net.minecraft.server.v1_8_R3.Entity;
import net.minecraft.server.v1_8_R3.EntityArmorStand;
import net.minecraft.server.v1_8_R3.EntityPlayer;
import net.minecraft.server.v1_8_R3.IBlockData;
import org.bukkit.Bukkit;
import org.bukkit.Effect;
@ -23,67 +15,122 @@ import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftFallingSand;
import org.bukkit.entity.FallingBlock;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftSlime;
import org.bukkit.craftbukkit.v1_8_R3.util.CraftMagicNumbers;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Player;
import org.bukkit.entity.Slime;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilBlock;
import mineplex.core.common.util.UtilEnt;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilServer;
import mineplex.core.disguise.disguises.DisguiseBlock;
import mineplex.core.disguise.disguises.DisguiseSlime;
import mineplex.core.gadget.event.GadgetBlockEvent;
import mineplex.core.itemstack.ItemStackFactory;
public class BlockForm
{
private MorphBlock _host;
private Player _player;
private Material _mat;
private Material _blockMat;
private int _blockData;
private Block _block;
private Location _loc;
public BlockForm(MorphBlock host, Player player, Material mat)
private Slime _fallingBlock;
private ArmorStand _fallingBlockBase;
private DisguiseBlock _disguiseBlock;
private DisguiseSlime _disguiseBlockBase;
private DisguiseSlime _hiddenDisguise;
private EntityPlayer _entityPlayer;
private Entity _nmsFallingBlockBase;
public BlockForm(MorphBlock host, Player player, Material blockMat, int blockData)
{
_host = host;
_player = player;
_mat = mat;
_blockMat = blockMat;
_blockData = blockData;
_loc = player.getLocation();
_entityPlayer = ((CraftPlayer) player).getHandle();
_hiddenDisguise = new DisguiseSlime(player);
_hiddenDisguise.setInvisible(true);
_host.Manager.getDisguiseManager().disguise(_hiddenDisguise);
this._fallingBlock = _loc.getWorld().spawn(_loc, Slime.class);
this._fallingBlock.setSize(0);
this._fallingBlock.setRemoveWhenFarAway(false);
UtilEnt.vegetate(this._fallingBlock, true);
UtilEnt.ghost(this._fallingBlock, true, true);
this._fallingBlockBase = (ArmorStand) new EntityArmorStand(((CraftWorld) this._loc.getWorld()).getHandle(), this._loc.getX(), this._loc.getY(), this._loc.getZ()).getBukkitEntity();
this._fallingBlockBase.setGravity(false);
this._fallingBlockBase.setVisible(false);
this._fallingBlockBase.setRemoveWhenFarAway(false);
this._fallingBlockBase.setPassenger(this._fallingBlock);
_nmsFallingBlockBase = ((CraftEntity) _fallingBlockBase).getHandle();
_disguiseBlockBase = new DisguiseSlime(_fallingBlockBase);
_disguiseBlockBase.SetSize(0);
_disguiseBlockBase.setInvisible(true);
_host.Manager.getDisguiseManager().disguise(_disguiseBlockBase);
Apply();
}
public void Apply()
private void createFallingBlock()
{
// Remove Old
if (_player.getPassenger() != null)
{
Recharge.Instance.useForce(_player, "PassengerChange", 100);
removeFallingBlock();
_player.getPassenger().remove();
_player.eject();
_disguiseBlock = new DisguiseBlock(_fallingBlock, _blockMat.getId(), _blockData);
_disguiseBlock.setHideIfNotDisguised(true);
_host.Manager.getDisguiseManager().disguise(_disguiseBlock);
_fallingBlockBase.setPassenger(_fallingBlock);
}
private void removeFallingBlock()
{
if (_disguiseBlock != null)
{
_host.Manager.getDisguiseManager().undisguise(_disguiseBlock);
_disguiseBlock = null;
_fallingBlockBase.setPassenger(null);
}
}
private void Apply()
{
if (_block != null)
{
SolidifyRemove(false);
}
createFallingBlock();
// Inform
String name = ItemStackFactory.Instance.GetName(_blockMat, (byte) _blockData, false);
if (!name.contains("Block"))
{
name = name + " Block";
}
((CraftEntity) _player).getHandle().getDataWatcher().watch(0, Byte.valueOf((byte) 32), Entity.META_ENTITYDATA, (byte) 32);
// Player > Chicken
DisguiseChicken disguise = new DisguiseChicken(_player);
disguise.setBaby();
disguise.setSoundDisguise(new DisguiseCat(_player));
disguise.setInvisible(true);
//_host.Manager.getDisguiseManager().disguise(disguise);
UtilMorph.disguise(_player, disguise, _host.Manager.getDisguiseManager());
// Apply Falling Block
FallingBlockCheck();
// Inform
String blockName = F.elem(ItemStackFactory.Instance.GetName(_mat, (byte) 0, false));
if (!blockName.contains("Block"))
UtilPlayer
.message(
_player,
F.main("Morph",
"You are now a "
+ F.elem(ItemStackFactory.Instance.GetName(_mat, (byte) 0, false) + " Block") + "!"));
else
UtilPlayer.message(_player,
F.main("Morph", "You are now a " + F.elem(ItemStackFactory.Instance.GetName(_mat, (byte) 0, false)) + "!"));
UtilPlayer.message(_player, F.main("Morph", "You are now " + F.vowelAN(name) + " " + F.elem(name) + "!"));
// Sound
_player.playSound(_player.getLocation(), Sound.ZOMBIE_UNFECT, 2f, 2f);
@ -91,32 +138,39 @@ public class BlockForm
public void Remove()
{
SolidifyRemove();
SolidifyRemove(false);
removeFallingBlock();
//_host.Manager.getDisguiseManager().undisguise(_player);
UtilMorph.undisguise(_player, _host.Manager.getDisguiseManager());
// Remove FB
if (_player.getPassenger() != null)
{
Recharge.Instance.useForce(_player, "PassengerChange", 100);
_player.getPassenger().remove();
_player.eject();
}
((CraftEntity) _player).getHandle().getDataWatcher().watch(0, Byte.valueOf((byte) 0), Entity.META_ENTITYDATA, (byte) 0);
_host.Manager.getDisguiseManager().undisguise(_hiddenDisguise);
_host.Manager.getDisguiseManager().undisguise(_disguiseBlockBase);
_fallingBlockBase.remove();
_fallingBlockBase = null;
_fallingBlock.remove();
_fallingBlock = null;
_nmsFallingBlockBase = null;
}
public void SolidifyUpdate()
public void update()
{
if (!_player.isSprinting())
((CraftEntity) _player).getHandle().getDataWatcher()
.watch(0, Byte.valueOf((byte) 32), Entity.META_ENTITYDATA, (byte) 32);
// Not a Block
if (_block == null)
{
if (_fallingBlockBase.getPassenger() != _fallingBlock)
_fallingBlockBase.setPassenger(_fallingBlock);
if (!_nmsFallingBlockBase.getBukkitEntity().getWorld().equals(_player.getWorld()))
_nmsFallingBlockBase.getBukkitEntity().teleport(_player);
else
{
_nmsFallingBlockBase.locX = _entityPlayer.locX;
_nmsFallingBlockBase.locY = _entityPlayer.locY;
_nmsFallingBlockBase.locZ = _entityPlayer.locZ;
_nmsFallingBlockBase.motX = _entityPlayer.motX;
_nmsFallingBlockBase.motY = _entityPlayer.motY;
_nmsFallingBlockBase.motZ = _entityPlayer.motZ;
_nmsFallingBlockBase.velocityChanged = true;
}
// Moved
if (!_loc.getBlock().equals(_player.getLocation().getBlock()))
{
@ -135,7 +189,7 @@ public class BlockForm
{
Block block = _player.getLocation().getBlock();
List<Block> blockList = new ArrayList<Block>();
List<Block> blockList = new ArrayList<>();
blockList.add(block);
GadgetBlockEvent event = new GadgetBlockEvent(_host, blockList);
@ -155,14 +209,15 @@ public class BlockForm
_block = block;
// Effect
_player.playEffect(_player.getLocation(), Effect.STEP_SOUND, _mat);
// block.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, _mat);
_player.playEffect(_player.getLocation(), Effect.STEP_SOUND, _blockMat);
removeFallingBlock();
// Display
SolidifyVisual();
// Invisible
// Host.Manager.GetCondition().Factory().Cloak("Disguised as Block", Player, Player, 60000, false, false);
for (Player other : UtilServer.getPlayers())
{
other.sendBlockChange(_player.getLocation(), _blockMat, (byte) _blockData);
}
// Sound
_player.playSound(_player.getLocation(), Sound.NOTE_PLING, 1f, 2f);
@ -175,93 +230,62 @@ public class BlockForm
// Moved
if (!_loc.getBlock().equals(_player.getLocation().getBlock()))
{
SolidifyRemove();
}
// Send Packets
else
{
SolidifyVisual();
SolidifyRemove(true);
}
}
}
public void SolidifyRemove()
private void SolidifyRemove(boolean create)
{
if (_block != null)
{
MapUtil.QuickChangeBlockAt(_block.getLocation(), 0, (byte) 0);
Location location = _block.getLocation();
_block = null;
for (Player other : UtilServer.getPlayers())
{
other.sendBlockChange(location, 0, (byte) 0);
}
_player.setExp(0f);
// Inform
_player.playSound(_player.getLocation(), Sound.NOTE_PLING, 1f, 0.5f);
if (create)
createFallingBlock();
}
_player.setExp(0f);
// Host.Manager.GetCondition().EndCondition(Player, null, "Disguised as Block");
// Inform
_player.playSound(_player.getLocation(), Sound.NOTE_PLING, 1f, 0.5f);
FallingBlockCheck();
}
@SuppressWarnings("deprecation")
public void SolidifyVisual()
public void reset(Block block)
{
if (_block == null)
if (block == null)
return;
// Remove Old
if (_player.getPassenger() != null)
{
Recharge.Instance.useForce(_player, "PassengerChange", 100);
_player.getPassenger().remove();
_player.eject();
}
// Others
for (Player other : UtilServer.getPlayers())
other.sendBlockChange(_player.getLocation(), _mat, (byte) 0);
// Self
_player.sendBlockChange(_player.getLocation(), 36, (byte) 0);
FallingBlockCheck();
}
public void FallingBlockCheck()
{
// Block Form (Hide Falling)
if (_block != null)
if (block.getType() == Material.AIR)
return;
// Recreate Falling
if (_player.getPassenger() == null || !_player.getPassenger().isValid())
{
if (!Recharge.Instance.use(_player, "PassengerChange", 100, false, false))
return;
if (_blockMat == block.getType() && _blockData == block.getData())
return;
// Falling Block
FallingBlock block = _player.getWorld().spawnFallingBlock(_player.getEyeLocation(), _mat, (byte) 0);
// No Arrow Collision
((CraftFallingSand) block).getHandle().spectating = true;
_player.setPassenger(block);
_host.fallingBlockRegister(block);
}
// Ensure Falling doesnt Despawn
else
{
((CraftFallingSand) _player.getPassenger()).getHandle().ticksLived = 1;
_player.getPassenger().setTicksLived(1);
}
_blockMat = block.getType();
_blockData = block.getData();
Apply();
}
public Block GetBlock()
public Block getBlock()
{
return _block;
}
public IBlockData getBlockData()
{
return CraftMagicNumbers.getBlock(_blockMat).fromLegacyData(_blockData);
}
public Player getPlayer()
{
return this._player;
}
}

View File

@ -1,48 +1,54 @@
package mineplex.core.gadget.gadgets.morph;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import net.minecraft.server.v1_8_R3.Blocks;
import net.minecraft.server.v1_8_R3.PacketPlayInBlockDig;
import net.minecraft.server.v1_8_R3.PacketPlayOutBlockChange;
import net.minecraft.server.v1_8_R3.PacketPlayOutEntityVelocity;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.FallingBlock;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.entity.ItemSpawnEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import mineplex.core.common.util.C;
import mineplex.core.common.util.LineFormat;
import mineplex.core.common.util.UtilBlock;
import mineplex.core.common.util.UtilEvent;
import mineplex.core.common.util.UtilMath;
import mineplex.core.common.util.UtilText;
import mineplex.core.common.util.UtilEvent.ActionType;
import mineplex.core.event.StackerEvent;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilText;
import mineplex.core.gadget.GadgetManager;
import mineplex.core.gadget.types.MorphGadget;
import mineplex.core.packethandler.IPacketHandler;
import mineplex.core.packethandler.PacketInfo;
import mineplex.core.recharge.Recharge;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.core.utils.UtilScheduler;
public class MorphBlock extends MorphGadget implements IPacketHandler
{
private Map<Player, BlockForm> _active = new HashMap<>();
public class MorphBlock extends MorphGadget
{
private HashMap<Player, BlockForm> _active = new HashMap<Player, BlockForm>();
private HashSet<FallingBlock> _blocks = new HashSet<FallingBlock>();
public MorphBlock(GadgetManager manager)
{
super(manager, "Block Morph", UtilText.splitLinesToArray(new String[]
{
C.cGray + "The blockiest block that ever blocked.",
C.blankLine,
"#" + C.cWhite + "Left Click to use Change Block",
"#" + C.cWhite + "Stay Still to use Solidify",
}, LineFormat.LORE),
super(manager, "Block Morph", UtilText.splitLinesToArray(new String[]
{
C.cGray + "The blockiest block that ever blocked.",
C.blankLine,
"#" + C.cWhite + "Left Click to use Change Block",
"#" + C.cWhite + "Stay Still to use Solidify",
}, LineFormat.LORE),
30000,
Material.EMERALD_BLOCK, (byte)0);
Material.EMERALD_BLOCK, (byte) 0);
UtilScheduler.runEvery(UpdateType.TICK, () -> _active.values().forEach(BlockForm::update));
manager.getPacketManager().addPacketHandler(this, PacketPlayOutBlockChange.class, PacketPlayInBlockDig.class);
}
@Override
@ -50,7 +56,7 @@ public class MorphBlock extends MorphGadget
{
this.applyArmor(player, message);
_active.put(player, new BlockForm(this, player, Material.EMERALD_BLOCK));
_active.put(player, new BlockForm(this, player, Material.EMERALD_BLOCK, 0));
}
@Override
@ -58,108 +64,74 @@ public class MorphBlock extends MorphGadget
{
this.removeArmor(player);
BlockForm form = _active.remove(player);
if (form != null)
{
form.Remove();
}
}
@EventHandler
public void formUpdate(UpdateEvent event)
{
if (event.getType() != UpdateType.TICK)
return;
for (BlockForm form : _active.values())
{
form.SolidifyUpdate();
form.FallingBlockCheck();
}
}
@EventHandler
public void formChange(PlayerInteractEvent event)
{
if (event.getClickedBlock() == null)
return;
if (!UtilEvent.isAction(event, ActionType.L_BLOCK) && !UtilEvent.isAction(event, ActionType.R_BLOCK))
return;
if (!UtilBlock.solid(event.getClickedBlock()))
return;
if (!Recharge.Instance.use(event.getPlayer(), getName(), 500, false, false))
return;
BlockForm form = _active.get(event.getPlayer());
if (form == null)
return;
form.Remove();
_active.put(event.getPlayer(), new BlockForm(this, event.getPlayer(), event.getClickedBlock().getType()));
form.reset(event.getClickedBlock());
}
@EventHandler
public void stacker(StackerEvent event)
@Override
public void handle(PacketInfo packetInfo)
{
if (_active.containsKey(event.getEntity()))
event.setCancelled(true);
}
public void fallingBlockRegister(FallingBlock block)
{
_blocks.add(block);
}
@EventHandler
public void fallingBlockForm(EntityChangeBlockEvent event)
{
if (_blocks.remove(event.getEntity()))
if (packetInfo.getPacket() instanceof PacketPlayOutBlockChange)
{
event.getEntity().remove();
event.setCancelled(true);
}
}
@EventHandler
public void fallingBlockClean(UpdateEvent event)
{
if (event.getType() != UpdateType.SEC)
return;
Iterator<FallingBlock> blockIterator = _blocks.iterator();
while (blockIterator.hasNext())
{
FallingBlock block = blockIterator.next();
if (!block.isValid() || block.getVehicle() == null)
PacketPlayOutBlockChange packet = (PacketPlayOutBlockChange) packetInfo.getPacket();
for (BlockForm form : _active.values())
{
block.remove();
blockIterator.remove();
if (form.getBlock() == null)
continue;
Location location = form.getBlock().getLocation();
if (packetInfo.getPlayer().getWorld() == location.getWorld() && packet.a.getX() == location.getX() && packet.a.getY() == location.getY() && packet.a.getZ() == location.getZ())
{
if (packetInfo.getPlayer() == form.getPlayer())
{
packet.block = Blocks.AIR.getBlockData();
}
else
{
packet.block = form.getBlockData();
}
}
}
}
}
@EventHandler
public void itemSpawnCancel(ItemSpawnEvent event)
{
Iterator<FallingBlock> blockIterator = _blocks.iterator();
while (blockIterator.hasNext())
else if (packetInfo.getPacket() instanceof PacketPlayInBlockDig)
{
FallingBlock block = blockIterator.next();
if (UtilMath.offset(block, event.getEntity()) < 0.1)
PacketPlayInBlockDig packet = (PacketPlayInBlockDig) packetInfo.getPacket();
for (BlockForm form : _active.values())
{
block.remove();
blockIterator.remove();
event.setCancelled(true);
if (form.getBlock() == null)
continue;
Location location = form.getBlock().getLocation();
if (packetInfo.getPlayer().getWorld() == location.getWorld() && packet.a.getX() == location.getX() && packet.a.getY() == location.getY() && packet.a.getZ() == location.getZ())
{
packetInfo.setCancelled(true);
packetInfo.getPlayer().sendBlockChange(location, 0, (byte) 0);
}
}
}
}