better hitboxes for blockhunt

This commit is contained in:
Tyler Murphy 2022-07-29 21:49:43 -04:00
parent 4d737afc4f
commit 055abc11bd
6 changed files with 109 additions and 67 deletions

View file

@ -96,6 +96,7 @@ public class Main extends JavaPlugin implements Listener {
Bukkit.getServer().getMessenger().unregisterOutgoingPluginChannel(this); Bukkit.getServer().getMessenger().unregisterOutgoingPluginChannel(this);
board.cleanup(); board.cleanup();
disguiser.cleanUp();
} }
private void onTick() { private void onTick() {

View file

@ -103,6 +103,9 @@ public class Board {
} }
public void addHider(Player player) { public void addHider(Player player) {
if(!Main.getInstance().supports(9)){
player.spigot().setCollidesWithEntities(false);
}
Hider.add(player.getUniqueId().toString()); Hider.add(player.getUniqueId().toString());
Seeker.remove(player.getUniqueId().toString()); Seeker.remove(player.getUniqueId().toString());
Spectator.remove(player.getUniqueId().toString()); Spectator.remove(player.getUniqueId().toString());
@ -110,6 +113,9 @@ public class Board {
} }
public void addSeeker(Player player) { public void addSeeker(Player player) {
if(!Main.getInstance().supports(9)){
player.spigot().setCollidesWithEntities(false);
}
Hider.remove(player.getUniqueId().toString()); Hider.remove(player.getUniqueId().toString());
Seeker.add(player.getUniqueId().toString()); Seeker.add(player.getUniqueId().toString());
Spectator.remove(player.getUniqueId().toString()); Spectator.remove(player.getUniqueId().toString());
@ -117,6 +123,9 @@ public class Board {
} }
public void addSpectator(Player player) { public void addSpectator(Player player) {
if(!Main.getInstance().supports(9)){
player.spigot().setCollidesWithEntities(false);
}
Hider.remove(player.getUniqueId().toString()); Hider.remove(player.getUniqueId().toString());
Seeker.remove(player.getUniqueId().toString()); Seeker.remove(player.getUniqueId().toString());
Spectator.add(player.getUniqueId().toString()); Spectator.add(player.getUniqueId().toString());
@ -124,6 +133,9 @@ public class Board {
} }
public void remove(Player player) { public void remove(Player player) {
if(!Main.getInstance().supports(9)){
player.spigot().setCollidesWithEntities(true);
}
Hider.remove(player.getUniqueId().toString()); Hider.remove(player.getUniqueId().toString());
Seeker.remove(player.getUniqueId().toString()); Seeker.remove(player.getUniqueId().toString());
Spectator.remove(player.getUniqueId().toString()); Spectator.remove(player.getUniqueId().toString());

View file

@ -1,16 +1,12 @@
package net.tylermurphy.hideAndSeek.game; package net.tylermurphy.hideAndSeek.game;
import net.tylermurphy.hideAndSeek.game.util.Disguise; import net.tylermurphy.hideAndSeek.game.util.Disguise;
import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.util.BlockVector;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.Vector;
public class Disguiser { public class Disguiser {
@ -32,11 +28,8 @@ public class Disguiser {
} }
@Nullable @Nullable
public Disguise getByBlockLocation(BlockVector loc){ public Disguise getByHitBoxID(int ID){
return disguises.values().stream().filter(disguise -> { return disguises.values().stream().filter(disguise -> disguise.getHitBoxID() == ID).findFirst().orElse(null);
if(disguise.getSolidLocation() == null) return false;
return disguise.getSolidLocation().toVector().toBlockVector() == loc;
}).findFirst().orElse(null);
} }
public void check(){ public void check(){
@ -66,4 +59,8 @@ public class Disguiser {
disguises.remove(player); disguises.remove(player);
} }
public void cleanUp() {
disguises.values().forEach(Disguise::remove);
}
} }

View file

@ -65,34 +65,20 @@ public class DisguiseHandler implements Listener {
// } // }
private PacketAdapter createProtocol(){ private PacketAdapter createProtocol(){
return new PacketAdapter(Main.getInstance(), USE_ITEM, USE_ENTITY) { return new PacketAdapter(Main.getInstance(), USE_ENTITY) {
@Override @Override
public void onPacketReceiving(PacketEvent event){ public void onPacketReceiving(PacketEvent event){
PacketContainer packet = event.getPacket(); PacketContainer packet = event.getPacket();
Player player = event.getPlayer(); Player player = event.getPlayer();
// if(!Main.getInstance().getBoard().isSeeker(player)) return; // if(!Main.getInstance().getBoard().isSeeker(player)) return;
if(packet.getType() == USE_ITEM) {
System.out.print("\nUse Item: ");
BlockPosition data;
try { data = packet.getBlockPositionModifier().read(0); }
catch (Exception e) { return; }
System.out.print(data + " ");
BlockVector loc = new BlockVector(data.getX(), data.getY(), data.getZ());
System.out.print(loc + " ");
Disguise disguise = Main.getInstance().getDisguiser().getByBlockLocation(loc);
System.out.print("FOUND");
handleAttack(disguise, player);
} else if(packet.getType() == USE_ENTITY) {
System.out.print("\nUse Entity: ");
int id = packet.getIntegers().read(0); int id = packet.getIntegers().read(0);
System.out.print(id + " ");
Disguise disguise = Main.getInstance().getDisguiser().getByEntityID(id); Disguise disguise = Main.getInstance().getDisguiser().getByEntityID(id);
System.out.print("FOUND"); if(disguise == null) disguise = Main.getInstance().getDisguiser().getByHitBoxID(id);
if(disguise == null) return;
event.setCancelled(true);
handleAttack(disguise, player); handleAttack(disguise, player);
} }
}
}; };
} }
@ -100,6 +86,8 @@ public class DisguiseHandler implements Listener {
private void handleAttack(Disguise disguise, Player seeker){ private void handleAttack(Disguise disguise, Player seeker){
if(disguise.getPlayer() == seeker) return;
double amount; double amount;
if(Main.getInstance().supports(9)) { if(Main.getInstance().supports(9)) {
amount = seeker.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE).getValue(); amount = seeker.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE).getValue();
@ -107,7 +95,6 @@ public class DisguiseHandler implements Listener {
amount = getItemDamageValue(seeker.getItemInHand(), disguise.getPlayer(), seeker); amount = getItemDamageValue(seeker.getItemInHand(), disguise.getPlayer(), seeker);
} }
if(disguise == null) return;
disguise.setSolidify(false); disguise.setSolidify(false);
if(debounce.contains(disguise.getPlayer())) return; if(debounce.contains(disguise.getPlayer())) return;
debounce.add(disguise.getPlayer()); debounce.add(disguise.getPlayer());
@ -197,4 +184,5 @@ public class DisguiseHandler implements Listener {
return (int) Math.round(Math.max(damageValue, 0.0)); return (int) Math.round(Math.max(damageValue, 0.0));
} }
} }

View file

@ -22,6 +22,7 @@ public class MovementHandler implements Listener {
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST)
public void onMove(PlayerMoveEvent event) { public void onMove(PlayerMoveEvent event) {
if (event.getTo() == null || event.getTo().getWorld() == null) return; if (event.getTo() == null || event.getTo().getWorld() == null) return;
checkJumping(event); checkJumping(event);
checkBounds(event); checkBounds(event);

View file

@ -10,11 +10,11 @@ import net.tylermurphy.hideAndSeek.Main;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.FallingBlock; import org.bukkit.entity.*;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.Nullable; import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
@ -24,62 +24,94 @@ public class Disguise {
final Player hider; final Player hider;
final Material material; final Material material;
FallingBlock entity; FallingBlock block;
Horse hitBox;
Location solidLocation; Location solidLocation;
boolean solid, solidify; boolean solid, solidify;
static Team hidden;
static {
if(Main.getInstance().supports(9)) {
Scoreboard board = Bukkit.getScoreboardManager().getMainScoreboard();
hidden = board.getTeam("KenshinHideAndSeek_CollisionGroup");
if (hidden == null) {
hidden = board.registerNewTeam("KenshinHideAndSeek_CollisionGroup");
}
hidden.setOption(Team.Option.COLLISION_RULE, Team.OptionStatus.NEVER);
hidden.setCanSeeFriendlyInvisibles(false);
}
}
public Disguise(Player player, Material material){ public Disguise(Player player, Material material){
this.hider = player; this.hider = player;
this.material = material; this.material = material;
this.solid = false; this.solid = false;
respawnEntity(); respawnFallingBlock();
player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 1000000, 0,false, false)); player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 1000000, 0,false, false));
if(Main.getInstance().supports(9)) {
hidden.addEntry(player.getName());
} else {
hider.spigot().setCollidesWithEntities(false);
}
} }
public void remove(){ public void remove(){
if(entity != null) if(block != null)
entity.remove(); block.remove();
if(hitBox != null){
if(Main.getInstance().supports(9))
hidden.removeEntry(hitBox.getUniqueId().toString());
hitBox.remove();
}
if(solid) if(solid)
sendBlockUpdate(Material.AIR); sendBlockUpdate(Material.AIR);
hider.removePotionEffect(PotionEffectType.INVISIBILITY); hider.removePotionEffect(PotionEffectType.INVISIBILITY);
if(Main.getInstance().supports(9)) {
hidden.removeEntry(hider.getName());
} else {
hider.spigot().setCollidesWithEntities(true);
} }
@Nullable
public Location getSolidLocation() {
return solidLocation;
} }
public int getEntityID() { public int getEntityID() {
if(entity == null) return -1; if(block == null) return -1;
return entity.getEntityId(); return block.getEntityId();
}
public int getHitBoxID() {
if(hitBox == null) return -1;
return hitBox.getEntityId();
} }
public Player getPlayer() { public Player getPlayer() {
return hider; return hider;
} }
public boolean isSolid(){
return solid;
}
public void update(){ public void update(){
if(entity == null || entity.isDead()){ if(block == null || block.isDead()){
if(entity != null) entity.remove(); if(block != null) block.remove();
respawnEntity(); respawnFallingBlock();
} }
if(solidify){ if(solidify){
if(!solid) if(!solid) {
solidLocation = hider.getLocation().getBlock().getLocation();
solid = true; solid = true;
solidLocation = hider.getLocation().getBlock().getLocation();
respawnHotbox();
teleportEntity(hitBox, false);
}
sendBlockUpdate(material); sendBlockUpdate(material);
} else if(solid){ } else if(solid){
solid = false; solid = false;
if(Main.getInstance().supports(9))
hidden.removeEntry(hitBox.getUniqueId().toString());
hitBox.remove();
hitBox = null;
sendBlockUpdate(Material.AIR); sendBlockUpdate(Material.AIR);
} }
sendToggleFallingBlock(!solid); toggleEntityVisibility(block, !solid);
sendFallingBlockUpdate(); teleportEntity(block, solid);
} }
public void setSolidify(boolean value){ public void setSolidify(boolean value){
@ -99,16 +131,12 @@ public class Disguise {
}); });
} }
private void sendFallingBlockUpdate() { private void teleportEntity(Entity entity, boolean center) {
if(entity == null || entity.isDead()){
if(entity != null) entity.remove();
respawnEntity();
}
final PacketContainer packet = protocolManager.createPacket(PacketType.Play.Server.ENTITY_TELEPORT); final PacketContainer packet = protocolManager.createPacket(PacketType.Play.Server.ENTITY_TELEPORT);
Location location = hider.getLocation(); Location location = hider.getLocation();
packet.getModifier().writeDefaults(); packet.getModifier().writeDefaults();
packet.getIntegers().write(0, entity.getEntityId()); packet.getIntegers().write(0, entity.getEntityId());
if(solid){ if(center){
packet.getDoubles().write(0, Math.round(location.getX()+.5)-.5); packet.getDoubles().write(0, Math.round(location.getX()+.5)-.5);
packet.getDoubles().write(1, (double)Math.round(location.getY())); packet.getDoubles().write(1, (double)Math.round(location.getY()));
packet.getDoubles().write(2, Math.round(location.getZ()+.5)-.5); packet.getDoubles().write(2, Math.round(location.getZ()+.5)-.5);
@ -124,7 +152,8 @@ public class Disguise {
}); });
} }
private void sendToggleFallingBlock(boolean show){ private void toggleEntityVisibility(Entity entity, boolean show){
if(entity == null) return;
Bukkit.getOnlinePlayers().forEach(receiver -> { Bukkit.getOnlinePlayers().forEach(receiver -> {
if(receiver == hider) return; if(receiver == hider) return;
if(show) if(show)
@ -134,10 +163,24 @@ public class Disguise {
}); });
} }
private void respawnEntity(){ private void respawnFallingBlock(){
entity = hider.getLocation().getWorld().spawnFallingBlock(hider.getLocation(), material, (byte)0); block = hider.getLocation().getWorld().spawnFallingBlock(hider.getLocation(), material, (byte)0);
entity.setGravity(false); block.setGravity(false);
entity.setDropItem(false); block.setDropItem(false);
block.setInvulnerable(true);
}
private void respawnHotbox(){
hitBox = (Horse) hider.getLocation().getWorld().spawnEntity(hider.getLocation().add(0, 1000, 0), EntityType.HORSE);
hitBox.setAI(false);
hitBox.setGravity(false);
hitBox.setInvulnerable(true);
hitBox.setCanPickupItems(false);
hitBox.setCollidable(false);
hitBox.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 1000000, 0,false, false));
if(Main.getInstance().supports(9)){
hidden.addEntry(hitBox.getUniqueId().toString());
}
} }
} }