Refactor and 1.17-pre1 (#96)

* Fix #94

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Add Chunk data save and load events, closes #89 (#92)

* Created Chunk save and load event. Closes #89

* Fixed missing licence

* Update common/src/main/java/me/shedaniel/architectury/event/events/ChunkEvent.java

* Update common/src/main/java/me/shedaniel/architectury/event/events/ChunkEvent.java

* Update common/src/main/java/me/shedaniel/architectury/event/events/ChunkEvent.java

* Supply ServerLevel in ChunkEvent.LOAD, style cleanup

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Add "Data" suffix to Chunk IO Events and mark level as nullable for load

* Update common/src/main/java/me/shedaniel/architectury/event/events/ChunkEvent.java

* Bump to 1.16

Co-authored-by: shedaniel <daniel@shedaniel.me>
Co-authored-by: Max <maxh2709@gmail.com>

* Update pr.yml

* Add NetworkManager hook for Entity spawn packets (#88)

* New utility hook for creating a entity spawn packet. before every mod had to implement this in itself.

* Apply suggestions from code review

Co-authored-by: shedaniel <daniel@shedaniel.me>

* Update common/src/main/java/me/shedaniel/architectury/networking/NetworkManager.java

* Update common/src/main/java/me/shedaniel/architectury/networking/NetworkManager.java

* Properly implement SpawnEntityPacket & Format style

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Format

Signed-off-by: shedaniel <daniel@shedaniel.me>

* createEntitySpawnPacket -> createAddEntityPacket for mojmap consistency

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Bump version to 1.17

Co-authored-by: Max <maxh2709@gmail.com>
Co-authored-by: shedaniel <daniel@shedaniel.me>

* Add Javadocs for all events (#91)

* javadoc for ClientChatEvent

* javadoc for ClientPlayerEvent

* javadoc for ClientRawInputEvent

* Added javadoc for ClientScreenInputEvent

* Bit refactoring and removed javadoc @see to the caller methods

* Add javadoc for BlockEvent

* Add javadoc for ChatEvent

* Add javadoc for CommandPerformEvent and CommandRegistrationEvent

* Add javadoc for EntityEvent

* Add javadoc for ExplosionEvent

* Add javadoc for GuiEvent

* Add javadoc for InteractionEvent

* Add javadoc for LightningEvent, RecipeUpdateEvent & TextureStitchEvent

* Add javadoc for LifecycleEvent & ClientLifecycleEvent

* Add javadoc for TooltipEvent

* Add javadoc for TickEvent and partly PlayerEvent

* More javadoc for PlayerEvent

* Finally all event have javadoc

* Update common/src/main/java/me/shedaniel/architectury/event/events/CommandPerformEvent.java

Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>

* Update common/src/main/java/me/shedaniel/architectury/event/events/CommandPerformEvent.java

Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>

* Update common/src/main/java/me/shedaniel/architectury/event/events/CommandPerformEvent.java

Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>

* Update common/src/main/java/me/shedaniel/architectury/event/events/ChatEvent.java

Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>

* Update common/src/main/java/me/shedaniel/architectury/event/events/CommandRegistrationEvent.java

Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>

* Update common/src/main/java/me/shedaniel/architectury/event/events/EntityEvent.java

Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>

* Update common/src/main/java/me/shedaniel/architectury/event/events/EntityEvent.java

Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>

* Update common/src/main/java/me/shedaniel/architectury/event/events/EntityEvent.java

Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>

* Update common/src/main/java/me/shedaniel/architectury/event/events/EntityEvent.java

Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>

* Update common/src/main/java/me/shedaniel/architectury/event/events/client/ClientScreenInputEvent.java

Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>

* Update common/src/main/java/me/shedaniel/architectury/event/events/client/ClientScreenInputEvent.java

Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>

* Update common/src/main/java/me/shedaniel/architectury/event/events/BlockEvent.java

Co-authored-by: Max <maxh2709@gmail.com>

* Update common/src/main/java/me/shedaniel/architectury/event/events/BlockEvent.java

Co-authored-by: Max <maxh2709@gmail.com>

* Update common/src/main/java/me/shedaniel/architectury/event/events/BlockEvent.java

Co-authored-by: Max <maxh2709@gmail.com>

* Update common/src/main/java/me/shedaniel/architectury/event/events/BlockEvent.java

Co-authored-by: Max <maxh2709@gmail.com>

* Update common/src/main/java/me/shedaniel/architectury/event/events/LifecycleEvent.java

Co-authored-by: Max <maxh2709@gmail.com>

* Update common/src/main/java/me/shedaniel/architectury/event/events/LifecycleEvent.java

Co-authored-by: Max <maxh2709@gmail.com>

* Update common/src/main/java/me/shedaniel/architectury/event/events/LifecycleEvent.java

Co-authored-by: Max <maxh2709@gmail.com>

* Update common/src/main/java/me/shedaniel/architectury/event/events/PlayerEvent.java

Co-authored-by: Max <maxh2709@gmail.com>

* Apply suggestions from code review

Co-authored-by: Max <maxh2709@gmail.com>
Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>
Co-authored-by: Max <maxh2709@gmail.com>

* Apply suggestions from code review

Co-authored-by: Max <maxh2709@gmail.com>
Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Max <maxh2709@gmail.com>
Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Max <maxh2709@gmail.com>
Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: shedaniel <daniel@shedaniel.me>

* Applied some more reviews

* Applied some more reviews

* Applied suggestions

* Reformat and apply some minor suggestions

* Apply suggestions from review (first batch)

* Remove double return javadoc

* Apply suggestions from code review

Co-authored-by: shedaniel <daniel@shedaniel.me>

* Add "scaled" to mouse coordinates

* Final touches

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Remove invalid tips

Signed-off-by: shedaniel <daniel@shedaniel.me>

Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>
Co-authored-by: Max <maxh2709@gmail.com>
Co-authored-by: shedaniel <daniel@shedaniel.me>

* Refactor

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Update common/src/main/java/dev/architectury/event/events/common/InteractionEvent.java

Co-authored-by: Max <maxh2709@gmail.com>

* Apply suggestions from code review

Co-authored-by: Max <maxh2709@gmail.com>

* attack -> hurt

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Update @param for InteractionEvent#FARMLAND_TRAMPLE

Signed-off-by: shedaniel <daniel@shedaniel.me>

* world -> level

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Relocate hooks

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Organize imports

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Registry -> Registrar Please voice out your opinion!!

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Some more renames
itemOptional -> optionalItem
RegistryBuilder -> RegistrarBuilder
RegistryOption -> RegistrarOption
Not forcing RegistryEntry on new registries, vanilla types don't have them

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Java 16-ification

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Optimise imports after the var change

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Un-minimize architectury-common.mixins.json

Signed-off-by: shedaniel <daniel@shedaniel.me>

Co-authored-by: canitzp <12819060+canitzp@users.noreply.github.com>
Co-authored-by: Max <maxh2709@gmail.com>
Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>
This commit is contained in:
shedaniel
2021-05-30 19:08:11 +08:00
committed by GitHub
parent 28867f012d
commit 8b262d07c0
229 changed files with 3529 additions and 2354 deletions

View File

@@ -0,0 +1,124 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021 architectury
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package dev.architectury.networking;
import com.google.common.collect.Maps;
import dev.architectury.networking.NetworkManager.PacketContext;
import dev.architectury.platform.Platform;
import dev.architectury.utils.Env;
import io.netty.buffer.Unpooled;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.Packet;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* Forge {@code SimpleChannel} like network wrapper of {@link NetworkManager}.
*/
public final class NetworkChannel {
private final ResourceLocation id;
private final Map<Class<?>, MessageInfo<?>> encoders = Maps.newHashMap();
private NetworkChannel(ResourceLocation id) {
this.id = id;
}
public static NetworkChannel create(ResourceLocation id) {
return new NetworkChannel(id);
}
public <T> void register(Class<T> type, BiConsumer<T, FriendlyByteBuf> encoder, Function<FriendlyByteBuf, T> decoder, BiConsumer<T, Supplier<PacketContext>> messageConsumer) {
// TODO: this is pretty wasteful; add a way to specify custom or numeric ids
var s = UUID.nameUUIDFromBytes(type.getName().getBytes(StandardCharsets.UTF_8)).toString().replace("-", "");
var info = new MessageInfo<T>(new ResourceLocation(id + "/" + s), encoder, decoder, messageConsumer);
encoders.put(type, info);
NetworkManager.NetworkReceiver receiver = (buf, context) -> {
info.messageConsumer.accept(info.decoder.apply(buf), () -> context);
};
NetworkManager.registerReceiver(NetworkManager.c2s(), info.packetId, receiver);
if (Platform.getEnvironment() == Env.CLIENT) {
NetworkManager.registerReceiver(NetworkManager.s2c(), info.packetId, receiver);
}
}
public static long hashCodeString(String str) {
long h = 0;
var length = str.length();
for (var i = 0; i < length; i++) {
h = 31 * h + str.charAt(i);
}
return h;
}
public <T> Packet<?> toPacket(NetworkManager.Side side, T message) {
var messageInfo = (MessageInfo<T>) Objects.requireNonNull(encoders.get(message.getClass()), "Unknown message type! " + message);
var buf = new FriendlyByteBuf(Unpooled.buffer());
messageInfo.encoder.accept(message, buf);
return NetworkManager.toPacket(side, messageInfo.packetId, buf);
}
public <T> void sendToPlayer(ServerPlayer player, T message) {
Objects.requireNonNull(player, "Unable to send packet to a 'null' player!").connection.send(toPacket(NetworkManager.s2c(), message));
}
public <T> void sendToPlayers(Iterable<ServerPlayer> players, T message) {
var packet = toPacket(NetworkManager.s2c(), message);
for (var player : players) {
Objects.requireNonNull(player, "Unable to send packet to a 'null' player!").connection.send(packet);
}
}
@Environment(EnvType.CLIENT)
public <T> void sendToServer(T message) {
if (Minecraft.getInstance().getConnection() != null) {
Minecraft.getInstance().getConnection().send(toPacket(NetworkManager.c2s(), message));
} else {
throw new IllegalStateException("Unable to send packet to the server while not in game!");
}
}
@Environment(EnvType.CLIENT)
public <T> boolean canServerReceive(Class<T> type) {
return NetworkManager.canServerReceive(encoders.get(type).packetId);
}
public <T> boolean canPlayerReceive(ServerPlayer player, Class<T> type) {
return NetworkManager.canPlayerReceive(player, encoders.get(type).packetId);
}
private record MessageInfo<T>(
ResourceLocation packetId,
BiConsumer<T, FriendlyByteBuf> encoder,
Function<FriendlyByteBuf, T> decoder,
BiConsumer<T, Supplier<PacketContext>> messageConsumer
) {
}
}

View File

@@ -0,0 +1,129 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021 architectury
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package dev.architectury.networking;
import dev.architectury.injectables.annotations.ExpectPlatform;
import dev.architectury.utils.Env;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.Packet;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import java.util.Objects;
public final class NetworkManager {
@ExpectPlatform
public static void registerReceiver(Side side, ResourceLocation id, NetworkReceiver receiver) {
throw new AssertionError();
}
@ExpectPlatform
public static Packet<?> toPacket(Side side, ResourceLocation id, FriendlyByteBuf buf) {
throw new AssertionError();
}
public static void sendToPlayer(ServerPlayer player, ResourceLocation id, FriendlyByteBuf buf) {
Objects.requireNonNull(player, "Unable to send packet to a 'null' player!").connection.send(toPacket(serverToClient(), id, buf));
}
public static void sendToPlayers(Iterable<ServerPlayer> players, ResourceLocation id, FriendlyByteBuf buf) {
var packet = toPacket(serverToClient(), id, buf);
for (var player : players) {
Objects.requireNonNull(player, "Unable to send packet to a 'null' player!").connection.send(packet);
}
}
@Environment(EnvType.CLIENT)
public static void sendToServer(ResourceLocation id, FriendlyByteBuf buf) {
if (Minecraft.getInstance().getConnection() != null) {
Minecraft.getInstance().getConnection().send(toPacket(clientToServer(), id, buf));
} else {
throw new IllegalStateException("Unable to send packet to the server while not in game!");
}
}
@Environment(EnvType.CLIENT)
@ExpectPlatform
public static boolean canServerReceive(ResourceLocation id) {
throw new AssertionError();
}
@ExpectPlatform
public static boolean canPlayerReceive(ServerPlayer player, ResourceLocation id) {
throw new AssertionError();
}
/**
* Easy to use utility method to create an entity spawn packet.
* This packet is needed everytime any mod adds a non-living entity.
* The entity should override {@link Entity#getAddEntityPacket()} to point to this method!
*
* @param entity The entity which should be spawned.
* @return The ready to use packet to spawn the entity on the client.
* @see Entity#getAddEntityPacket()
*/
@ExpectPlatform
public static Packet<?> createAddEntityPacket(Entity entity) {
throw new AssertionError();
}
@FunctionalInterface
public interface NetworkReceiver {
void receive(FriendlyByteBuf buf, PacketContext context);
}
public interface PacketContext {
Player getPlayer();
void queue(Runnable runnable);
Env getEnvironment();
default EnvType getEnv() {
return getEnvironment().toPlatform();
}
}
public static Side s2c() {
return Side.S2C;
}
public static Side c2s() {
return Side.C2S;
}
public static Side serverToClient() {
return Side.S2C;
}
public static Side clientToServer() {
return Side.C2S;
}
public enum Side {
S2C,
C2S
}
}