Skip to content

Instantly share code, notes, and snippets.

@YoungOG
Created December 30, 2018 07:44
Show Gist options
  • Save YoungOG/e3265d98661957abece71594b70d6a01 to your computer and use it in GitHub Desktop.
Save YoungOG/e3265d98661957abece71594b70d6a01 to your computer and use it in GitHub Desktop.
Here's the god damn "magic" kohi knockback, maybe people can stop believing in magic some day and just play the fucking game. Credit to OCN's SportBukkit for the relog fix
knockbackFriction = 2.0D;
knockbackHorizontal = 0.35D;
knockbackVertical = 0.35D;
knockbackVerticalLimit = 0.4D;
knockbackExtraHorizontal = 0.425D;
knockbackExtraVertical = 0.085D;
diff --git a/src/main/java/net/minecraft/server/EntityHuman.java b/src/main/java/net/minecraft/server/EntityHuman.java
index 8bb39ec..4f024cf 100644
--- a/src/main/java/net/minecraft/server/EntityHuman.java
+++ b/src/main/java/net/minecraft/server/EntityHuman.java
@@ -18,6 +18,7 @@ import org.bukkit.event.player.PlayerBedEnterEvent;
import org.bukkit.event.player.PlayerBedLeaveEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
+import org.bukkit.event.player.PlayerVelocityEvent;
// CraftBukkit end
import org.spigotmc.ProtocolData; // Spigot - protocol patch
@@ -947,6 +948,13 @@ public abstract class EntityHuman extends EntityLiving implements ICommandListen
// CraftBukkit end
}
+ // Kohi start
+ // Save the victim's velocity before they are potentially knocked back
+ double victimMotX = entity.motX;
+ double victimMotY = entity.motY;
+ double victimMotZ = entity.motZ;
+ // Kohi end
+
boolean flag2 = entity.damageEntity(DamageSource.playerAttack(this), f);
if (flag2) {
@@ -957,6 +965,31 @@ public abstract class EntityHuman extends EntityLiving implements ICommandListen
this.setSprinting(false);
}
+ // Kohi start
+ // If the attack caused knockback, send the new velocity to the victim's client immediately,
+ // and undo the change. Otherwise, if movement packets from the victim are processed before
+ // the end of the tick, then friction may reduce the velocity considerably before it's sent
+ // to the client, particularly if the victim was standing on the ground when those packets
+ // were generated. And because this glitch is also likely to make server-side velocity very
+ // inconsistent, we simply reverse the knockback after sending it so that KB, like most other
+ // things, doesn't affect server velocity at all.
+ if (entity instanceof EntityPlayer && entity.velocityChanged) {
+ EntityPlayer attackedPlayer = (EntityPlayer) entity;
+ PlayerVelocityEvent event = new PlayerVelocityEvent(attackedPlayer.getBukkitEntity(),
+ attackedPlayer.getBukkitEntity().getVelocity());
+ this.world.getServer().getPluginManager().callEvent(event);
+ if (!event.isCancelled()) {
+ attackedPlayer.getBukkitEntity().setVelocityDirect(event.getVelocity());
+ attackedPlayer.playerConnection.sendPacket(new PacketPlayOutEntityVelocity(attackedPlayer));
+ }
+
+ attackedPlayer.velocityChanged = false;
+ attackedPlayer.motX = victimMotX;
+ attackedPlayer.motY = victimMotY;
+ attackedPlayer.motZ = victimMotZ;
+ }
+ // Kohi end
+
if (flag) {
this.b(entity);
}
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
index bcd956f..093d381 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
@@ -53,6 +53,7 @@ import org.bukkit.event.player.PlayerGameModeChangeEvent;
import org.bukkit.event.player.PlayerRegisterChannelEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerUnregisterChannelEvent;
+import org.bukkit.event.player.PlayerVelocityEvent;
import org.bukkit.inventory.InventoryView.Property;
import org.bukkit.map.MapView;
import org.bukkit.metadata.MetadataValue;
@@ -60,6 +61,7 @@ import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.messaging.StandardMessenger;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scoreboard.Scoreboard;
+import org.bukkit.util.Vector;
@DelegateDeserialization(CraftOfflinePlayer.class)
public class CraftPlayer extends CraftHumanEntity implements Player {
@@ -1473,4 +1475,37 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
return kohi;
}
// Kohi end
+
+ // Kohi start
+ @Override
+ public void setVelocity(Vector vel) {
+ // To be consistent with old behavior, set the velocity before firing the event
+ this.setVelocityDirect(vel);
+
+ PlayerVelocityEvent event = new PlayerVelocityEvent(this, vel.clone());
+ this.getServer().getPluginManager().callEvent(event);
+
+ if(!event.isCancelled()) {
+ // Set the velocity again in case it was changed by event handlers
+ this.setVelocityDirect(event.getVelocity());
+
+ // Send the new velocity to the player's client immediately, so it isn't affected by
+ // any movement packets from this player that may be processed before the end of the tick.
+ // Without this, player velocity changes tend to be very inconsistent.
+ this.getHandle().playerConnection.sendPacket(new PacketPlayOutEntityVelocity(this.getHandle()));
+ }
+
+ // Note that cancelling the event does not restore the old velocity, it only prevents
+ // the packet from sending. Again, this is to be consistent with old behavior.
+ }
+
+ public void setVelocityDirect(Vector vel) {
+ entity.motX = vel.getX();
+ entity.motY = vel.getY();
+ entity.motZ = vel.getZ();
+ if (entity.motY > 0) {
+ entity.fallDistance = 0.0f;
+ }
+ }
+ // Kohi end
}
diff --git a/src/main/java/net/minecraft/server/EntityHuman.java b/src/main/java/net/minecraft/server/EntityHuman.java
index 4f024cf..547421b 100644
--- a/src/main/java/net/minecraft/server/EntityHuman.java
+++ b/src/main/java/net/minecraft/server/EntityHuman.java
@@ -21,6 +21,7 @@ import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.event.player.PlayerVelocityEvent;
// CraftBukkit end
import org.spigotmc.ProtocolData; // Spigot - protocol patch
+import org.spigotmc.SpigotConfig;
public abstract class EntityHuman extends EntityLiving implements ICommandListener {
@@ -959,7 +960,12 @@ public abstract class EntityHuman extends EntityLiving implements ICommandListen
if (flag2) {
if (i > 0) {
- entity.g((double) (-MathHelper.sin(this.yaw * 3.1415927F / 180.0F) * (float) i * 0.5F), 0.1D, (double) (MathHelper.cos(this.yaw * 3.1415927F / 180.0F) * (float) i * 0.5F));
+ // Kohi start - configurable knockback
+ entity.g(
+ (double) (-MathHelper.sin(this.yaw * 3.1415927F / 180.0F) * (float) i * SpigotConfig.knockbackExtraHorizontal),
+ SpigotConfig.knockbackExtraVertical,
+ (double) (MathHelper.cos(this.yaw * 3.1415927F / 180.0F) * (float) i * SpigotConfig.knockbackExtraHorizontal));
+ // Kohi end
this.motX *= 0.6D;
this.motZ *= 0.6D;
this.setSprinting(false);
diff --git a/src/main/java/net/minecraft/server/EntityLiving.java b/src/main/java/net/minecraft/server/EntityLiving.java
index 63d22ad..9acf58d 100644
--- a/src/main/java/net/minecraft/server/EntityLiving.java
+++ b/src/main/java/net/minecraft/server/EntityLiving.java
@@ -18,6 +18,7 @@ import org.bukkit.event.entity.EntityRegainHealthEvent;
// CraftBukkit end
import org.bukkit.craftbukkit.SpigotTimings; // Spigot
+import org.spigotmc.SpigotConfig;
public abstract class EntityLiving extends Entity {
@@ -831,18 +832,21 @@ public abstract class EntityLiving extends Entity {
public void a(Entity entity, float f, double d0, double d1) {
if (this.random.nextDouble() >= this.getAttributeInstance(GenericAttributes.c).getValue()) {
this.al = true;
- float f1 = MathHelper.sqrt(d0 * d0 + d1 * d1);
- float f2 = 0.4F;
-
- this.motX /= 2.0D;
- this.motY /= 2.0D;
- this.motZ /= 2.0D;
- this.motX -= d0 / (double) f1 * (double) f2;
- this.motY += (double) f2;
- this.motZ -= d1 / (double) f1 * (double) f2;
- if (this.motY > 0.4000000059604645D) {
- this.motY = 0.4000000059604645D;
+ // Kohi start - configurable knockback
+ double magnitude = MathHelper.sqrt(d0 * d0 + d1 * d1);
+
+ this.motX /= SpigotConfig.knockbackFriction;
+ this.motY /= SpigotConfig.knockbackFriction;
+ this.motZ /= SpigotConfig.knockbackFriction;
+
+ this.motX -= d0 / magnitude * SpigotConfig.knockbackHorizontal;
+ this.motY += SpigotConfig.knockbackVertical;
+ this.motZ -= d1 / magnitude * SpigotConfig.knockbackHorizontal;
+
+ if (this.motY > SpigotConfig.knockbackVerticalLimit) {
+ this.motY = SpigotConfig.knockbackVerticalLimit;
}
+ // Kohi end
}
}
@klirman
Copy link

klirman commented Dec 28, 2019

cock

@Elb1to
Copy link

Elb1to commented Apr 13, 2020

Shroud's fat cock

@speedboost57
Copy link

shroud has fat cock yes

@MWHunter
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment