Skip to content

Commit

Permalink
Added a new packet
Browse files Browse the repository at this point in the history
  • Loading branch information
North-West-Wind authored Dec 6, 2020
1 parent 042879b commit a4fba4a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.client.Minecraft;
import net.minecraft.enchantment.FrostWalkerEnchantment;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
Expand All @@ -35,6 +36,7 @@
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.event.entity.living.*;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.entity.player.PlayerXpEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import org.lwjgl.glfw.GLFW;
Expand All @@ -44,6 +46,14 @@
public class MoreBootsHandler {
private static final Random rng = new Random();

@SubscribeEvent
@OnlyIn(Dist.CLIENT)
public void onPlayerLeftClick(PlayerInteractEvent.LeftClickEmpty event) {
PlayerEntity player = event.getPlayer();
ItemStack boots = player.getItemStackFromSlot(EquipmentSlotType.FEET);
if (boots.getItem().equals(ItemInit.KA_BOOTS)) MoreBootsPacketHandler.INSTANCE.sendToServer(new CPlayerKAPacket());
}

@SubscribeEvent
public void onLivingFall(final LivingFallEvent event) {
LivingEntity entity = event.getEntityLiving();
Expand Down Expand Up @@ -336,6 +346,12 @@ public void onLivingUpdate(final LivingEvent.LivingUpdateEvent event) {
Vector3d direction = entity.getLookVec().scale(0.15);
entity.setMotion(motion.mul(1.01, 1, 1.01).add(direction));
if (rng.nextInt(100) == 0) boots.damageItem(1, entity, entity1 -> entity1.playSound(SoundEvents.ENTITY_ITEM_BREAK, 1, 1));
} else if(boots.getItem().equals(ItemInit.BAT_BOOTS)) {
Vector3d upper = entity.getPositionVec().add(0, entity.getHeight(), 0);
boolean climable = !entity.func_233570_aj_() && !entity.world.isAirBlock(new BlockPos(upper)) && !entity.isSneaking();
Vector3d motion = entity.getMotion();
if (climable) entity.setMotion(motion.mul(1, 0, 1));
if(climable && rng.nextInt(100) == 0) boots.damageItem(1, entity, entity1 -> entity1.playSound(SoundEvents.ENTITY_ITEM_BREAK, 1,1));
}
}

Expand Down Expand Up @@ -373,6 +389,7 @@ public void onLivingDrop(final LivingDropsEvent event) {
int shouldDrop = rng.nextInt((1 + looting) * 2) + looting;
if (shouldDrop < 1) return;
LivingEntity entity = event.getEntityLiving();
if (!entity.getType().equals(EntityType.BAT)) return;
ItemStack stack = new ItemStack(ItemInit.BAT_HIDE, shouldDrop);
ItemEntity item = new ItemEntity(entity.world, entity.getPosX(), entity.getPosY(), entity.getPosZ(), stack);
event.getDrops().add(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ public static void registerPackets() {
ctx.get().enqueueWork(() -> msg.handle(ctx.get()));
ctx.get().setPacketHandled(true);
});
INSTANCE.registerMessage(ID++, CPlayerKAPacket.class, (packet, packetBuffer) -> packetBuffer.writeByteArray(Utils.objToBytes(packet)), packetBuffer -> (CPlayerKAPacket) Utils.bytesToObj(packetBuffer.readByteArray()), (msg, ctx) -> {
ctx.get().enqueueWork(() -> msg.handle(ctx.get()));
ctx.get().setPacketHandled(true);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.northwestwind.moreboots.handler.packet;

import com.northwestwind.moreboots.init.ItemInit;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EntityPredicates;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.network.NetworkDirection;
import net.minecraftforge.fml.network.NetworkEvent;

import java.io.Serializable;
import java.util.List;

public class CPlayerKAPacket implements Serializable {
public void handle(final NetworkEvent.Context ctx) {
if (!ctx.getDirection().equals(NetworkDirection.PLAY_TO_SERVER)) return;
ServerPlayerEntity player = ctx.getSender();
if(player == null) return;
ItemStack boots = player.getItemStackFromSlot(EquipmentSlotType.FEET);
if(!boots.getItem().equals(ItemInit.KA_BOOTS)) return;
BlockPos pos = new BlockPos(player.getPositionVec());
AxisAlignedBB area = new AxisAlignedBB(pos).expand(8, 8, 8);
List<Entity> collidedEntities = player.world.getEntitiesInAABBexcluding(player, area, EntityPredicates.NOT_SPECTATING);
LivingEntity closest = null;
for (Entity entity : collidedEntities) {
if (!(entity instanceof LivingEntity)) continue;
if (closest == null || closest.getPositionVec().distanceTo(player.getPositionVec()) > entity.getPositionVec().distanceTo(player.getPositionVec())) closest = (LivingEntity) entity;
}
if (closest == null) return;
player.attackTargetEntityWithCurrentItem(closest);
}
}

0 comments on commit a4fba4a

Please sign in to comment.