Skip to content

Commit

Permalink
Merge pull request #200 from Gu-ZT/Depository
Browse files Browse the repository at this point in the history
完全重写方块实体
  • Loading branch information
DancingSnow0517 authored Apr 10, 2024
2 parents beb8206 + 5c16b8b commit b999ea3
Show file tree
Hide file tree
Showing 48 changed files with 2,479 additions and 1,224 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.dubhe.anvilcraft.api;

import net.minecraft.nbt.CompoundTag;
import org.jetbrains.annotations.NotNull;

public interface INamedTagSerializable {
/**
* 向NBT中序列化数据
*/
@NotNull CompoundTag serializeNBT();

/**
* 从NBT中反序列化数据
*
* @param tag 读取数据的NBT来源
*/
void deserializeNBT(@NotNull CompoundTag tag);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package dev.dubhe.anvilcraft.api.depository;

import lombok.Getter;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;


@Getter
public class FilteredItemDepository extends ItemDepository {
private boolean filterEnabled = false;
private final NonNullList<ItemStack> filteredItems;
private final NonNullList<Boolean> disabled;

public FilteredItemDepository(int size) {
super(size);
this.filteredItems = NonNullList.withSize(size, ItemStack.EMPTY);
this.disabled = NonNullList.withSize(size, false);
}

/**
* 设置是否启用过滤
*
* @param filterEnabled 是否启用过滤
*/
public void setFilterEnabled(boolean filterEnabled) {
this.filteredItems.clear();
this.filterEnabled = filterEnabled;
if (this.filterEnabled) {
for (int i = 0; i < this.getSlots(); i++) {
ItemStack stack = this.getStack(i);
if (stack.isEmpty()) continue;
this.setFilter(i, stack);
}
}
}

@Override
public boolean isItemValid(int slot, ItemStack stack) {
if (!this.filterEnabled) return !this.isSlotDisabled(slot);
return !this.isSlotDisabled(slot) && this.isFiltered(slot, stack);
}

@Override
public boolean canPlaceItem(int slot, ItemStack stack) {
return super.isItemValid(slot, stack);
}

@Override
public void setStack(int slot, ItemStack stack) {
if (filterEnabled) this.setFilter(slot, stack);
super.setStack(slot, stack);
}

/**
* 判断指定槽位是否被禁用
*
* @param slot 槽位
* @return 指定槽位是否被禁用
*/
public boolean isSlotDisabled(int slot) {
if (!this.filterEnabled) return this.disabled.get(slot);
else return this.disabled.get(slot) || (getStack(slot).isEmpty() && this.filteredItems.get(slot).isEmpty());
}

/**
* 为指定槽位设定禁用情况
*
* @param slot 槽位
* @param disable 禁用情况
*/
public void setSlotDisabled(int slot, boolean disable) {
this.filteredItems.set(slot, ItemStack.EMPTY);
this.disabled.set(slot, disable);
}

/**
* 使指定槽位禁用情况翻转
*
* @param slot 槽位
* @return 指定槽位的禁用情况
*/
public boolean cycleDisabled(int slot) {
boolean disable = !this.disabled.get(slot);
this.setSlotDisabled(slot, disable);
return disable;
}

/**
* 判断指定槽位是否允许放入指定物品堆栈
*
* @param slot 槽位
* @param stack 物品堆栈
* @return 指定槽位是否允许放入指定物品堆栈
*/
public boolean isFiltered(int slot, ItemStack stack) {
ItemStack filter = this.filteredItems.get(slot);
return filter.isEmpty() || ItemStack.isSameItem(filter, stack);
}

/**
* 设置指定槽位的过滤
*
* @param slot 槽位
* @param stack 过滤物品堆栈(不检查NBT)
*/
public void setFilter(int slot, @NotNull ItemStack stack) {
if (stack.isEmpty()) return;
this.setSlotDisabled(slot, false);
this.filteredItems.set(slot, new ItemStack(stack.getItem(), 1));
}

/**
* 获取指定槽位上的过滤
*
* @param slot 槽位
* @return 指定槽位上的过滤
*/
public ItemStack getFilter(int slot) {
return this.filteredItems.get(slot);
}

@Override
public @NotNull CompoundTag serializeNBT() {
CompoundTag compoundTag = new CompoundTag();
compoundTag.putBoolean("filterEnabled", this.filterEnabled);
ListTag listTag = new ListTag();
int slots = this.getSlots();
compoundTag.putInt("Size", slots);
for (int slot = 0; slot < slots; slot++) {
ItemStack stack = this.getStack(slot);
CompoundTag itemTag = new CompoundTag();
itemTag.putInt("Slot", slot);
stack.save(itemTag);
ItemStack filter = this.filteredItems.get(slot);
if (!filter.isEmpty()) {
CompoundTag filtered = new CompoundTag();
filter.save(filtered);
itemTag.put("filtered", filtered);
}
itemTag.putBoolean("disabled", this.disabled.get(slot));
listTag.add(itemTag);
}
if (!listTag.isEmpty()) compoundTag.put("Items", listTag);
return compoundTag;
}

@Override
public void deserializeNBT(@NotNull CompoundTag tag) {
if (!tag.contains("Items")) return;
this.filterEnabled = tag.getBoolean("filterEnabled");
ListTag listTag = tag.getList("Items", Tag.TAG_COMPOUND);
int slots = this.getSlots();
for (int i = 0; i < listTag.size(); i++) {
CompoundTag itemTag = listTag.getCompound(i);
int slot = itemTag.getInt("Slot");
if (slot < 0 || slot >= slots) continue;
this.getStacks().set(slot, ItemStack.of(itemTag));
if (tag.contains("filtered")) this.filteredItems.set(i, ItemStack.of(itemTag.getCompound("filtered")));
this.disabled.set(i, itemTag.getBoolean("disabled"));
}
}

public static class Pollable extends FilteredItemDepository {

public Pollable(int size) {
super(size);
}

@Override
public boolean isItemValid(int slot, ItemStack stack) {
return getEmptyOrSmallerSlot(stack) == slot && super.isItemValid(slot, stack);
}

@Override
public boolean canPlaceItem(int slot, ItemStack stack) {
return super.canPlaceItem(slot, stack);
}

private int getEmptyOrSmallerSlot(ItemStack stack) {
int slotCount = this.getSlots();
int slot = -1;
int countInSlot = Integer.MAX_VALUE;
for (int i = slotCount - 1; i >= 0; i--) {
if (this.isSlotDisabled(i)) continue;
ItemStack stackInSlot = this.getStack(i);
if (!stackInSlot.isEmpty() && !ItemStack.isSameItemSameTags(stackInSlot, stack)) continue;
int stackInSlotCount = stackInSlot.getCount();
if (stackInSlotCount <= countInSlot && stackInSlotCount < this.getSlotLimit(i)) {
slot = i;
countInSlot = stackInSlotCount;
}
}
return slot;
}
}
}
Loading

0 comments on commit b999ea3

Please sign in to comment.