Skip to content

Commit

Permalink
yay
Browse files Browse the repository at this point in the history
  • Loading branch information
Leclowndu93150 committed Dec 27, 2024
1 parent 033c7ba commit ec67125
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.portingdeadmods.plonk.common.block;

import com.mojang.serialization.MapCodec;
import com.portingdeadmods.plonk.common.registry.RegistryTileEntities;
import com.portingdeadmods.plonk.common.tile.TilePlacedItems;
import com.portingdeadmods.plonk.common.util.ItemUtils;
Expand All @@ -8,12 +9,10 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.Container;
import net.minecraft.world.Containers;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.*;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
Expand Down Expand Up @@ -72,6 +71,12 @@ public BlockPlacedItems(BlockBehaviour.Properties properties) {
this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.UP));
}

@Override
protected MapCodec<? extends BaseEntityBlock> codec() {
//TODO: Implement this
return null;
}

@Nullable
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, BlockState pState, BlockEntityType<T> pBlockEntityType) {
if (pLevel.isClientSide) {
Expand Down Expand Up @@ -188,11 +193,11 @@ public void onRemove(BlockState state, Level worldIn, BlockPos pos, BlockState n
*
* @return -1 if no hit otherwise the closest slot
* @see Entity#pick(double, float, boolean)
* @see ForgeMod#BLOCK_REACH
* @see Attributes#BLOCK_INTERACTION_RANGE
*/
protected int getPickedSlot(TilePlacedItems tile, BlockPos pos, Player player) {
if (picking.get()) return -1;
double blockReachDistance = Objects.requireNonNull(player.getAttribute(ForgeMod.BLOCK_REACH.get())).getValue();
double blockReachDistance = player.getAttribute(Attributes.BLOCK_INTERACTION_RANGE).getValue();
float partialTicks = 0.0f;

// Might have issues if player is moving fast or turning their vision fast
Expand All @@ -213,16 +218,15 @@ protected int getPickedSlot(TilePlacedItems tile, BlockPos pos, Player player) {
}

@Override
@SuppressWarnings("deprecation")
public InteractionResult use(BlockState state, Level worldIn, BlockPos pos, Player player, InteractionHand handIn, BlockHitResult hit) {
if (worldIn.isClientSide) return InteractionResult.SUCCESS;
protected ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult) {
if (level.isClientSide) return ItemInteractionResult.SUCCESS;

TilePlacedItems tile = (TilePlacedItems) worldIn.getBlockEntity(pos);
if (tile == null) return InteractionResult.SUCCESS;
TilePlacedItems tile = (TilePlacedItems) level.getBlockEntity(pos);
if (tile == null) return ItemInteractionResult.SUCCESS;

int slot = getPickedSlot(tile, pos, player);
if (slot >= 0) {
ItemStack stack = tile.getItem(slot);
stack = tile.getItem(slot);
if (!stack.isEmpty()) {
//ItemUtils.dropItemWithinBlock(worldId, x, y, z, stack);
if (player.isShiftKeyDown()) {
Expand All @@ -234,9 +238,9 @@ public InteractionResult use(BlockState state, Level worldIn, BlockPos pos, Play
tile.setChanged();
tile.clean();
}
return InteractionResult.CONSUME;
return ItemInteractionResult.CONSUME;
}
return super.use(state, worldIn, pos, player, handIn, hit);
return super.useItemOn(stack,state, level, pos, player, hand, hitResult);
}

@Override
Expand Down Expand Up @@ -309,4 +313,4 @@ public boolean addDestroyEffects(BlockState state, Level world, BlockPos pos, Pa
public SoundType getSoundType(BlockState state, LevelReader world, BlockPos pos, @Nullable Entity entity) {
return super.getSoundType(state, world, pos, entity);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ public ItemBlockPlacedItems(Item.Properties builder) {
}

public void setHeldStack(ItemStack stack, ItemStack held, int renderType) {
stack.set(DataComponents.HELD, true);
stack.set(DataComponents.RENDER_TYPE, renderType);
stack.set(DataComponents.TAG_HELD, true);
stack.set(DataComponents.TAG_RENDER_TYPE, renderType);
}

public ItemStack getHeldStack(ItemStack stack) {
if (!stack.has(DataComponents.HELD))
if (!stack.has(DataComponents.TAG_HELD))
return ItemStack.EMPTY;

ItemStack held = new ItemStack(stack.getItem());
held.set(DataComponents.HELD,stack.get(DataComponents.HELD));
held.set(DataComponents.TAG_HELD,stack.get(DataComponents.TAG_HELD));
return held;
}

public int getHeldRenderType(ItemStack stack) {
if (!stack.has(DataComponents.RENDER_TYPE))
if (!stack.has(DataComponents.TAG_RENDER_TYPE))
return 0;
return stack.get(DataComponents.RENDER_TYPE);
return stack.get(DataComponents.TAG_RENDER_TYPE);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.portingdeadmods.plonk.common.tile;

import com.google.common.collect.ImmutableMap;
import com.portingdeadmods.plonk.common.block.BlockPlacedItems;
import com.portingdeadmods.plonk.common.config.PlonkConfig;
import com.portingdeadmods.plonk.common.registry.RegistryBlocks;
import com.portingdeadmods.plonk.common.registry.RegistryTileEntities;
import com.portingdeadmods.plonk.common.util.ItemUtils;
import com.portingdeadmods.plonk.common.util.bound.Box;
import com.portingdeadmods.plonk.common.util.bound.BoxCollection;
import com.google.common.collect.ImmutableMap;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
Expand Down Expand Up @@ -73,7 +74,10 @@ public class TilePlacedItems extends BlockEntity implements WorldlyContainer {
HEIGHT_ITEM,
0.75
);
public static final String TAG_VERSION = "Version";
public static final String TAG_TILE_ROTATION = "TileRotation";
public static final int TILE_ROTATION_COUNT = 4;
public static final String TAG_ITEM_ROTATION = "ItemRotation";
public static final String TAG_ITEMS = "Items";
public static final String TAG_SLOT = "Slot";
public static final String TAG_RENDER_TYPE = "RenderType";
Expand Down Expand Up @@ -284,12 +288,9 @@ private void updateContentsBoxes(int count) {
contentsBoxes = builder.build();
}

/**
* @see ChestBlockEntity
*/
@Override
public void load(CompoundTag tag) {
super.load(tag);
protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.loadAdditional(tag, registries);
TagUpgrader.upgrade(tag);
this.tileRotation = tag.getInt(TAG_TILE_ROTATION);
ListTag tagItems = tag.getList(TAG_ITEMS, 10);
Expand All @@ -304,17 +305,22 @@ public void load(CompoundTag tag) {
int itemRotation = tagItem.getInt(TAG_ITEM_ROTATION);

if (slot < this.contents.size()) {
this.contents.set(slot, ItemStack.of(tagItem));
this.contents.set(slot, ItemStack.parseOptional(registries,tagItem));
this.contentsMeta[slot] = new ItemMeta(renderType, itemRotation);
}
}

this.needsCleaning = true;
}

/**
* @see ChestBlockEntity
*/


@Override
public void saveAdditional(CompoundTag tag) {
super.saveAdditional(tag);
public void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.saveAdditional(tag, registries);
tag.putInt(TAG_VERSION, Tag_VERSION);
tag.putInt(TAG_TILE_ROTATION, tileRotation);
ListTag tagItems = new ListTag();
Expand All @@ -325,7 +331,8 @@ public void saveAdditional(CompoundTag tag) {
tagItem.putByte(TAG_SLOT, (byte) slot);
tagItem.putInt(TAG_RENDER_TYPE, this.contentsMeta[slot].renderType);
tagItem.putInt(TAG_ITEM_ROTATION, this.contentsMeta[slot].rotation);
this.contents.get(slot).save(tagItem);
this.contents.get(slot).save(registries);
//FIX: i think that's correct
tagItems.add(tagItem);
}
}
Expand Down Expand Up @@ -368,10 +375,11 @@ public Packet<ClientGamePacketListener> getUpdatePacket() {
}

@Override
public CompoundTag getUpdateTag() {
return this.saveWithoutMetadata();
public CompoundTag getUpdateTag(HolderLookup.Provider registries) {
return this.saveWithoutMetadata(registries);
}

/*
@Override
@OnlyIn(Dist.CLIENT)
public AABB getRenderBoundingBox() {
Expand All @@ -380,6 +388,7 @@ public AABB getRenderBoundingBox() {
// return this.contentsBoxes.getRenderBoundingBox(this);
return BlockEntity.INFINITE_EXTENT_AABB;
}
*/

@Override
public int getContainerSize() {
Expand Down Expand Up @@ -593,4 +602,4 @@ private interface Upgrade {
void apply(CompoundTag tag);
}
}
}
}
15 changes: 4 additions & 11 deletions src/main/java/com/portingdeadmods/plonk/data/DataComponents.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,18 @@
import net.minecraft.network.codec.ByteBufCodecs;
import net.neoforged.neoforge.registries.DeferredRegister;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

public class DataComponents {

public static final DeferredRegister.DataComponents COMPONENTS = DeferredRegister.createDataComponents(Plonk.MOD_ID);

public static final Supplier<DataComponentType<Boolean>> HELD = COMPONENTS.registerComponentType("Held",
public static final Supplier<DataComponentType<Boolean>> TAG_HELD = COMPONENTS.registerComponentType("Held",
builder -> builder.persistent(Codec.BOOL).networkSynchronized(ByteBufCodecs.BOOL));

public static final Supplier<DataComponentType<Integer>> RENDER_TYPE = COMPONENTS.registerComponentType("RenderType",
builder -> builder.persistent(Codec.INT).networkSynchronized(ByteBufCodecs.INT));

public static final Supplier<DataComponentType<Integer>> TAG_VERSION = COMPONENTS.registerComponentType("TagVersion",
builder -> builder.persistent(Codec.INT).networkSynchronized(ByteBufCodecs.INT));

public static final Supplier<DataComponentType<Integer>> TILE_ROTATION = COMPONENTS.registerComponentType("TileRotation",
builder -> builder.persistent(Codec.INT).networkSynchronized(ByteBufCodecs.INT));

public static final Supplier<DataComponentType<Integer>> TAG_ITEM_ROTATION = COMPONENTS.registerComponentType("TagItemRotation",
public static final Supplier<DataComponentType<Integer>> TAG_RENDER_TYPE = COMPONENTS.registerComponentType("RenderType",
builder -> builder.persistent(Codec.INT).networkSynchronized(ByteBufCodecs.INT));


Expand Down

0 comments on commit ec67125

Please sign in to comment.