Skip to content

Commit

Permalink
Fix loot tables
Browse files Browse the repository at this point in the history
  • Loading branch information
ustc-zzzz committed Aug 16, 2024
1 parent c7efa4d commit 8bbb863
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/teacon/slides/ModRegistries.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public final class ModRegistries {
public static final DeferredHolder<MenuType<?>, MenuType<SlideItemContainerMenu>> SLIDE_ITEM_MENU;

static {
PROJECTOR_BLOCK = DeferredHolder.create(BuiltInRegistries.BLOCK.key(), PROJECTOR_ID);
SLIDE_ITEM = DeferredHolder.create(BuiltInRegistries.ITEM.key(), SLIDE_ITEM_ID);
PROJECTOR_BLOCK = DeferredHolder.create(BuiltInRegistries.BLOCK.key(), PROJECTOR_ID);
SLIDE_ENTRY = DeferredHolder.create(BuiltInRegistries.DATA_COMPONENT_TYPE.key(), SLIDE_ENTRY_ID);
PROJECTOR_BLOCK_ENTITY = DeferredHolder.create(BuiltInRegistries.BLOCK_ENTITY_TYPE.key(), PROJECTOR_ID);
PROJECTOR_MENU = DeferredHolder.create(BuiltInRegistries.MENU.key(), PROJECTOR_ID);
Expand All @@ -74,9 +74,9 @@ public final class ModRegistries {

@SubscribeEvent
public static void register(final RegisterEvent event) {
event.register(BuiltInRegistries.ITEM.key(), SLIDE_ITEM_ID, SlideItem::new);
event.register(BuiltInRegistries.BLOCK.key(), PROJECTOR_ID, ProjectorBlock::new);
event.register(BuiltInRegistries.ITEM.key(), PROJECTOR_ID, ProjectorItem::new);
event.register(BuiltInRegistries.ITEM.key(), SLIDE_ITEM_ID, SlideItem::new);
event.register(BuiltInRegistries.DATA_COMPONENT_TYPE.key(), SLIDE_ENTRY_ID, SlideItem.Entry::createComponentType);
event.register(BuiltInRegistries.BLOCK_ENTITY_TYPE.key(), PROJECTOR_ID, ProjectorBlockEntity::create);
event.register(BuiltInRegistries.MENU.key(), PROJECTOR_ID, ProjectorContainerMenu::create);
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/org/teacon/slides/block/ProjectorBlockEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.NonNullList;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.Connection;
Expand All @@ -18,6 +21,7 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.component.ItemContainerContents;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
Expand Down Expand Up @@ -92,6 +96,36 @@ public boolean hasCustomOutlineRendering(Player player) {
return handItems.contains(ModRegistries.PROJECTOR_BLOCK.get().asItem());
}

@Override
protected void applyImplicitComponents(BlockEntity.DataComponentInput componentInput) {
super.applyImplicitComponents(componentInput);
var container = componentInput.getOrDefault(DataComponents.CONTAINER, ItemContainerContents.EMPTY);
var count = container.getSlots();
for (var i = 0; i < ProjectorBlock.SLIDE_ITEM_HANDLER_CAPACITY; ++i) {
var j = i + ProjectorBlock.SLIDE_ITEM_HANDLER_CAPACITY;
this.mItemsDisplayed.setStackInSlot(i, i < count ? container.getStackInSlot(i) : ItemStack.EMPTY);
this.mItemsToDisplay.setStackInSlot(i, j < count ? container.getStackInSlot(j) : ItemStack.EMPTY);
}
}

@Override
protected void collectImplicitComponents(DataComponentMap.Builder components) {
super.collectImplicitComponents(components);
var containerItems = NonNullList.withSize(ProjectorBlock.SLIDE_ITEM_HANDLER_CAPACITY * 2, ItemStack.EMPTY);
for (var i = 0; i < ProjectorBlock.SLIDE_ITEM_HANDLER_CAPACITY; ++i) {
var j = i + ProjectorBlock.SLIDE_ITEM_HANDLER_CAPACITY;
containerItems.set(i, this.mItemsDisplayed.getStackInSlot(i));
containerItems.set(j, this.mItemsToDisplay.getStackInSlot(i));
}
components.set(DataComponents.CONTAINER, ItemContainerContents.fromItems(containerItems));
}

@Override
@SuppressWarnings("deprecation")
public void removeComponentsFromTag(CompoundTag tag) {
tag.remove("Items");
}

@Override
public ClientboundBlockEntityDataPacket getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/org/teacon/slides/item/ProjectorItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
import net.minecraft.FieldsAreNonnullByDefault;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.BlockPos;
import net.minecraft.core.NonNullList;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Rarity;
import net.minecraft.world.item.component.ItemContainerContents;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import org.teacon.slides.ModRegistries;
import org.teacon.slides.block.ProjectorBlock;
import org.teacon.slides.block.ProjectorBlockEntity;
import org.teacon.slides.inventory.ProjectorContainerMenu;

Expand All @@ -23,7 +27,8 @@
public final class ProjectorItem extends BlockItem {

public ProjectorItem() {
super(ModRegistries.PROJECTOR_BLOCK.get(), new Item.Properties().rarity(Rarity.RARE));
super(ModRegistries.PROJECTOR_BLOCK.get(),
new Item.Properties().rarity(Rarity.RARE).component(DataComponents.CONTAINER, getDefaultContents()));
}

@Override
Expand All @@ -32,10 +37,15 @@ protected boolean updateCustomBlockEntityTag(BlockPos pos, Level level, @Nullabl
final boolean superResult = super.updateCustomBlockEntityTag(pos, level, player, stack, state);
if (!superResult && !level.isClientSide && player != null) {
if (level.getBlockEntity(pos) instanceof ProjectorBlockEntity tile) {
tile.getItemsDisplayed().insertItem(0, ModRegistries.SLIDE_ITEM.get().getDefaultInstance(), false);
ProjectorContainerMenu.openGui(player, tile);
}
}
return superResult;
}

private static ItemContainerContents getDefaultContents() {
var list = NonNullList.withSize(ProjectorBlock.SLIDE_ITEM_HANDLER_CAPACITY * 2, ItemStack.EMPTY);
list.set(0, ModRegistries.SLIDE_ITEM.get().getDefaultInstance());
return ItemContainerContents.fromItems(list);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@
"pools": [
{
"rolls": 1,
"bonus_rolls": 0.0,
"entries": [
{
"type": "minecraft:item",
"name": "slide_show:projector",
"functions": [
{
"function": "minecraft:copy_name",
"function": "minecraft:copy_components",
"include": [
"minecraft:container"
],
"source": "block_entity"
}
]
}
]
}
]
],
"random_sequence": "slide_show:blocks/projector"
}

0 comments on commit 8bbb863

Please sign in to comment.