Skip to content

Commit

Permalink
1.20.4
Browse files Browse the repository at this point in the history
  • Loading branch information
seymourimadeit committed Feb 18, 2024
1 parent 89a2567 commit 2c16b11
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 85 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ minecraft_version=1.20.4
# as they do not follow standard versioning conventions.
minecraft_version_range=[1.20.4,1.21)
# The Neo version must agree with the Minecraft version to get a valid artifact
neo_version=20.4.47-beta
neo_version=20.4.161-beta
# The Neo version range can use any version of Neo as bounds
neo_version_range=[20.4,)
# The loader version range can only use the major version of FML as bounds
Expand Down
14 changes: 0 additions & 14 deletions src/main/java/tallestegg/guardvillagers/GuardPacketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,16 @@

import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
import net.neoforged.neoforge.network.NetworkRegistry;
import net.neoforged.neoforge.network.simple.SimpleChannel;
import tallestegg.guardvillagers.client.gui.GuardInventoryScreen;
import tallestegg.guardvillagers.entities.Guard;
import tallestegg.guardvillagers.entities.GuardContainer;
import tallestegg.guardvillagers.networking.GuardFollowPacket;
import tallestegg.guardvillagers.networking.GuardOpenInventoryPacket;
import tallestegg.guardvillagers.networking.GuardSetPatrolPosPacket;

public class GuardPacketHandler {
private static final String PROTOCOL_VERSION = "1";
public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(new ResourceLocation(GuardVillagers.MODID, "main"), () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals);

public static void registerPackets() {
INSTANCE.messageBuilder(GuardOpenInventoryPacket.class, 0).encoder(GuardOpenInventoryPacket::encode).decoder(GuardOpenInventoryPacket::decode).consumerMainThread(GuardOpenInventoryPacket::handle).add();
INSTANCE.messageBuilder(GuardFollowPacket.class, 1).encoder(GuardFollowPacket::encode).decoder(GuardFollowPacket::decode).consumerNetworkThread(GuardFollowPacket::handle).add();
INSTANCE.messageBuilder(GuardSetPatrolPosPacket.class, 2).encoder(GuardSetPatrolPosPacket::encode).decoder(GuardSetPatrolPosPacket::decode).consumerNetworkThread(GuardSetPatrolPosPacket::handle).add();
}

@SuppressWarnings("resource")
@OnlyIn(Dist.CLIENT)
public static void openGuardInventory(GuardOpenInventoryPacket packet) {
Expand Down
21 changes: 18 additions & 3 deletions src/main/java/tallestegg/guardvillagers/GuardVillagers.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package tallestegg.guardvillagers;

import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.raid.Raid;
Expand All @@ -11,16 +14,19 @@
import net.neoforged.fml.ModLoadingContext;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.config.ModConfig;
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;
import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent;
import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext;
import net.neoforged.fml.loading.FMLPaths;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.BuildCreativeModeTabContentsEvent;
import net.neoforged.neoforge.event.entity.EntityAttributeCreationEvent;
import net.neoforged.neoforge.network.event.RegisterPayloadHandlerEvent;
import net.neoforged.neoforge.network.registration.IPayloadRegistrar;
import tallestegg.guardvillagers.client.GuardSounds;
import tallestegg.guardvillagers.configuration.GuardConfig;
import tallestegg.guardvillagers.entities.Guard;
import tallestegg.guardvillagers.networking.GuardFollowPacket;
import tallestegg.guardvillagers.networking.GuardOpenInventoryPacket;
import tallestegg.guardvillagers.networking.GuardSetPatrolPosPacket;

@Mod(GuardVillagers.MODID)
public class GuardVillagers {
Expand All @@ -38,7 +44,16 @@ public GuardVillagers(IEventBus modEventBus, Dist dist) {
GuardSounds.SOUNDS.register(modEventBus);
modEventBus.addListener(this::addAttributes);
modEventBus.addListener(this::addCreativeTabs);
GuardPacketHandler.registerPackets();
modEventBus.addListener(this::register);
}


private void register(final RegisterPayloadHandlerEvent event) {
final IPayloadRegistrar reg = event.registrar(MODID).versioned("2.0.1");
reg.play(GuardSetPatrolPosPacket.ID, GuardSetPatrolPosPacket::new, payload -> payload.server(GuardSetPatrolPosPacket::handle));
reg.play(GuardOpenInventoryPacket.ID, GuardOpenInventoryPacket::new, payload -> payload.client(GuardOpenInventoryPacket::handle));
reg.play(GuardFollowPacket.ID, GuardFollowPacket::new, payload -> payload.server(GuardFollowPacket::handle));

}

public static boolean hotvChecker(Player player, Guard guard) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.neoforged.neoforge.network.PacketDistributor;
import tallestegg.guardvillagers.GuardPacketHandler;
import tallestegg.guardvillagers.GuardVillagers;
import tallestegg.guardvillagers.configuration.GuardConfig;
Expand Down Expand Up @@ -47,13 +48,13 @@ public void init() {
super.init();
if (GuardConfig.followHero && player.hasEffect(MobEffects.HERO_OF_THE_VILLAGE) || !GuardConfig.followHero) {
this.addRenderableWidget(new GuardGuiButton(this.leftPos + 100, this.height / 2 - 40, 20, 18, GUARD_FOLLOWING_ICONS, GUARD_NOT_FOLLOWING_ICONS, true, (p_214086_1_) -> {
GuardPacketHandler.INSTANCE.sendToServer(new GuardFollowPacket(guard.getId()));
PacketDistributor.SERVER.noArg().send(new GuardFollowPacket(guard.getId()));
}));
}
if (GuardConfig.setGuardPatrolHotv && player.hasEffect(MobEffects.HERO_OF_THE_VILLAGE) || !GuardConfig.setGuardPatrolHotv) {
this.addRenderableWidget(new GuardGuiButton(this.leftPos + 120, this.height / 2 - 40, 20, 18, GUARD_PATROLLING_ICONS, GUARD_NOT_PATROLLING_ICONS, false, (p_214086_1_) -> {
buttonPressed = !buttonPressed;
GuardPacketHandler.INSTANCE.sendToServer(new GuardSetPatrolPosPacket(guard.getId(), buttonPressed));
PacketDistributor.SERVER.noArg().send(new GuardSetPatrolPosPacket(guard.getId(), buttonPressed));
}));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ public void openGui(ServerPlayer player) {
}
this.interacting = true;
player.nextContainerCounter();
GuardPacketHandler.INSTANCE.send(PacketDistributor.PLAYER.with(() -> player), new GuardOpenInventoryPacket(player.containerCounter, this.guardInventory.getContainerSize(), this.getId()));
PacketDistributor.PLAYER.with(player).send(new GuardOpenInventoryPacket(player.containerCounter, this.guardInventory.getContainerSize(), this.getId()));
player.containerMenu = new GuardContainer(player.containerCounter, player.getInventory(), this.guardInventory, this);
player.initMenu(player.containerMenu);
NeoForge.EVENT_BUS.post(new PlayerContainerEvent.Open(player, player.containerMenu));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,54 @@
package tallestegg.guardvillagers.networking;

import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.Entity;
import net.neoforged.neoforge.network.NetworkEvent;
import net.neoforged.neoforge.network.handling.PlayPayloadContext;
import tallestegg.guardvillagers.GuardVillagers;
import tallestegg.guardvillagers.entities.Guard;

import java.util.function.Supplier;

public class GuardFollowPacket{
public class GuardFollowPacket implements CustomPacketPayload {
public static final ResourceLocation ID = new ResourceLocation(GuardVillagers.MODID, "set_following_packet");
private final int entityId;

public GuardFollowPacket(int entityId) {
this.entityId = entityId;
}

public static GuardFollowPacket decode(FriendlyByteBuf buf) {
return new GuardFollowPacket(buf.readInt());
}

public static void encode(GuardFollowPacket msg, FriendlyByteBuf buf) {
buf.writeInt(msg.entityId);
public GuardFollowPacket(FriendlyByteBuf buf) {
this.entityId = buf.readInt();
}

public int getEntityId() {
return this.entityId;
}

public void handle(NetworkEvent.Context ctx) {
ctx.enqueueWork(() -> {
ctx.enqueueWork(new Runnable() {
@Override
public void run() {
ServerPlayer player = ctx.getSender();
if (player != null && player.level() instanceof ServerLevel) {
Entity entity = player.level().getEntity(getEntityId());
if (entity instanceof Guard) {
Guard guard = (Guard) entity;
guard.setFollowing(!guard.isFollowing());
guard.setOwnerId(player.getUUID());
guard.playSound(SoundEvents.VILLAGER_YES, 1.0F, 1.0F);
}
}
}
});
public void handle(PlayPayloadContext ctx) {
ctx.workHandler().execute(() -> {
ServerPlayer player = (ServerPlayer) ctx.player().orElseThrow();
if (player != null && player.level() instanceof ServerLevel) {
Entity entity = player.level().getEntity(getEntityId());
if (entity instanceof Guard) {
Guard guard = (Guard) entity;
guard.setFollowing(!guard.isFollowing());
guard.setOwnerId(player.getUUID());
guard.playSound(SoundEvents.VILLAGER_YES, 1.0F, 1.0F);
}
}
});
ctx.setPacketHandled(true);
}

@Override
public void write(FriendlyByteBuf buf) {
buf.writeInt(this.entityId);
}

@Override
public ResourceLocation id() {
return ID;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package tallestegg.guardvillagers.networking;

import net.minecraft.network.FriendlyByteBuf;
import net.neoforged.neoforge.network.NetworkEvent;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.network.handling.PlayPayloadContext;
import tallestegg.guardvillagers.GuardPacketHandler;
import tallestegg.guardvillagers.GuardVillagers;

public class GuardOpenInventoryPacket {
public class GuardOpenInventoryPacket implements CustomPacketPayload {
public static final ResourceLocation ID = new ResourceLocation(GuardVillagers.MODID, "open_inventory_packet");
private final int id;
private final int size;
private final int entityId;
Expand All @@ -14,33 +18,45 @@ public GuardOpenInventoryPacket(int id, int size, int entityId) {
this.size = size;
this.entityId = entityId;
}


public GuardOpenInventoryPacket(FriendlyByteBuf buf) {
this.id = buf.readUnsignedByte();
this.size = buf.readVarInt();
this.entityId = buf.readInt();
}

public static GuardOpenInventoryPacket decode(FriendlyByteBuf buf) {
return new GuardOpenInventoryPacket(buf.readUnsignedByte(), buf.readVarInt(), buf.readInt());
}

public static void encode(GuardOpenInventoryPacket msg, FriendlyByteBuf buf) {
buf.writeByte(msg.id);
buf.writeVarInt(msg.size);
buf.writeInt(msg.entityId);
public void write(FriendlyByteBuf buf) {
buf.writeByte(this.id);
buf.writeVarInt(this.size);
buf.writeInt(this.entityId);
}

@Override
public ResourceLocation id() {
return ID;
}

public int getId() {
return this.id;
}

public int getSize() {
return this.size;
}

public int getEntityId() {
return this.entityId;
}

public void handle(NetworkEvent.Context context) {
context.enqueueWork(() -> {
public void handle(PlayPayloadContext context) {
context.workHandler().execute(() -> {
GuardPacketHandler.openGuardInventory(this);
});
context.setPacketHandled(true);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,37 @@

import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.neoforged.neoforge.network.NetworkEvent;
import net.minecraft.world.entity.player.Player;
import net.neoforged.neoforge.network.handling.PlayPayloadContext;
import tallestegg.guardvillagers.GuardVillagers;
import tallestegg.guardvillagers.entities.Guard;

public class GuardSetPatrolPosPacket {
public class GuardSetPatrolPosPacket implements CustomPacketPayload {
public static final ResourceLocation ID = new ResourceLocation(GuardVillagers.MODID, "set_patrol_packet");
private final int entityId;
private boolean pressed;


public GuardSetPatrolPosPacket(int entityId, boolean pressed) {
this.pressed = pressed;
this.entityId = entityId;
}

public static GuardSetPatrolPosPacket decode(FriendlyByteBuf buf) {
return new GuardSetPatrolPosPacket(buf.readInt(), buf.readBoolean());
public GuardSetPatrolPosPacket(FriendlyByteBuf buf) {
this.entityId = buf.readInt();
this.pressed = buf.readBoolean();
}

public static void encode(GuardSetPatrolPosPacket msg, FriendlyByteBuf buf) {
buf.writeInt(msg.entityId);
buf.writeBoolean(msg.pressed);
public void write(FriendlyByteBuf buf) {
buf.writeInt(this.entityId);
buf.writeBoolean(this.pressed);
}


public int getEntityId() {
return this.entityId;
}
Expand All @@ -38,21 +45,26 @@ public void setPressed(boolean pressed) {
this.pressed = pressed;
}

public void handle(NetworkEvent.Context context) {
context.enqueueWork(() -> {
ServerPlayer player = context.getSender();
if (player != null && player.level() instanceof ServerLevel) {
Entity entity = player.level().getEntity(this.getEntityId());
if (entity instanceof Guard) {
Guard guard = (Guard) entity;
BlockPos pos = this.isPressed() ? null : guard.blockPosition();
if (guard.blockPosition() != null)
guard.setPatrolPos(pos);
guard.setPatrolling(!this.isPressed());
this.setPressed(!this.isPressed());
}
public void handle(PlayPayloadContext context) {
context.workHandler().execute(() -> setPatrolPosition(context.player().orElseThrow(), this));
}

@Override
public ResourceLocation id() {
return ID;
}

public static void setPatrolPosition(Player player, GuardSetPatrolPosPacket packet) {
if (player != null && player.level() instanceof ServerLevel) {
Entity entity = player.level().getEntity(packet.getEntityId());
if (entity instanceof Guard) {
Guard guard = (Guard) entity;
BlockPos pos = packet.isPressed() ? null : guard.blockPosition();
if (guard.blockPosition() != null)
guard.setPatrolPos(pos);
guard.setPatrolling(!packet.isPressed());
packet.setPressed(!packet.isPressed());
}
});
context.setPacketHandled(true);
}
}
}

0 comments on commit 2c16b11

Please sign in to comment.