Skip to content

Commit

Permalink
Fix shield knockback not working on players (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
KnightMiner committed Dec 15, 2019
1 parent a534aa5 commit e4c284a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
@@ -1,7 +1,9 @@
package knightminer.inspirations.common.network;

import knightminer.inspirations.Inspirations;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.network.IPacket;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
Expand All @@ -28,20 +30,52 @@ public void setup() {
registerPacket(MilkablePacket.class, MilkablePacket::encode, MilkablePacket::decode, MilkablePacket::handle);
}

/**
* Sends a packet to all players on the network
* @param packet Packet
*/
public static void sendToAll(AbstractPacket packet) {
instance.network.send(PacketDistributor.ALL.noArg(), packet);
}

/**
*
* Sends a packet to a specific player
* @param packet Packet
* @param player Player receiving packet
*/
public static void sendTo(AbstractPacket packet, ServerPlayerEntity player) {
instance.network.send(PacketDistributor.PLAYER.with(() -> player), packet);
}

/**
* Sends a vanilla packet to a player
* @param player Player receiving packet
* @param packet Packet
*/
public static void sendPacket(Entity player, IPacket<?> packet) {
if(player instanceof ServerPlayerEntity && ((ServerPlayerEntity) player).connection != null) {
((ServerPlayerEntity) player).connection.sendPacket(packet);
}
}

/**
* Sends a packet to all clients near a location
* @param world World, does nothing if not a WorldServer
* @param pos Players too far from this position will not receive the packet
* @param packet Packet
*/
public static void sendToClients(World world, BlockPos pos, AbstractPacket packet) {
if(world instanceof ServerWorld) {
sendToClients((ServerWorld)world, pos, packet);
}
}

/**
* Sends a packet to all clients near a location
* @param world World
* @param pos Players too far from this position will not receive the packet
* @param packet Packet
*/
public static void sendToClients(ServerWorld world, BlockPos pos, AbstractPacket packet) {
Chunk chunk = world.getChunkAt(pos);
instance.network.send(PacketDistributor.TRACKING_CHUNK.with(() -> chunk), packet);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/knightminer/inspirations/tools/ToolsEvents.java
@@ -1,6 +1,7 @@
package knightminer.inspirations.tools;

import knightminer.inspirations.common.Config;
import knightminer.inspirations.common.network.InspirationsNetwork;
import knightminer.inspirations.library.InspirationsRegistry;
import knightminer.inspirations.library.Util;
import knightminer.inspirations.tools.item.WaypointCompassItem;
Expand All @@ -14,11 +15,13 @@
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.DyeColor;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.ShearsItem;
import net.minecraft.network.play.server.SEntityVelocityPacket;
import net.minecraft.tileentity.LockableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
Expand Down Expand Up @@ -284,6 +287,9 @@ public static void onShieldHit(LivingAttackEvent event) {
if (knockback > 0) {
if (attacker instanceof LivingEntity) {
((LivingEntity)attacker).knockBack(target, knockback * 0.5F, MathHelper.sin(target.rotationYaw * 0.017453292F), -MathHelper.cos(target.rotationYaw * 0.017453292F));
if (attacker instanceof ServerPlayerEntity) {
InspirationsNetwork.sendPacket(attacker, new SEntityVelocityPacket(attacker));
}
} else {
attacker.addVelocity(-MathHelper.sin(target.rotationYaw * 0.017453292F) * knockback * 0.5f, 0.1D, MathHelper.cos(target.rotationYaw * 0.017453292F) * knockback * 0.5f);
}
Expand Down

0 comments on commit e4c284a

Please sign in to comment.