-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
chemiofitor
committed
Apr 4, 2024
1 parent
5a0dc68
commit fdd9be1
Showing
60 changed files
with
1,682 additions
and
354 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
src/main/java/ho/artisan/lib/common/block/HODataBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
src/main/java/ho/artisan/lib/common/block/HOReserveBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
106
src/main/java/ho/artisan/lib/common/blockentity/HOBlockEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/ho/artisan/lib/common/blockentity/HOScreenBlockEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/ho/artisan/lib/common/screen/handler/HOScreenHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.