Working TE, packets 🎉🎉

This commit is contained in:
oitsjustjose 2022-10-16 01:35:52 -05:00
commit 9d5d6f9656
7 changed files with 130 additions and 26 deletions

View file

@ -25,6 +25,7 @@ public class ChargedExplosives {
instance = this;
REGISTRY.BlockRegistry.register(FMLJavaModLoadingContext.get().getModEventBus());
REGISTRY.ItemRegistry.register(FMLJavaModLoadingContext.get().getModEventBus());
REGISTRY.BlockEntityTypeRegistry.register(FMLJavaModLoadingContext.get().getModEventBus());
MinecraftForge.EVENT_BUS.register(this);
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
}

View file

@ -14,7 +14,7 @@ public class ClientProxy extends CommonProxy {
@Override
public void init() {
CommonProxy.netMgr.networkWrapper.registerMessage(CommonProxy.disc++, OpenGuiPacket.class, OpenGuiPacket::encode, OpenGuiPacket::decode, ClientOpenGuiPacket::handleClient);
CommonProxy.netMgr.networkWrapper.registerMessage(CommonProxy.disc++, UpdateNbtPacket.class, UpdateNbtPacket::encode, UpdateNbtPacket::decode, UpdateNbtPacket::handleClient, Optional.of(NetworkDirection.PLAY_TO_SERVER));
CommonProxy.netMgr.networkWrapper.registerMessage(CommonProxy.disc++, UpdateNbtPacket.class, UpdateNbtPacket::encode, UpdateNbtPacket::decode, UpdateNbtPacket::handle, Optional.of(NetworkDirection.PLAY_TO_SERVER));
}
@Override

View file

@ -19,7 +19,7 @@ public class CommonProxy {
public void init() {
CommonProxy.netMgr.networkWrapper.registerMessage(CommonProxy.disc++, OpenGuiPacket.class, OpenGuiPacket::encode, OpenGuiPacket::decode, OpenGuiPacket::handleServer);
CommonProxy.netMgr.networkWrapper.registerMessage(CommonProxy.disc++, UpdateNbtPacket.class, UpdateNbtPacket::encode, UpdateNbtPacket::decode, UpdateNbtPacket::handleServer, Optional.of(NetworkDirection.PLAY_TO_SERVER));
CommonProxy.netMgr.networkWrapper.registerMessage(CommonProxy.disc++, UpdateNbtPacket.class, UpdateNbtPacket::encode, UpdateNbtPacket::decode, UpdateNbtPacket::handle, Optional.of(NetworkDirection.PLAY_TO_SERVER));
}
public void openExplosiveGui(Player player, ItemStack stack) {

View file

@ -1,44 +1,81 @@
package com.oitsjustjose.charged_explosives.common.blocks;
import com.oitsjustjose.charged_explosives.common.tile.ChargedExplosiveBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.Containers;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.FaceAttachedHorizontalDirectionalBlock;
import net.minecraft.world.level.block.entity.AbstractFurnaceBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.AttachFace;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.material.MaterialColor;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class ChargedExplosiveBlock extends FaceAttachedHorizontalDirectionalBlock {
public class ChargedExplosiveBlock extends FaceAttachedHorizontalDirectionalBlock implements EntityBlock {
protected static final VoxelShape CEILING_AABB_X = Block.box(6.0D, 14.0D, 5.0D, 10.0D, 16.0D, 11.0D);
protected static final VoxelShape CEILING_AABB_Z = Block.box(5.0D, 14.0D, 6.0D, 11.0D, 16.0D, 10.0D);
protected static final VoxelShape FLOOR_AABB_X = Block.box(6.0D, 0.0D, 5.0D, 10.0D, 2.0D, 11.0D);
protected static final VoxelShape FLOOR_AABB_Z = Block.box(5.0D, 0.0D, 6.0D, 11.0D, 2.0D, 10.0D);
protected static final VoxelShape NORTH_AABB = Block.box(5.0D, 6.0D, 14.0D, 11.0D, 10.0D, 16.0D);
protected static final VoxelShape SOUTH_AABB = Block.box(5.0D, 6.0D, 0.0D, 11.0D, 10.0D, 2.0D);
protected static final VoxelShape WEST_AABB = Block.box(14.0D, 6.0D, 5.0D, 16.0D, 10.0D, 11.0D);
protected static final VoxelShape EAST_AABB = Block.box(0.0D, 6.0D, 5.0D, 2.0D, 10.0D, 11.0D);
protected static final VoxelShape CEILING_AABB_X = Block.box(4D, 13D, 2D, 12D, 16D, 14D);
protected static final VoxelShape CEILING_AABB_Z = Block.box(2D, 13D, 4D, 14D, 16D, 12D);
protected static final VoxelShape FLOOR_AABB_X = Block.box(4D, 0D, 2D, 12D, 3D, 14D);
protected static final VoxelShape FLOOR_AABB_Z = Block.box(2D, 0D, 4D, 14D, 3D, 12D);
protected static final VoxelShape SOUTH_AABB = Block.box(2D, 4D, 0D, 14D, 12D, 3D);
protected static final VoxelShape NORTH_AABB = Block.box(2D, 4D, 13D, 14D, 12D, 16D);
protected static final VoxelShape WEST_AABB = Block.box(13D, 4D, 2D, 16D, 12D, 14D);
protected static final VoxelShape EAST_AABB = Block.box(0D, 4D, 2D, 3D, 12D, 14D);
public ChargedExplosiveBlock() {
super(BlockBehaviour.Properties.of(Material.EXPLOSIVE, MaterialColor.COLOR_RED)
.dynamicShape()
.noOcclusion()
.isValidSpawn((w, x, y, z) -> false)
.isRedstoneConductor((x, y, z) -> false)
.isSuffocating((x, y, z) -> false)
.isViewBlocking((x, y, z) -> false));
super(BlockBehaviour.Properties.of(Material.EXPLOSIVE, MaterialColor.COLOR_RED).dynamicShape().noOcclusion().isValidSpawn((w, x, y, z) -> false).isRedstoneConductor((x, y, z) -> false).isSuffocating((x, y, z) -> false).isViewBlocking((x, y, z) -> false));
this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(FACE, AttachFace.WALL));
}
public @NotNull VoxelShape getShape(BlockState state, @NotNull BlockGetter getter, @NotNull BlockPos pos) {
@Override
public @NotNull VoxelShape getShape(BlockState state, BlockGetter worldIn, BlockPos pos, CollisionContext context) {
Direction direction = state.getValue(FACING);
switch (state.getValue(FACE)) {
case FLOOR:
if (direction.getAxis() == Direction.Axis.X) {
return FLOOR_AABB_X;
}
return FLOOR_AABB_Z;
case WALL:
return switch (direction) {
case EAST -> EAST_AABB;
case WEST -> WEST_AABB;
case SOUTH -> SOUTH_AABB;
case DOWN -> FLOOR_AABB_X;
case UP -> CEILING_AABB_X;
case NORTH -> NORTH_AABB;
};
case CEILING:
default:
if (direction.getAxis() == Direction.Axis.X) {
return CEILING_AABB_X;
} else {
return CEILING_AABB_Z;
}
}
}
@Override
public @NotNull VoxelShape getCollisionShape(BlockState state, BlockGetter worldIn, BlockPos pos, CollisionContext context) {
Direction direction = state.getValue(FACING);
switch (state.getValue(FACE)) {
case FLOOR:
@ -69,4 +106,37 @@ public class ChargedExplosiveBlock extends FaceAttachedHorizontalDirectionalBloc
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> Builder) {
Builder.add(FACING, FACE);
}
@Override
public void setPlacedBy(@NotNull Level level, @NotNull BlockPos pos, @NotNull BlockState state, LivingEntity placer, @NotNull ItemStack stack) {
super.setPlacedBy(level, pos, state, placer, stack);
ChargedExplosiveBlockEntity be = (ChargedExplosiveBlockEntity) level.getBlockEntity(pos);
if (be == null) {
return;
}
CompoundTag tag = stack.getOrCreateTag().copy();
if (!tag.contains("explosionWidth") || !tag.contains("explosionHeight") || !tag.contains("explosionDepth")) {
tag.putInt("explosionWidth", 0);
tag.putInt("explosionHeight", 0);
tag.putInt("explosionDepth", 0);
}
be.load(tag);
}
@Override
public void onRemove(BlockState oldState, @NotNull Level level, @NotNull BlockPos pos, BlockState newState, boolean moving) {
if (!newState.is(oldState.getBlock())) {
level.removeBlockEntity(pos);
}
}
@Nullable
@Override
public BlockEntity newBlockEntity(@NotNull BlockPos pos, @NotNull BlockState state) {
return new ChargedExplosiveBlockEntity(pos, state);
}
}

View file

@ -32,11 +32,7 @@ public class UpdateNbtPacket {
buf.writeItem(pkt.stack);
}
public void handleClient(Supplier<NetworkEvent.Context> ctx) {
ctx.get().setPacketHandled(true);
}
public static void handleServer(UpdateNbtPacket pkt, Supplier<NetworkEvent.Context> ctx) {
public static void handle(UpdateNbtPacket pkt, Supplier<NetworkEvent.Context> ctx) {
if (ctx.get().getDirection().getReceptionSide() == LogicalSide.SERVER) {
ctx.get().enqueueWork(() -> {
if (!pkt.stack.hasTag()) {
@ -50,7 +46,7 @@ public class UpdateNbtPacket {
return x.hasTag()
&& x.getTag() != null
&& x.getTag().contains("explosive_id")
&& x.getTag().getUUID("explosive_id") == newTag.getUUID("explosive_id");
&& x.getTag().getUUID("explosive_id").equals(newTag.getUUID("explosive_id"));
}).findFirst();
if (stack.isPresent()) {

View file

@ -3,8 +3,11 @@ package com.oitsjustjose.charged_explosives.common.registry;
import com.oitsjustjose.charged_explosives.ChargedExplosives;
import com.oitsjustjose.charged_explosives.common.blocks.ChargedExplosiveBlock;
import com.oitsjustjose.charged_explosives.common.items.ChargedExplosiveItem;
import com.oitsjustjose.charged_explosives.common.tile.ChargedExplosiveBlockEntity;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
@ -12,7 +15,9 @@ import net.minecraftforge.registries.RegistryObject;
public class Registry {
public final DeferredRegister<Block> BlockRegistry = DeferredRegister.create(ForgeRegistries.BLOCKS, ChargedExplosives.MODID);
public final DeferredRegister<Item> ItemRegistry = DeferredRegister.create(ForgeRegistries.ITEMS, ChargedExplosives.MODID);
public final DeferredRegister<BlockEntityType<?>> BlockEntityTypeRegistry = DeferredRegister.create(ForgeRegistries.BLOCK_ENTITY_TYPES, ChargedExplosives.MODID);
public final RegistryObject<Block> CeBlock = BlockRegistry.register("charged_explosive", ChargedExplosiveBlock::new);
public final RegistryObject<Item> CeItem = ItemRegistry.register("charged_explosive", () -> new ChargedExplosiveItem(CeBlock.get()));
public final RegistryObject<BlockEntityType<ChargedExplosiveBlockEntity>> CeBlockEntityType = BlockEntityTypeRegistry.register("charged_explosive", () -> BlockEntityType.Builder.of(ChargedExplosiveBlockEntity::new, Blocks.AIR).build(null));
}

View file

@ -0,0 +1,32 @@
package com.oitsjustjose.charged_explosives.common.tile;
import com.oitsjustjose.charged_explosives.ChargedExplosives;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
public class ChargedExplosiveBlockEntity extends BlockEntity {
private int explosionWidth = 0;
private int explosionHeight = 0;
private int explosionDepth = 0;
public ChargedExplosiveBlockEntity(BlockPos pos, BlockState state) {
super(ChargedExplosives.getInstance().REGISTRY.CeBlockEntityType.get(), pos, state);
}
// saveAdditional(CompoundTag tag)
protected void saveAdditional(CompoundTag tag) {
tag.putInt("explosionWidth", this.explosionWidth);
tag.putInt("explosionHeight", this.explosionHeight);
tag.putInt("explosionDepth", this.explosionDepth);
}
public void load(CompoundTag tag) {
this.explosionWidth = tag.getInt("explosionWidth");
this.explosionHeight = tag.getInt("explosionHeight");
this.explosionDepth = tag.getInt("explosionDepth");
}
}