Skip to content

Commit

Permalink
👽 CabinetAPI 1.0.5
Browse files Browse the repository at this point in the history
CallMeEchoCodes committed Mar 19, 2024
1 parent 0f22000 commit b55790f
Showing 10 changed files with 16 additions and 193 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -10,4 +10,4 @@ maven_group=dev.callmeecho
archives_base_name=hollow

fabric_version=0.92.0+1.20.1
cabinet_version=1.0.4
cabinet_version=1.0.5
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.callmeecho.hollow.client;

import dev.callmeecho.cabinetapi.misc.ReflectionHelper;
import dev.callmeecho.cabinetapi.util.ReflectionHelper;
import dev.callmeecho.cabinetapi.registry.RegistrarHandler;
import dev.callmeecho.hollow.client.render.entity.FireflyEntityRenderer;
import dev.callmeecho.hollow.client.render.entity.JarBlockEntityRenderer;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.callmeecho.hollow.datagen;

import dev.callmeecho.cabinetapi.misc.ReflectionHelper;
import dev.callmeecho.cabinetapi.util.ReflectionHelper;
import dev.callmeecho.cabinetapi.registry.RegistrarHandler;
import dev.callmeecho.hollow.main.block.HollowLogBlock;
import dev.callmeecho.hollow.main.registry.HollowBlockRegistry;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.callmeecho.hollow.datagen;

import dev.callmeecho.cabinetapi.misc.ReflectionHelper;
import dev.callmeecho.cabinetapi.util.ReflectionHelper;
import dev.callmeecho.cabinetapi.registry.RegistrarHandler;
import dev.callmeecho.hollow.main.block.HollowLogBlock;
import dev.callmeecho.hollow.main.registry.HollowBlockRegistry;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.callmeecho.hollow.main.block;

import dev.callmeecho.cabinetapi.misc.VoxelShapeHelper;
import dev.callmeecho.cabinetapi.util.VoxelShapeHelper;
import dev.callmeecho.hollow.main.HollowTags;
import net.minecraft.block.*;
import net.minecraft.item.ItemPlacementContext;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.callmeecho.hollow.main.block;

import dev.callmeecho.cabinetapi.misc.VoxelShapeHelper;
import dev.callmeecho.cabinetapi.util.VoxelShapeHelper;
import dev.callmeecho.hollow.main.block.entity.EchoingPotBlockEntity;
import dev.callmeecho.hollow.main.block.entity.JarBlockEntity;
import dev.callmeecho.hollow.main.block.entity.StoneChestBlockEntity;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.callmeecho.hollow.main.block;

import dev.callmeecho.cabinetapi.misc.VoxelShapeHelper;
import dev.callmeecho.cabinetapi.util.VoxelShapeHelper;
import dev.callmeecho.hollow.main.block.entity.StoneChestBlockEntity;
import net.minecraft.block.*;
import net.minecraft.block.entity.BlockEntity;
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
package dev.callmeecho.hollow.main.block.entity;

import dev.callmeecho.cabinetapi.misc.DefaultedInventory;
import dev.callmeecho.cabinetapi.util.DefaultedInventory;
import dev.callmeecho.cabinetapi.util.InventoryBlockEntity;
import dev.callmeecho.hollow.main.registry.HollowBlockEntityRegistry;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventories;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Hand;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

public class JarBlockEntity extends BlockEntity implements DefaultedInventory {
private final DefaultedList<ItemStack> inventory = DefaultedList.ofSize(16, ItemStack.EMPTY);

public class JarBlockEntity extends InventoryBlockEntity implements DefaultedInventory {
public JarBlockEntity(BlockPos pos, BlockState state) {
super(HollowBlockEntityRegistry.JAR_BLOCK_ENTITY, pos, state);
}

@Override
public DefaultedList<ItemStack> getItems() {
return inventory;
super(HollowBlockEntityRegistry.JAR_BLOCK_ENTITY, pos, state, 17);
}

public void use(World world, BlockPos pos, PlayerEntity player, Hand hand) {
@@ -66,28 +52,4 @@ public void use(World world, BlockPos pos, PlayerEntity player, Hand hand) {
notifyListeners();
player.setStackInHand(hand, ItemStack.EMPTY);
}

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

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

@Override
public void writeNbt(NbtCompound nbt) {
Inventories.writeNbt(nbt, inventory);
super.writeNbt(nbt);
}

@Override
public void readNbt(NbtCompound nbt) {
inventory.clear();
super.readNbt(nbt);
Inventories.readNbt(nbt, inventory);
}
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,18 @@
package dev.callmeecho.hollow.main.block.entity;

import dev.callmeecho.cabinetapi.particle.ParticleSystem;
import dev.callmeecho.cabinetapi.misc.DefaultedInventory;
import dev.callmeecho.cabinetapi.util.LootableInventoryBlockEntity;
import dev.callmeecho.hollow.main.registry.HollowBlockEntityRegistry;
import net.minecraft.advancement.criterion.Criteria;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.LootableContainerBlockEntity;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventories;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.loot.LootTable;
import net.minecraft.loot.LootTables;
import net.minecraft.loot.context.LootContextParameterSet;
import net.minecraft.loot.context.LootContextParameters;
import net.minecraft.loot.context.LootContextTypes;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

public class StoneChestBlockEntity extends BlockEntity implements DefaultedInventory {
public static final String LOOT_TABLE_KEY = "LootTable";
public static final String LOOT_TABLE_SEED_KEY = "LootTableSeed";

public class StoneChestBlockEntity extends LootableInventoryBlockEntity {
private static final ParticleSystem PARTICLE_SYSTEM = new ParticleSystem(
new Vec3d(0.025F, 0.05F, 0.025F),
new Vec3d(0.5, 1, 0.5),
@@ -50,81 +22,14 @@ public class StoneChestBlockEntity extends BlockEntity implements DefaultedInven
true,
ParticleTypes.LARGE_SMOKE
);

@Nullable
protected Identifier lootTableId;
protected long lootTableSeed;

private final DefaultedList<ItemStack> inventory = DefaultedList.ofSize(27, ItemStack.EMPTY);

@Override
public DefaultedList<ItemStack> getItems() {
return inventory;
}

public StoneChestBlockEntity(BlockPos pos, BlockState state) {
super(HollowBlockEntityRegistry.STONE_CHEST_BLOCK_ENTITY, pos, state);
}

public void checkLootInteraction(@Nullable PlayerEntity player) {
if (world == null) return;
if (this.lootTableId != null && this.world.getServer() != null) {
LootTable lootTable = this.world.getServer().getLootManager().getLootTable(this.lootTableId);
if (player instanceof ServerPlayerEntity) {
Criteria.PLAYER_GENERATES_CONTAINER_LOOT.trigger((ServerPlayerEntity)player, this.lootTableId);
}

this.lootTableId = null;
LootContextParameterSet.Builder builder = new LootContextParameterSet.Builder((ServerWorld)this.world)
.add(LootContextParameters.ORIGIN, Vec3d.ofCenter(this.pos));
if (player != null) {
builder.luck(player.getLuck()).add(LootContextParameters.THIS_ENTITY, player);
}

// I would use the seed but that would make it the same for every chest in a structure.
// I have no idea how mojang does it and I can't be bothered to look into it. So you get this hacky solution instead.
lootTable.supplyInventory(this, builder.build(LootContextTypes.CHEST), world.getRandom().nextLong());
}
}

public static void setLootTable(BlockView world, Random random, BlockPos pos, Identifier id) {
BlockEntity blockEntity = world.getBlockEntity(pos);
if (blockEntity instanceof LootableContainerBlockEntity) {
((LootableContainerBlockEntity)blockEntity).setLootTable(id, random.nextLong());
}
}

protected boolean deserializeLootTable(NbtCompound nbt) {
if (nbt.contains("LootTable", NbtElement.STRING_TYPE)) {
this.lootTableId = new Identifier(nbt.getString("LootTable"));
this.lootTableSeed = nbt.getLong("LootTableSeed");
return true;
} else {
return false;
}
}

protected boolean serializeLootTable(NbtCompound nbt) {
if (this.lootTableId == null) {
return false;
} else {
nbt.putString("LootTable", this.lootTableId.toString());
if (this.lootTableSeed != 0L) {
nbt.putLong("LootTableSeed", this.lootTableSeed);
}

return true;
}
}

public void setLootTable(Identifier id, long seed) {
this.lootTableId = id;
this.lootTableSeed = seed;
super(HollowBlockEntityRegistry.STONE_CHEST_BLOCK_ENTITY, pos, state, 27);
}

public void aboveBroken() {
if (world == null) return;
checkLootInteraction(null);
checkLootInteraction(null, true);
Vec3d centerPos = pos.toCenterPos();
for (ItemStack stack : inventory) {
if (!stack.isEmpty()) {
@@ -156,48 +61,4 @@ public void use(PlayerEntity player, Hand hand) {
notifyListeners();
player.setStackInHand(hand, ItemStack.EMPTY);
}

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

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

@Override
public void readNbt(NbtCompound nbt) {
super.readNbt(nbt);
inventory.clear();
if (!this.deserializeLootTable(nbt)) {
Inventories.readNbt(nbt, this.inventory);
}
}

@Override
protected void writeNbt(NbtCompound nbt) {
super.writeNbt(nbt);
if (!this.serializeLootTable(nbt)) {
Inventories.writeNbt(nbt, this.inventory);
}
}

@Nullable
@Override
public World getWorld() {
return super.getWorld();
}

@Override
public BlockPos getPos() {
return super.getPos();
}

@Override
public BlockState getCachedState() {
return super.getCachedState();
}
}
2 changes: 1 addition & 1 deletion src/main/resources/assets/hollow/models/item/paeonia.json
Original file line number Diff line number Diff line change
@@ -3,4 +3,4 @@
"textures": {
"layer0": "hollow:block/paeonia"
}
}a
}

0 comments on commit b55790f

Please sign in to comment.