Skip to content

Commit

Permalink
添加部分 javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
MegumiKasuga committed Mar 25, 2024
1 parent 0cecfbb commit a89b063
Show file tree
Hide file tree
Showing 15 changed files with 578 additions and 96 deletions.
27 changes: 25 additions & 2 deletions src/main/java/kasuga/lib/core/network/C2SPacket.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
package kasuga.lib.core.network;

import kasuga.lib.core.annos.Inner;
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.network.NetworkDirection;
import net.minecraftforge.network.NetworkEvent;

/**
* This packet should be sent by logical client and received by logical server.
* It is used for your custom packets that transmit custom data from client to server.
* To register one of this, see {@link kasuga.lib.registrations.common.ChannelReg}
*/
public abstract class C2SPacket extends Packet {
public C2SPacket() {super();}
public C2SPacket(FriendlyByteBuf buf) {}

/**
* This function is the deserializer of your packet.
* See {@link Packet} for more constructor info.
* @param buf data bytes you got from the network.
*/
public C2SPacket(FriendlyByteBuf buf) {super(buf);}

@Inner
public boolean onReach(NetworkEvent.Context context) {
context.enqueueWork(() -> handle(context));
return true;
}

/**
* The handler of your packet. After this packet has been received by
* logical server, we would handle this packet in this method.
* @param context some server info, such level, player and so on.
*/
public abstract void handle(NetworkEvent.Context context);

/**
* The encoder of your packet. You must put all your data into this
* byte buffer in order to transmit them.
* @param buf the data container buffer, push your data into it.
*/
abstract public void encode(FriendlyByteBuf buf);
}
15 changes: 14 additions & 1 deletion src/main/java/kasuga/lib/core/network/Packet.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kasuga.lib.core.network;

import kasuga.lib.core.annos.Inner;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.network.NetworkEvent;

Expand All @@ -9,8 +10,20 @@
* For packages from server to client, use {@link S2CPacket}
*/
public abstract class Packet {
public Packet(){}

/**
* This constructor is also used as decoder. While the program get data from network, it would
* use this deserializer to create our packet.
* @param buf the bytes we got from network.
*/
public Packet(FriendlyByteBuf buf) {}

@Inner
abstract public boolean onReach(NetworkEvent.Context context);

/**
* The encoder of your packet, you must push all your data into this byte buffer.
* @param buf the data container buffer, push your data into it.
*/
abstract public void encode(FriendlyByteBuf buf);
}
21 changes: 20 additions & 1 deletion src/main/java/kasuga/lib/core/network/S2CPacket.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
package kasuga.lib.core.network;

import kasuga.lib.core.annos.Inner;
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.network.NetworkEvent;

/**
* This packet should be sent by logical server and received by logical client.
* It is used for your custom packets that transmit custom data from server to client.
* To register one of this, see {@link kasuga.lib.registrations.common.ChannelReg}
*/
public abstract class S2CPacket extends Packet {

public S2CPacket() {super();}
/**
* The decoder constructor of your packet. Take all your data out from the byte buffer here.
* @param buf the received byte buffer.
*/
public S2CPacket(FriendlyByteBuf buf) {super(buf);}
@Override
@Inner
public boolean onReach(NetworkEvent.Context context) {
context.enqueueWork(() -> handle(Minecraft.getInstance()));
return true;
}

/**
* The handler of your packet, the packet would be handled here.
* @param minecraft Your minecraft client.
*/
public abstract void handle(Minecraft minecraft);

/**
* Push your data into the byte buffer here.
* @param buf the data container buffer, push your data into it.
*/
public abstract void encode(FriendlyByteBuf buf);
}
9 changes: 9 additions & 0 deletions src/main/java/kasuga/lib/core/util/data_type/Pair.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package kasuga.lib.core.util.data_type;

import kasuga.lib.core.annos.Util;

/**
* A simple data struct that contains two elements.
* @param <K> Type of first data.
* @param <V> Type of second data.
*/
@Util
public class Pair<K, V> {
K first;
V second;
Expand All @@ -12,6 +20,7 @@ public static <K, V> Pair<K, V> of(K first, V second) {
return new Pair<K, V>(first, second);
}


public K getFirst() {
return first;
}
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/kasuga/lib/registrations/TagReg.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,4 @@ public TagReg(String registrationKey) {
* @return the location.
*/
public ResourceLocation location() {return location;}

public interface TagProvider<T> {
TagKey<T> provide();
}
}
81 changes: 73 additions & 8 deletions src/main/java/kasuga/lib/registrations/common/BlockEntityReg.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,106 @@
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.registries.RegistryObject;

import java.util.ArrayList;
import java.util.List;

/**
* The registration element of block entity. Block entity is a custom block data container, which makes us can
* customize the block action or renderer. It is spawn and destroy as same as the block's lifecycle.
* If you want to storage your custom data or your custom function, use a block entity. To use a block entity,
* Your block must be a subClass of BaseEntityBlock, See
* {@link net.minecraft.world.level.block.BaseEntityBlock} and {@link BlockEntity} for more info.
* @param <T> the type of blockEntity,
*/
public class BlockEntityReg<T extends BlockEntity> extends Reg {
private ArrayList<BlockProvider<?>> blockInvokerList;
private RegistryObject<BlockEntityType<T>> registryObject;
private com.mojang.datafixers.types.Type<?> dataType = null;
private BlockEntityType.BlockEntitySupplier<T> builder;
private BlockEntityRendererBuilder<T> rendererBuilder = null;

/**
* Use this to create a block entity reg.
* @param registrationKey the registration key of your block entity
*/
public BlockEntityReg(String registrationKey) {
super(registrationKey);
blockInvokerList = new ArrayList<>();
}

/**
* The instance supplier of your block entity. It is used to provide a block entity instance for the game.
* @param blockEntity Your Block Entity supplier.
* @return self.
*/
@Mandatory
public BlockEntityReg<T> blockEntityType(BlockEntityType.BlockEntitySupplier<? extends BlockEntity> blockEntity) {
this.builder =(BlockEntityType.BlockEntitySupplier<T>) blockEntity;
return this;
}

/**
* Which blocks could have this block entity ? Block Entity would be destroyed if
* there's an invalid block in its block pos.
* @param block The block suppliers.
* @return self.
*/
@Optional
public BlockEntityReg<T> withBlocks(BlockProvider<?>... block) {
blockInvokerList.addAll(List.of(block));
return this;
}

/**
* Aaa a block to block entity's valid block list.
* @param block the block supplier.
* @return self
*/
@Optional
public BlockEntityReg<T> addBlock(BlockProvider<?> block) {
this.blockInvokerList.add(block);
return this;
}

/**
* The block entity may have a block entity renderer.
* Block entity renderer is used for customize block rendering.
* It would only be applied in client side.
* @param builder your block entity renderer supplier.
* @return self.
*/
@Optional
public BlockEntityReg<T> withRenderer(BlockEntityRendererBuilder<?> builder) {
this.rendererBuilder = (BlockEntityRendererBuilder<T>) builder;
return this;
}

/**
* Other vanilla data you would like to apply to this block entity.
* @param dataType data you would like to give.
* @return self.
*/
@Optional
public BlockEntityReg<T> dataType(com.mojang.datafixers.types.Type<?> dataType) {
this.dataType = dataType;
return this;
}

/**
* You must call this after your config completed. It would handle your reg to forge and minecraft.
* @param registry the mod SimpleRegistry.
* @return self
*/
@Mandatory
public BlockEntityReg<T> submit(SimpleRegistry registry) {
registryObject = registry.blockEntity()
.register(registrationKey, () -> BlockEntityType.Builder.of(builder, getBlockList()).build(dataType));
return this;
}

public Block[] getBlockList() {
Block[] result = new Block[blockInvokerList.size()];
int counter = 0;
Expand All @@ -83,28 +136,40 @@ public String getIdentifier() {
return "block_entity";
}


@Mandatory
public BlockEntityReg<T> submit(SimpleRegistry registry) {
registryObject = registry.blockEntity()
.register(registrationKey, () -> BlockEntityType.Builder.of(builder, getBlockList()).build(dataType));
return this;
}

@Inner
public void registerRenderer(SimpleRegistry registry) {
if(this.rendererBuilder != null)
registry.registerBlockEntityRenderer(() -> registryObject.get(), rendererBuilder);
}

/**
* A function interface that provides a block. It is important that you must get your block from an vaild
* registration object like BlockReg in Kasuga Reg or RegistryObject in minecraftforge.
* The game needs blocks that has their own registration data. If you just provide this by calling a new instance
* For example, we could write "() -> exampleBlockReg.instance()" or "exampleBlockReg::get"
* to get this. For more info, see {@link BlockReg#instance()} and {@link RegistryObject#get()}
* @param <T> Type of your block.
*/
public interface BlockProvider<T extends Block> {
T provide();
}

/**
* A function interface that provides a bLock entity. It is a initializer of your block entity. Pass a
* constructor method in this, like "ExampleBlockEntity::new" or "() -> new ExampleBlockEntity()".
* @param <T> Type of your block entity.
*/
public interface BlockEntityProvider<T extends BlockEntity> {
BlockEntityType<T> provide();
}

/**
* A function interface that provides a block entity renderer. If you want to use a block entity renderer on your block and
* block entity, you should first override the {@link Block#getRenderShape(BlockState)} method under your block class.
* Then, pass a block entity renderer builder into {@link BlockEntityReg#withRenderer(BlockEntityRendererBuilder)} method.
* The lib would deal with it for you.
* @param <T> the block entity your renderer belongs to.
*/
public interface BlockEntityRendererBuilder<T extends BlockEntity> {
BlockEntityRenderer<T> build(BlockEntityRendererProvider.Context context);
}
Expand Down
Loading

0 comments on commit a89b063

Please sign in to comment.