Skip to content

Commit

Permalink
Merge pull request #441 from Gu-ZT/releases/1.20.1
Browse files Browse the repository at this point in the history
注册并实现含生物琥珀块,怨恨琥珀块
  • Loading branch information
Gu-ZT authored Apr 28, 2024
2 parents c005154 + 4e8f85e commit 51c95c7
Show file tree
Hide file tree
Showing 57 changed files with 1,355 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,29 @@ private ItemDepositoryHelper() {
* @param source 物品源
* @param maxAmount 导出的最大数量
* @param predicate 能够导出的物品
* @param level 当前 Level
* @param pos 导出目标的位置
* @param direction 导出目标的方向
* @param target 目标
*/
@SuppressWarnings({"DuplicatedCode", "UnreachableCode"})
@SuppressWarnings("DuplicatedCode")
public static void exportToTarget(
IItemDepository source,
@NotNull IItemDepository source,
int maxAmount,
Predicate<ItemStack> predicate,
@NotNull Level level,
BlockPos pos,
Direction direction
IItemDepository target
) {
if (level.getBlockState(pos).hasBlockEntity()) {
IItemDepository target = getItemDepository(level, pos, direction);
for (int srcIndex = 0; srcIndex < source.getSlots(); srcIndex++) {
ItemStack sourceStack = source.extract(srcIndex, Integer.MAX_VALUE, true);
if (sourceStack.isEmpty() || !predicate.test(sourceStack)) {
continue;
}
ItemStack remainder = insertItem(target, sourceStack, true);
int amountToInsert = sourceStack.getCount() - remainder.getCount();
if (amountToInsert > 0) {
sourceStack = source.extract(srcIndex, Math.min(maxAmount, amountToInsert), false);
remainder = insertItem(target, sourceStack, false);
maxAmount -= Math.min(maxAmount, amountToInsert);
source.insert(srcIndex, remainder, false);
}
if (maxAmount <= 0) return;
for (int srcIndex = 0; srcIndex < source.getSlots(); srcIndex++) {
ItemStack sourceStack = source.extract(srcIndex, Integer.MAX_VALUE, true);
if (sourceStack.isEmpty() || !predicate.test(sourceStack)) {
continue;
}
ItemStack remainder = insertItem(target, sourceStack, true);
int amountToInsert = sourceStack.getCount() - remainder.getCount();
if (amountToInsert > 0) {
sourceStack = source.extract(srcIndex, Math.min(maxAmount, amountToInsert), false);
remainder = insertItem(target, sourceStack, false);
maxAmount -= Math.min(maxAmount, amountToInsert);
source.insert(srcIndex, remainder, false);
}
if (maxAmount <= 0) return;
}
}

Expand All @@ -75,35 +68,28 @@ public static void exportToTarget(
* @param target 物品目标
* @param maxAmount 导入的最大数量
* @param predicate 能够导入的物品
* @param level 当前 Level
* @param pos 导入物品源的位置
* @param direction 导入物品源的方向
* @param source 物品源
*/
@SuppressWarnings({"DuplicatedCode", "UnreachableCode"})
@SuppressWarnings("DuplicatedCode")
public static void importToTarget(
IItemDepository target,
int maxAmount,
Predicate<ItemStack> predicate,
@NotNull Level level,
BlockPos pos,
Direction direction
@NotNull IItemDepository source
) {
if (level.getBlockState(pos).hasBlockEntity()) {
IItemDepository source = getItemDepository(level, pos, direction);
for (int srcIndex = 0; srcIndex < source.getSlots(); srcIndex++) {
ItemStack sourceStack = source.extract(srcIndex, Integer.MAX_VALUE, true);
if (sourceStack.isEmpty() || !predicate.test(sourceStack)) {
continue;
}
ItemStack remainder = insertItem(target, sourceStack, true);
int amountToInsert = sourceStack.getCount() - remainder.getCount();
if (amountToInsert > 0) {
sourceStack = source.extract(srcIndex, Math.min(maxAmount, amountToInsert), false);
insertItem(target, sourceStack, false);
maxAmount -= Math.min(maxAmount, amountToInsert);
}
if (maxAmount <= 0) return;
for (int srcIndex = 0; srcIndex < source.getSlots(); srcIndex++) {
ItemStack sourceStack = source.extract(srcIndex, Integer.MAX_VALUE, true);
if (sourceStack.isEmpty() || !predicate.test(sourceStack)) {
continue;
}
ItemStack remainder = insertItem(target, sourceStack, true);
int amountToInsert = sourceStack.getCount() - remainder.getCount();
if (amountToInsert > 0) {
sourceStack = source.extract(srcIndex, Math.min(maxAmount, amountToInsert), false);
insertItem(target, sourceStack, false);
maxAmount -= Math.min(maxAmount, amountToInsert);
}
if (maxAmount <= 0) return;
}
}

Expand Down
27 changes: 27 additions & 0 deletions common/src/main/java/dev/dubhe/anvilcraft/block/MobAmberBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.dubhe.anvilcraft.block;

import dev.dubhe.anvilcraft.init.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class MobAmberBlock extends BaseEntityBlock {
public MobAmberBlock(Properties properties) {
super(properties);
}

@Nullable
@Override
public BlockEntity newBlockEntity(@NotNull BlockPos pos, @NotNull BlockState state) {
return ModBlockEntities.MOB_AMBER_BLOCK.create(pos, state);
}

@Override
public @NotNull RenderShape getRenderShape(@NotNull BlockState state) {
return RenderShape.MODEL;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.dubhe.anvilcraft.block;

import dev.dubhe.anvilcraft.init.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class ResentfulAmberBlock extends MobAmberBlock {
public ResentfulAmberBlock(Properties properties) {
super(properties);
}

@Nullable
@Override
public BlockEntity newBlockEntity(@NotNull BlockPos pos, @NotNull BlockState state) {
return ModBlockEntities.RESENTFUL_AMBER_BLOCK.create(pos, state);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.architectury.injectables.annotations.ExpectPlatform;
import dev.dubhe.anvilcraft.AnvilCraft;
import dev.dubhe.anvilcraft.api.depository.FilteredItemDepository;
import dev.dubhe.anvilcraft.api.depository.IItemDepository;
import dev.dubhe.anvilcraft.api.depository.ItemDepositoryHelper;
import dev.dubhe.anvilcraft.block.ChuteBlock;
import dev.dubhe.anvilcraft.init.ModBlocks;
Expand Down Expand Up @@ -109,44 +110,50 @@ public void load(@NotNull CompoundTag tag) {
public void tick() {
if (cooldown <= 0) {
if (getBlockState().getValue(ChuteBlock.ENABLED)) {
if (ItemDepositoryHelper.getItemDepository(getLevel(),
getBlockPos().relative(Direction.UP), Direction.UP.getOpposite()) != null) {
IItemDepository depository = ItemDepositoryHelper.getItemDepository(
getLevel(),
getBlockPos().relative(Direction.UP),
Direction.UP.getOpposite()
);
if (depository != null) {
// 尝试从上方容器输入
ItemDepositoryHelper.importToTarget(depository,
64, stack -> true, getLevel(),
getBlockPos().relative(Direction.UP), Direction.UP.getOpposite());
ItemDepositoryHelper.importToTarget(this.depository, 64, stack -> true, depository);
} else {
List<ItemEntity> itemEntities = getLevel().getEntitiesOfClass(
ItemEntity.class, new AABB(getBlockPos().relative(Direction.UP)),
itemEntity -> !itemEntity.getItem().isEmpty());
for (ItemEntity itemEntity : itemEntities) {
ItemStack remaining = ItemDepositoryHelper.insertItem(depository, itemEntity.getItem(), true);
ItemStack remaining = ItemDepositoryHelper.insertItem(
this.depository, itemEntity.getItem(), true
);
if (!remaining.isEmpty()) continue;
ItemDepositoryHelper.insertItem(depository, itemEntity.getItem(), false);
ItemDepositoryHelper.insertItem(this.depository, itemEntity.getItem(), false);
itemEntity.kill();
break;
}
}
if (ItemDepositoryHelper.getItemDepository(getLevel(),
getBlockPos().relative(getDirection()), getDirection().getOpposite()) != null) {
depository = ItemDepositoryHelper.getItemDepository(
getLevel(),
getBlockPos().relative(this.getDirection()),
this.getDirection().getOpposite()
);
if (depository != null) {
// 尝试向朝向容器输出
if (!depository.isEmpty()) {
ItemDepositoryHelper.exportToTarget(depository,
64, stack -> true, getLevel(),
getBlockPos().relative(getDirection()), getDirection().getOpposite());
if (!this.depository.isEmpty()) {
ItemDepositoryHelper.exportToTarget(this.depository, 64, stack -> true, depository);
}
} else {
Vec3 center = getBlockPos().relative(getDirection()).getCenter();
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 < depository.getSlots(); i++) {
ItemStack stack = depository.getStack(i);
for (int i = 0; i < this.depository.getSlots(); i++) {
ItemStack stack = this.depository.getStack(i);
if (!stack.isEmpty()) {
ItemEntity itemEntity = new ItemEntity(
getLevel(), center.x, center.y, center.z, stack, 0, 0, 0);
itemEntity.setDefaultPickUpDelay();
getLevel().addFreshEntity(itemEntity);
depository.setStack(i, ItemStack.EMPTY);
this.depository.setStack(i, ItemStack.EMPTY);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package dev.dubhe.anvilcraft.block.entity;

import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.level.Level;
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 org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

public abstract class HasMobBlockEntity extends BlockEntity {
private CompoundTag entity = null;
private Entity displayEntity = null;

protected HasMobBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState blockState) {
super(type, pos, blockState);
}

/**
* 设置实体
*/
public void setEntity(Entity entity) {
if (entity == null) return;
if (this.entity == null) this.entity = new CompoundTag();
entity.save(this.entity);
this.entity.remove(Entity.UUID_TAG);
}

/**
* 设置实体
*/
public void setEntity(CompoundTag entity) {
if (entity == null) return;
this.entity = entity;
this.entity.remove(Entity.UUID_TAG);
}

@Override
protected void saveAdditional(@NotNull CompoundTag tag) {
super.saveAdditional(tag);
if (this.entity != null) {
tag.put("entity", this.entity);
}
}

@Override
public void load(@NotNull CompoundTag tag) {
if (tag.contains("entity")) {
this.entity = tag.getCompound("entity");
if (this.level != null) {
this.getEntity(this.level);
}
}
super.load(tag);
}

@Override
public @NotNull CompoundTag getUpdateTag() {
return this.saveWithoutMetadata();
}

@Nullable
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}

/**
* @return 实体
*/
@Nullable
public Entity getOrCreateDisplayEntity(Level level) {
if (this.displayEntity == null && this.entity != null) {
this.getEntity(level);
}
return this.displayEntity;
}

private void getEntity(Level level) {
Optional<EntityType<?>> optional = EntityType.by(this.entity);
if (optional.isEmpty()) return;
EntityType<?> type = optional.get();
Entity entity = type.create(level);
if (entity == null) return;
entity.load(this.entity);
this.displayEntity = entity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.dubhe.anvilcraft.block.entity;

import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;

public class MobAmberBlockEntity extends HasMobBlockEntity {
protected MobAmberBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState blockState) {
super(type, pos, blockState);
}

public static @NotNull MobAmberBlockEntity createBlockEntity(
BlockEntityType<?> type, BlockPos pos, BlockState blockState
) {
return new MobAmberBlockEntity(type, pos, blockState);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.dubhe.anvilcraft.block.entity;

import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;

public class ResentfulAmberBlock extends MobAmberBlockEntity {
private ResentfulAmberBlock(BlockEntityType<?> type, BlockPos pos, BlockState blockState) {
super(type, pos, blockState);
}

public static @NotNull ResentfulAmberBlock createBlockEntity(
BlockEntityType<?> type, BlockPos pos, BlockState blockState
) {
return new ResentfulAmberBlock(type, pos, blockState);
}
}
Loading

0 comments on commit 51c95c7

Please sign in to comment.