Fixed TNT and basic networking.

This commit is contained in:
Sebastian Hartte
2020-06-30 11:30:04 +02:00
parent 3337eb9ae3
commit c0a4762ffc
25 changed files with 593 additions and 454 deletions
@@ -1,105 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 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.
*
* Applied Energistics 2 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 Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.core.sync;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import net.minecraft.network.PacketByteBuf;
import appeng.core.sync.packets.*;
public class BasePacketHandler {
private static final Map<Class<? extends BasePacket>, PacketTypes> REVERSE_LOOKUP = new HashMap<>();
public enum PacketTypes {
PACKET_COMPASS_REQUEST(CompassRequestPacket.class, CompassRequestPacket::new),
PACKET_COMPASS_RESPONSE(CompassResponsePacket.class, CompassResponsePacket::new),
PACKET_INVENTORY_ACTION(InventoryActionPacket.class, InventoryActionPacket::new),
PACKET_ME_INVENTORY_UPDATE(MEInventoryUpdatePacket.class, MEInventoryUpdatePacket::new),
PACKET_ME_FLUID_INVENTORY_UPDATE(MEFluidInventoryUpdatePacket.class, MEFluidInventoryUpdatePacket::new),
PACKET_CONFIG_BUTTON(ConfigButtonPacket.class, ConfigButtonPacket::new),
PACKET_PART_PLACEMENT(PartPlacementPacket.class, PartPlacementPacket::new),
PACKET_LIGHTNING(LightningPacket.class, LightningPacket::new),
PACKET_MATTER_CANNON(MatterCannonPacket.class, MatterCannonPacket::new),
PACKET_MOCK_EXPLOSION(MockExplosionPacket.class, MockExplosionPacket::new),
PACKET_VALUE_CONFIG(ConfigValuePacket.class, ConfigValuePacket::new),
PACKET_ITEM_TRANSITION_EFFECT(ItemTransitionEffectPacket.class, ItemTransitionEffectPacket::new),
PACKET_BLOCK_TRANSITION_EFFECT(BlockTransitionEffectPacket.class, BlockTransitionEffectPacket::new),
PACKET_PROGRESS_VALUE(ProgressBarPacket.class, ProgressBarPacket::new),
PACKET_CLICK(ClickPacket.class, ClickPacket::new),
PACKET_SWITCH_GUIS(SwitchGuisPacket.class, SwitchGuisPacket::new),
PACKET_SWAP_SLOTS(SwapSlotsPacket.class, SwapSlotsPacket::new),
PACKET_PATTERN_SLOT(PatternSlotPacket.class, PatternSlotPacket::new),
PACKET_RECIPE_JEI(JEIRecipePacket.class, JEIRecipePacket::new),
PACKET_TARGET_ITEM(TargetItemStackPacket.class, TargetItemStackPacket::new),
PACKET_TARGET_FLUID(TargetFluidStackPacket.class, TargetFluidStackPacket::new),
PACKET_CRAFTING_REQUEST(CraftRequestPacket.class, CraftRequestPacket::new),
PACKET_ASSEMBLER_ANIMATION(AssemblerAnimationPacket.class, AssemblerAnimationPacket::new),
PACKET_COMPRESSED_NBT(CompressedNBTPacket.class, CompressedNBTPacket::new),
PACKET_PAINTED_ENTITY(PaintedEntityPacket.class, PaintedEntityPacket::new),
PACKET_FLUID_TANK(FluidSlotPacket.class, FluidSlotPacket::new);
private final Function<PacketByteBuf, BasePacket> factory;
PacketTypes(Class<? extends BasePacket> packetClass, Function<PacketByteBuf, BasePacket> factory) {
this.factory = factory;
REVERSE_LOOKUP.put(packetClass, this);
}
public static PacketTypes getPacket(final int id) {
return (values())[id];
}
static PacketTypes getID(final Class<? extends BasePacket> c) {
return REVERSE_LOOKUP.get(c);
}
public BasePacket parsePacket(final PacketByteBuf in) throws IllegalArgumentException {
return this.factory.apply(in);
}
}
}
@@ -1,4 +0,0 @@
package appeng.core.sync.network;
public class ClientNetworkHandler {
}
@@ -1,43 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 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.
*
* Applied Energistics 2 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 Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.core.sync.network;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.INetHandler;
import net.minecraft.network.PacketByteBuf;
import appeng.core.AELog;
import appeng.core.sync.BasePacket;
import appeng.core.sync.BasePacketHandler;
public class ClientPacketHandler extends BasePacketHandler implements IPacketHandler {
@Override
public void onPacketData(final INetworkInfo manager, final INetHandler handler, final PacketByteBuf packet,
final PlayerEntity player) {
try {
final int packetType = packet.readInt();
final BasePacket pack = PacketTypes.getPacket(packetType).parsePacket(packet);
pack.clientPacketData(manager, MinecraftClient.getInstance().player);
} catch (final IllegalArgumentException e) {
AELog.debug(e);
}
}
}
@@ -1,29 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 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.
*
* Applied Energistics 2 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 Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.core.sync.network;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.INetHandler;
import net.minecraft.network.PacketByteBuf;
public interface IPacketHandler {
void onPacketData(INetworkInfo manager, INetHandler handler, PacketByteBuf packet, PlayerEntity player);
}
@@ -1,138 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 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.
*
* Applied Energistics 2 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 Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.core.sync.network;
import net.minecraft.client.MinecraftClient;
import net.minecraft.network.NetworkSide;
import net.minecraft.network.Packet;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.network.INetHandler;
import net.minecraft.network.ThreadQuickExitException;
import net.minecraft.network.play.ServerPlayNetHandler;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.Identifier;
import net.minecraft.world.dimension.DimensionType;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.fml.LogicalSidedProvider;
import net.minecraftforge.fml.network.NetworkEvent;
import net.minecraftforge.fml.network.NetworkRegistry;
import net.minecraftforge.fml.network.event.EventNetworkChannel;
import appeng.core.sync.BasePacket;
public class NetworkHandler {
private static NetworkHandler instance;
private final Identifier myChannelName;
private final IPacketHandler clientHandler;
private final IPacketHandler serverHandler;
public NetworkHandler(final Identifier channelName) {
EventNetworkChannel ec = NetworkRegistry.ChannelBuilder.named(myChannelName = channelName)
.networkProtocolVersion(() -> "1").clientAcceptedVersions(s -> true).serverAcceptedVersions(s -> true)
.eventNetworkChannel();
ec.registerObject(this);
this.clientHandler = DistExecutor.unsafeRunForDist(() -> ClientPacketHandler::new, () -> () -> null);
this.serverHandler = this.createServerSide();
}
public static void init(final Identifier channelName) {
instance = new NetworkHandler(channelName);
}
public static NetworkHandler instance() {
return instance;
}
private IPacketHandler createServerSide() {
try {
return new ServerPacketHandler();
} catch (final Throwable t) {
return null;
}
}
@SubscribeEvent
public void serverPacket(final NetworkEvent.ClientCustomPayloadEvent ev) {
if (this.serverHandler != null) {
try {
NetworkEvent.Context ctx = ev.getSource().get();
ServerPlayNetHandler netHandler = (ServerPlayNetHandler) ctx.getNetworkManager().getNetHandler();
ctx.setPacketHandled(true);
ctx.enqueueWork(
() -> this.serverHandler.onPacketData(null, netHandler, ev.getPayload(), netHandler.player));
} catch (final ThreadQuickExitException ignored) {
}
}
}
@SubscribeEvent
public void clientPacket(final NetworkEvent.ServerCustomPayloadEvent ev) {
if (ev instanceof NetworkEvent.ServerCustomPayloadLoginEvent) {
return;
}
if (this.clientHandler != null) {
try {
NetworkEvent.Context ctx = ev.getSource().get();
INetHandler netHandler = ctx.getNetworkManager().getNetHandler();
ctx.setPacketHandled(true);
ctx.enqueueWork(() -> this.clientHandler.onPacketData(null, netHandler, ev.getPayload(), null));
} catch (final ThreadQuickExitException ignored) {
}
}
}
public Identifier getChannel() {
return this.myChannelName;
}
public void sendToAll(final BasePacket message) {
getServer().getPlayerList().sendPacketToAllPlayers(message.toPacket(NetworkSide.PLAY_TO_CLIENT));
}
public void sendTo(final BasePacket message, final ServerPlayerEntity player) {
player.connection.sendPacket(message.toPacket(NetworkSide.PLAY_TO_CLIENT));
}
public void sendToAllAround(final BasePacket message, final TargetPoint point) {
Packet<?> pkt = message.toPacket(NetworkSide.PLAY_TO_CLIENT);
getServer().getPlayerList().sendToAllNearExcept(point.excluded, point.x, point.y, point.z, point.r2, point.dim,
pkt);
}
public void sendToDimension(final BasePacket message, final DimensionType dim) {
getServer().getPlayerList().sendPacketToAllPlayersInDimension(message.toPacket(NetworkSide.PLAY_TO_CLIENT),
dim);
}
public void sendToServer(final BasePacket message) {
MinecraftClient.getInstance().getConnection().sendPacket(message.toPacket(NetworkSide.PLAY_TO_SERVER));
}
private MinecraftServer getServer() {
return LogicalSidedProvider.INSTANCE.get(LogicalSide.SERVER);
}
}
@@ -1,4 +0,0 @@
package appeng.core.sync.network;
public class ServerNetworkHandler {
}
@@ -1,42 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 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.
*
* Applied Energistics 2 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 Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.core.sync.network;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.INetHandler;
import net.minecraft.network.PacketByteBuf;
import appeng.core.AELog;
import appeng.core.sync.BasePacket;
import appeng.core.sync.BasePacketHandler;
public final class ServerPacketHandler extends BasePacketHandler implements IPacketHandler {
@Override
public void onPacketData(final INetworkInfo manager, final INetHandler handler, final PacketByteBuf packet,
final PlayerEntity player) {
try {
final int packetType = packet.readInt();
final BasePacket pack = PacketTypes.getPacket(packetType).parsePacket(packet);
pack.serverPacketData(manager, player);
} catch (final IllegalArgumentException e) {
AELog.debug(e);
}
}
}
@@ -1,39 +0,0 @@
package appeng.core.sync.network;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.world.dimension.DimensionType;
/**
* Created by covers1624 on 1/6/20.
*/
public class TargetPoint {
public final ServerPlayerEntity excluded;
public final double x;
public final double y;
public final double z;
public final double r2;
public final DimensionType dim;
public TargetPoint(double x, double y, double z, double r2, DimensionType dim) {
this(null, x, y, z, r2, dim);
}
public TargetPoint(ServerPlayerEntity excluded, double x, double y, double z, double r2, DimensionType dim) {
this.excluded = excluded;
this.x = x;
this.y = y;
this.z = z;
this.r2 = r2;
this.dim = dim;
}
public static TargetPoint at(double x, double y, double z, double r2, DimensionType dim) {
return new TargetPoint(x, y, z, r2, dim);
}
public static TargetPoint at(ServerPlayerEntity excluded, double x, double y, double z, double r2,
DimensionType dim) {
return new TargetPoint(excluded, x, y, z, r2, dim);
}
}
@@ -1,65 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 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.
*
* Applied Energistics 2 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 Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.core.sync.packets;
import io.netty.buffer.Unpooled;
import net.fabricmc.api.Environment;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.particle.ParticleTypes;
import net.fabricmc.api.EnvType;
import appeng.core.sync.BasePacket;
import appeng.core.sync.network.INetworkInfo;
public class MockExplosionPacket extends BasePacket {
private final double x;
private final double y;
private final double z;
public MockExplosionPacket(final PacketByteBuf stream) {
this.x = stream.readDouble();
this.y = stream.readDouble();
this.z = stream.readDouble();
}
// api
public MockExplosionPacket(final double x, final double y, final double z) {
this.x = x;
this.y = y;
this.z = z;
final PacketByteBuf data = new PacketByteBuf(Unpooled.buffer());
data.writeInt(this.getPacketID());
data.writeDouble(x);
data.writeDouble(y);
data.writeDouble(z);
this.configureWrite(data);
}
@Override
@Environment(EnvType.CLIENT)
public void clientPacketData(final INetworkInfo network, final PlayerEntity player) {
player.world.addParticle(ParticleTypes.EXPLOSION, this.x, this.y, this.z, 1.0D, 0.0D, 0.0D);
}
}