Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
chemiofitor committed Apr 4, 2024
1 parent 5a0dc68 commit fdd9be1
Show file tree
Hide file tree
Showing 60 changed files with 1,682 additions and 354 deletions.
23 changes: 23 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@ loom {
accessWidenerPath = file("src/main/resources/holib.accesswidener")
}

loom {
runs {
datagen {
inherit client
name "Minecraft DataGeneration"
vmArg "-Dfabric-api.datagen"
vmArg "-Dfabric-api.datagen.output-dir=${file("src/main/generated")}"

runDir "build/datagen"
}
}
}

sourceSets {
main {
resources {
srcDirs += [
'src/main/generated'
]
}
}
}

repositories {
maven { url "https://maven.terraformersmc.com/releases/" }
maven { url "https://api.modrinth.com/maven" }
Expand Down
6 changes: 6 additions & 0 deletions src/main/generated/.cache/cache
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bbb381f96e51e6e0d287f582143af547aa5f19b1 assets\ho\models\item\coal_sword.json
8021ad86c3499788e1ca1453352a9340398451b6 assets\ho\models\item\iron_coal.json
dcff91ebced2ae747965a906aa4a0580e22b4890 assets\holib\lang\en_us.json
bf7ab9938c517f88614ff7ebfde9c93ae70e36bc data\c\tags\items\coal\iron.json
bf7ab9938c517f88614ff7ebfde9c93ae70e36bc data\c\tags\items\coal.json
77ed1d946b345861306eb581756dc9c14ec55168 assets\holib\lang\zh_cn.json
21 changes: 14 additions & 7 deletions src/main/java/ho/artisan/lib/HOLibrary.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package ho.artisan.lib;

import ho.artisan.lib.example.HORegistryHandler;
import ho.artisan.lib.test.TestHOLibrary;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HOLibrary implements ModInitializer {
public class HOLibrary implements ModInitializer, ClientModInitializer {
public static final String MOD_ID = "holib";
public static final String MOD_NAME = "HOLibrary";
public static final String MOD_NAME = "HO-Library";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_NAME);

@Override
public void onInitialize() {
if (FabricLoader.getInstance().isDevelopmentEnvironment()) {
HORegistryHandler.load();
}
LOGGER.info(MOD_NAME + " has set up!");
if (FabricLoader.getInstance().isDevelopmentEnvironment())
TestHOLibrary.onInitialize();
LOGGER.info("HO-Library has set up!");
}

@Override
public void onInitializeClient() {
if (FabricLoader.getInstance().isDevelopmentEnvironment())
TestHOLibrary.onInitializeClient();
LOGGER.info("HO-Library has set up in client-side!");
}
}
31 changes: 31 additions & 0 deletions src/main/java/ho/artisan/lib/common/block/HODataBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ho.artisan.lib.common.block;

import ho.artisan.lib.common.blockentity.HOBlockEntity;
import net.minecraft.block.BlockRenderType;
import net.minecraft.block.BlockState;
import net.minecraft.block.BlockWithEntity;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.Nullable;

import java.util.function.BiFunction;

public class HODataBlock<T extends HOBlockEntity> extends BlockWithEntity {
private final BiFunction<BlockPos, BlockState, T> tBuilder;

public HODataBlock(Settings settings, BiFunction<BlockPos, BlockState, T> tBuilder) {
super(settings);
this.tBuilder = tBuilder;
}

@Override
public BlockRenderType getRenderType(BlockState state) {
return BlockRenderType.MODEL;
}

@Nullable
@Override
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return tBuilder.apply(pos, state);
}
}
41 changes: 41 additions & 0 deletions src/main/java/ho/artisan/lib/common/block/HOReserveBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ho.artisan.lib.common.block;

import ho.artisan.lib.common.blockentity.HOBlockEntity;
import net.minecraft.block.BlockState;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;

import java.util.function.BiFunction;

public class HOReserveBlock<T extends HOBlockEntity> extends HODataBlock<T> {
public HOReserveBlock(Settings settings, BiFunction<BlockPos, BlockState, T> tBuilder) {
super(settings, tBuilder);
}

@Override
public ItemStack getPickStack(BlockView world, BlockPos pos, BlockState state) {
ItemStack itemStack = super.getPickStack(world, pos, state);
if (world.getBlockEntity(pos) instanceof HOBlockEntity blockEntity) {
blockEntity.writeReserves(itemStack);
}
return itemStack;
}

@Override
public void onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) {
if (world.getBlockEntity(pos) instanceof HOBlockEntity blockEntity) {
if (!world.isClient && player.isCreative() && !blockEntity.getReserves().isEmpty()) {
ItemStack itemStack = new ItemStack(this);
blockEntity.writeReserves(itemStack);
ItemEntity itemEntity = new ItemEntity(world, (double)pos.getX() + 0.5, (double)pos.getY() + 0.5, (double)pos.getZ() + 0.5, itemStack);
itemEntity.setToDefaultPickupDelay();
world.spawnEntity(itemEntity);
}
}
super.onBreak(world, pos, state, player);
}
}
106 changes: 106 additions & 0 deletions src/main/java/ho/artisan/lib/common/blockentity/HOBlockEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package ho.artisan.lib.common.blockentity;

import ho.artisan.lib.data.IData;
import ho.artisan.lib.data.Observable;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.Packet;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.Nullable;

import java.util.*;

public abstract class HOBlockEntity extends BlockEntity {
private final Map<String, IData<?>> dataMap = new HashMap<>();
private final Set<String> reserveDataKeys = new TreeSet<>();

public HOBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state);
}

public void load(IData<?>... iData) {
for (IData<?> iDatum : iData) {
this.dataMap.put(iDatum.key(), iDatum);
if (iDatum instanceof Observable<?> observable)
observable.addListener("BlockUpdate", (o -> markDirty()));
}
}

public void reserve(IData<?>... iData) {
for (IData<?> iDatum : iData) {
reserveDataKeys.add(iDatum.key());
}
}

@Override
public void markDirty() {
super.markDirty();
if (world != null) {
world.updateListeners(pos, this.getCachedState(), this.getCachedState(), Block.NOTIFY_LISTENERS);
}
}

@Override
protected void writeNbt(NbtCompound nbt) {
super.writeNbt(nbt);
getDataMap().forEach((key, value) -> value.write(nbt));
}

@Override
public void readNbt(NbtCompound nbt) {
super.readNbt(nbt);
getDataMap().forEach((key, value) -> value.read(nbt));
}

@Nullable
@Override
public Packet<ClientPlayPacketListener> toUpdatePacket() {
return BlockEntityUpdateS2CPacket.create(this);
}

@Override
public NbtCompound toInitialChunkDataNbt() {
return createNbt();
}

public void writeReserves(ItemStack stack) {
NbtCompound nbt = new NbtCompound();
for (IData<?> reserve : getReserves()) {
reserve.write(nbt);
}
BlockItem.setBlockEntityNbt(stack, this.getType(), nbt);
}

public Collection<IData<?>> getReserves() {
Collection<IData<?>> collection = new ArrayList<>();
getDataMap().forEach((s, data) -> {
if (reserveDataKeys.contains(s))
collection.add(data);
});
return collection;
}

public Map<String, IData<?>> getDataMap() {
return this.dataMap;
}

@Nullable
@SuppressWarnings("unchecked")
public <T> IData<? extends T> getData(String key, Class<T> type) {
IData<?> iData = this.dataMap.get(key);
try {
if (type.isInstance(iData.get()))
return (IData<? extends T>) iData;
return null;
} catch (Exception e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ho.artisan.lib.common.blockentity;

import ho.artisan.lib.data.IData;
import ho.artisan.lib.data.IScreen;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.Nullable;

import java.util.*;

public abstract class HOScreenBlockEntity extends HOBlockEntity implements ExtendedScreenHandlerFactory {
private final Set<String> screenDataKeys = new TreeSet<>();

public HOScreenBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state);
}

public void screenLoad(IData<?>... iData) {
for (IData<?> iDatum : iData) {
screenDataKeys.add(iDatum.key());
}
}

public Collection<IData<?>> getScreenData() {
Collection<IData<?>> collection = new ArrayList<>();
getDataMap().forEach((s, data) -> {
if (screenDataKeys.contains(s))
collection.add(data);
});
return collection;
}

@Override
public void writeScreenOpeningData(ServerPlayerEntity player, PacketByteBuf buf) {
getScreenData().forEach(data -> {
if (data instanceof IScreen iScreen)
iScreen.write(player, buf);
else
data.write(buf);
});
}

@Nullable
@Override
public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) {
return null;
}
}
4 changes: 0 additions & 4 deletions src/main/java/ho/artisan/lib/common/item/FuelItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import net.fabricmc.fabric.api.registry.FuelRegistry;
import net.minecraft.item.Item;


/**
* 使用FabricAPI注册燃烧时间的燃料类物品
*/
public class FuelItem extends Item {
public FuelItem(Settings settings, int burnTime) {
super(settings);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ho.artisan.lib.common.screen.handler;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
import org.jetbrains.annotations.Nullable;

public abstract class HOScreenHandler extends ScreenHandler {
protected HOScreenHandler(@Nullable ScreenHandlerType<?> type, int syncId, PlayerInventory inventory, PacketByteBuf buf) {
super(type, syncId);
}

@Override
public boolean canUse(PlayerEntity player) {
return false;
}
}
Loading

0 comments on commit fdd9be1

Please sign in to comment.