Skip to content

Commit

Permalink
迁移渲染器 gui 部分方块实体
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuRuoLing committed Sep 8, 2024
1 parent abfff97 commit 799b67e
Show file tree
Hide file tree
Showing 67 changed files with 584 additions and 2,167 deletions.
3 changes: 1 addition & 2 deletions src/main/java/dev/dubhe/anvilcraft/AnvilCraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import dev.dubhe.anvilcraft.init.ModBlocks;
import dev.dubhe.anvilcraft.init.ModComponents;
import dev.dubhe.anvilcraft.init.ModDispenserBehavior;
import dev.dubhe.anvilcraft.init.ModEnchantments;
import dev.dubhe.anvilcraft.init.ModEntities;
import dev.dubhe.anvilcraft.init.ModEvents;
import dev.dubhe.anvilcraft.init.ModItemGroups;
Expand Down Expand Up @@ -67,8 +68,6 @@ public static void init(IEventBus bus) {
ModBlockEntities.register();
ModMenuTypes.register();
ModDispenserBehavior.register();
ModEnchantments.key();
ModResourcePacks.register();
ModComponents.register(bus);

AnvilRecipe.init();
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/dev/dubhe/anvilcraft/block/CrabTrapBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,19 @@ public void onRemove(
super.onRemove(state, level, pos, newState, movedByPiston);
}

private void tryInsertLoot(BlockState state, ServerLevel level, BlockPos pos, ResourceLocation loot) {
private void tryInsertLoot(
BlockState state,
ServerLevel level,
BlockPos pos,
ResourceKey<LootTable> loot
) {
if (state.hasBlockEntity()) {
LootParams lootParams = new LootParams.Builder(level)
.withParameter(LootContextParams.ORIGIN, pos.getCenter())
.create(LootContextParamSets.CHEST);

LootTable lootTable = level.getServer().reloadableRegistries()
.getLootTable(ResourceKey.create(Registries.LOOT_TABLE, loot));
.getLootTable(loot);
ObjectArrayList<ItemStack> items = lootTable.getRandomItems(lootParams);
if (items.isEmpty()) return;
CrabTrapBlockEntity blockEntity = (CrabTrapBlockEntity) level.getBlockEntity(pos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import dev.dubhe.anvilcraft.inventory.ActiveSilencerMenu;
import lombok.Getter;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
Expand Down Expand Up @@ -37,7 +38,7 @@

public class ActiveSilencerBlockEntity extends BlockEntity implements MenuProvider, SoundEventListener, IDiskCloneable {
public static final Codec<List<ResourceLocation>> CODEC =
ResourceLocation.CODEC.listOf().fieldOf("mutedSound").codec();
ResourceLocation.CODEC.listOf().fieldOf("mutedSound").codec();
@Getter
private final Set<ResourceLocation> mutedSound = new CopyOnWriteArraySet<>();

Expand All @@ -49,29 +50,26 @@ public class ActiveSilencerBlockEntity extends BlockEntity implements MenuProvid
public ActiveSilencerBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState blockState) {
super(type, pos, blockState);
range = AABB.ofSize(
Vec3.atCenterOf(pos),
31,
31,
31
Vec3.atCenterOf(pos),
31,
31,
31
);
}

@Override
protected void saveAdditional(@NotNull CompoundTag tag) {
super.saveAdditional(tag);
protected void saveAdditional(@NotNull CompoundTag tag, HolderLookup.@NotNull Provider pRegistries) {
super.saveAdditional(tag, pRegistries);
Tag t = CODEC.encodeStart(NbtOps.INSTANCE, new ArrayList<>(mutedSound))
.getOrThrow(false, x -> {
});
.getOrThrow();
tag.put("MutedSound", t);
}

@Override
public void load(@NotNull CompoundTag tag) {
super.load(tag);
public void loadAdditional(@NotNull CompoundTag tag, HolderLookup.@NotNull Provider pRegistries) {
super.loadAdditional(tag, pRegistries);
mutedSound.addAll(
CODEC.decode(NbtOps.INSTANCE, tag.get("MutedSound"))
.getOrThrow(false, x -> {
}).getFirst()
CODEC.decode(NbtOps.INSTANCE, tag.get("MutedSound")).getOrThrow().getFirst()
);
}

Expand All @@ -82,11 +80,10 @@ public Packet<ClientGamePacketListener> getUpdatePacket() {
}

@Override
public @NotNull CompoundTag getUpdateTag() {
public @NotNull CompoundTag getUpdateTag(HolderLookup.Provider pRegistries) {
CompoundTag tag = new CompoundTag();
Tag t = CODEC.encodeStart(NbtOps.INSTANCE, new ArrayList<>(mutedSound))
.getOrThrow(false, x -> {
});
.getOrThrow();
tag.put("MutedSound", t);
return tag;
}
Expand All @@ -113,10 +110,10 @@ public void setLevel(@NotNull Level level) {
@Override
public AbstractContainerMenu createMenu(int i, @NotNull Inventory inventory, @NotNull Player player) {
return new ActiveSilencerMenu(
ModMenuTypes.ACTIVE_SILENCER.get(),
i,
inventory,
this
ModMenuTypes.ACTIVE_SILENCER.get(),
i,
inventory,
this
);
}

Expand Down Expand Up @@ -148,17 +145,15 @@ public void sync(List<ResourceLocation> sounds) {
@Override
public void storeDiskData(CompoundTag tag) {
Tag t = CODEC.encodeStart(NbtOps.INSTANCE, new ArrayList<>(mutedSound))
.getOrThrow(false, x -> {
});
.getOrThrow();
tag.put("MutedSound", t);
}

@Override
public void applyDiskData(CompoundTag data) {
mutedSound.addAll(
CODEC.decode(NbtOps.INSTANCE, data.get("MutedSound"))
.getOrThrow(false, x -> {
}).getFirst()
CODEC.decode(NbtOps.INSTANCE, data.get("MutedSound"))
.getOrThrow().getFirst()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.Getter;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.item.ItemEntity;
Expand All @@ -29,8 +30,8 @@

@Getter
public abstract class BaseChuteBlockEntity
extends BaseMachineBlockEntity
implements IFilterBlockEntity, IDiskCloneable {
extends BaseMachineBlockEntity
implements IFilterBlockEntity, IDiskCloneable {
private int cooldown = 0;
private final FilteredItemDepository depository = new FilteredItemDepository(9) {
@Override
Expand Down Expand Up @@ -87,15 +88,15 @@ public FilteredItemDepository getFilteredItemDepository() {
public abstract AbstractContainerMenu createMenu(int i, @NotNull Inventory inventory, @NotNull Player player);

@Override
protected void saveAdditional(@NotNull CompoundTag tag) {
super.saveAdditional(tag);
protected void saveAdditional(@NotNull CompoundTag tag, HolderLookup.@NotNull Provider pRegistries) {
super.saveAdditional(tag, pRegistries);
tag.putInt("Cooldown", cooldown);
tag.put("Inventory", depository.serializeNbt());
}

@Override
public void load(@NotNull CompoundTag tag) {
super.load(tag);
public void loadAdditional(@NotNull CompoundTag tag, HolderLookup.@NotNull Provider pRegistries) {
super.loadAdditional(tag, pRegistries);
cooldown = tag.getInt("Cooldown");
depository.deserializeNbt(tag.getCompound("Inventory"));
}
Expand All @@ -108,9 +109,9 @@ public void tick() {
if (cooldown <= 0) {
if (isEnabled()) {
IItemDepository depository = ItemDepositoryHelper.getItemDepository(
getLevel(),
getBlockPos().relative(getInputDirection()),
getInputDirection().getOpposite()
getLevel(),
getBlockPos().relative(getInputDirection()),
getInputDirection().getOpposite()
);
if (depository != null) {
// 尝试从上方容器输入
Expand All @@ -119,12 +120,12 @@ public void tick() {
}
} else {
List<ItemEntity> itemEntities = getLevel().getEntitiesOfClass(
ItemEntity.class, new AABB(getBlockPos().relative(getInputDirection())),
itemEntity -> !itemEntity.getItem().isEmpty());
ItemEntity.class, new AABB(getBlockPos().relative(getInputDirection())),
itemEntity -> !itemEntity.getItem().isEmpty());
int prevSize = itemEntities.size();
for (ItemEntity itemEntity : itemEntities) {
ItemStack remaining = ItemDepositoryHelper.insertItem(
this.depository, itemEntity.getItem(), true
this.depository, itemEntity.getItem(), true
);
if (!remaining.isEmpty()) continue;
ItemDepositoryHelper.insertItem(this.depository, itemEntity.getItem(), false);
Expand All @@ -136,9 +137,9 @@ ItemEntity.class, new AABB(getBlockPos().relative(getInputDirection())),
}
}
depository = ItemDepositoryHelper.getItemDepository(
getLevel(),
getBlockPos().relative(this.getOutputDirection()),
this.getOutputDirection().getOpposite()
getLevel(),
getBlockPos().relative(this.getOutputDirection()),
this.getOutputDirection().getOpposite()
);
if (depository != null) {
// 尝试向朝向容器输出
Expand All @@ -150,8 +151,8 @@ ItemEntity.class, new AABB(getBlockPos().relative(getInputDirection())),
} else {
Vec3 center = getBlockPos().relative(getOutputDirection()).getCenter();
List<ItemEntity> itemEntities = getLevel().getEntitiesOfClass(
ItemEntity.class, new AABB(getBlockPos().relative(getOutputDirection())),
itemEntity -> !itemEntity.getItem().isEmpty());
ItemEntity.class, new AABB(getBlockPos().relative(getOutputDirection())),
itemEntity -> !itemEntity.getItem().isEmpty());
AABB aabb = new AABB(center.add(-0.125, -0.125, -0.125), center.add(0.125, 0.125, 0.125));
if (getLevel().noCollision(aabb)) {
for (int i = 0; i < this.depository.getSlots(); i++) {
Expand All @@ -163,17 +164,17 @@ ItemEntity.class, new AABB(getBlockPos().relative(getOutputDirection())),
sameItemCount += entity.getItem().getCount();
}
}
if (sameItemCount < stack.getItem().getMaxStackSize()) {
if (sameItemCount < stack.getItem().getMaxStackSize(stack)) {
ItemStack droppedItemStack = stack.copy();
int droppedItemCount = Math.min(stack.getCount(),
stack.getMaxStackSize() - sameItemCount);
stack.getMaxStackSize() - sameItemCount);
droppedItemStack.setCount(droppedItemCount);
stack.setCount(stack.getCount() - droppedItemCount);
if (stack.getCount() == 0) stack = ItemStack.EMPTY;
ItemEntity itemEntity = new ItemEntity(
getLevel(), center.x, center.y, center.z,
droppedItemStack,
0, 0, 0);
getLevel(), center.x, center.y, center.z,
droppedItemStack,
0, 0, 0);
itemEntity.setDefaultPickUpDelay();
getLevel().addFreshEntity(itemEntity);
this.depository.setStack(i, stack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ private boolean canPassThrough(Direction direction, BlockPos blockPos) {
BlockState blockState = level.getBlockState(blockPos);
if (blockState.is(ModBlockTags.LASE_CAN_PASS_THROUGH)
|| blockState.is(ModBlockTags.GLASS_BLOCKS)
|| blockState.is(ModBlockTags.FORGE_GLASS_BLOCKS)
|| blockState.is(ModBlockTags.GLASS_PANES)
|| blockState.is(ModBlockTags.FORGE_GLASS_PANES)
|| blockState.is(BlockTags.REPLACEABLE)) return true;
if (!AnvilCraft.config.isLaserDoImpactChecking) return false;
AABB laseBoundingBox = switch (direction.getAxis()) {
Expand Down Expand Up @@ -120,7 +118,7 @@ public void emitLaser(Direction direction) {
? levelToTimeMap.get(Math.min(16, laserLevel) / 4) * 20 : Integer.MAX_VALUE;
if (tickCount >= coldDown) {
tickCount = 0;
if (irradiateBlock.is(ModBlockTags.FORGE_ORES) || irradiateBlock.is(ModBlockTags.ORES)) {
if (irradiateBlock.is(ModBlockTags.ORES)) {
Vec3 blockPos = getBlockPos().relative(direction.getOpposite()).getCenter();
IItemDepository depository = ItemDepositoryHelper.getItemDepository(
getLevel(),
Expand Down Expand Up @@ -152,11 +150,9 @@ public void emitLaser(Direction direction) {
});
if (irradiateBlock.is(Blocks.ANCIENT_DEBRIS))
level.setBlockAndUpdate(irradiateBlockPos, Blocks.NETHERRACK.defaultBlockState());
else if (irradiateBlock.is(ModBlockTags.ORES_IN_GROUND_DEEPSLATE)
|| irradiateBlock.is(ModBlockTags.FORGE_ORES_IN_GROUND_DEEPSLATE))
else if (irradiateBlock.is(ModBlockTags.ORES_IN_GROUND_DEEPSLATE))
level.setBlockAndUpdate(irradiateBlockPos, Blocks.DEEPSLATE.defaultBlockState());
else if (irradiateBlock.is(ModBlockTags.ORES_IN_GROUND_NETHERRACK)
|| irradiateBlock.is(ModBlockTags.FORGE_ORES_IN_GROUND_NETHERRACK))
else if (irradiateBlock.is(ModBlockTags.ORES_IN_GROUND_NETHERRACK))
level.setBlockAndUpdate(irradiateBlockPos, Blocks.NETHERRACK.defaultBlockState());
else level.setBlockAndUpdate(irradiateBlockPos, Blocks.STONE.defaultBlockState());
/* else {
Expand Down
Loading

0 comments on commit 799b67e

Please sign in to comment.