-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89a2567
commit 2c16b11
Showing
8 changed files
with
117 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 30 additions & 28 deletions
58
src/main/java/tallestegg/guardvillagers/networking/GuardFollowPacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters