Skip to content

Commit

Permalink
wardrobe ui and independ CBlockEntity
Browse files Browse the repository at this point in the history
  • Loading branch information
khjxiaogu committed Feb 12, 2025
1 parent 022ab2f commit 2c816a5
Show file tree
Hide file tree
Showing 26 changed files with 565 additions and 145 deletions.
10 changes: 6 additions & 4 deletions src/main/java/com/teammoeg/chorda/block/CBlockInterfaces.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@

package com.teammoeg.chorda.block;

import com.teammoeg.chorda.block.entity.BlockStateAccess;

import blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;

public class CBlockInterfaces {
public interface IActiveState extends IEBlockInterfaces.BlockstateProvider {
public interface IActiveState extends BlockStateAccess {
default boolean getIsActive() {
BlockState state = this.getState();
BlockState state = this.getBlock();
return state.hasProperty(BlockStateProperties.LIT) ? state.getValue(BlockStateProperties.LIT) : false;
}

Expand All @@ -36,10 +38,10 @@ default boolean getIsActive() {
* @return true if the state was changed, false otherwise
*/
default boolean setActive(boolean active) {
BlockState state = this.getState();
BlockState state = this.getBlock();
if (state.getValue(BlockStateProperties.LIT) != active) {
BlockState newState = state.setValue(BlockStateProperties.LIT, active);
this.setState(newState);
this.setBlock(newState);
return true;
}
return false;
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/com/teammoeg/chorda/block/CGuiBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,17 @@ public InteractionResult use(BlockState state, Level world, BlockPos pos, Player
if (b)
return InteractionResult.SUCCESS;
}
if (tile instanceof IInteractionObjectIE interaction && hand == InteractionHand.MAIN_HAND && !player.isShiftKeyDown()) {
BlockEntity master = interaction.getGuiMaster();
if (master instanceof MenuProvider menu)
NetworkHooks.openScreen((ServerPlayer)player, menu,master.getBlockPos());
return InteractionResult.SUCCESS;
if(hand == InteractionHand.MAIN_HAND && !player.isShiftKeyDown()) {
if (tile instanceof IInteractionObjectIE interaction) {
BlockEntity master = interaction.getGuiMaster();
if (master instanceof MenuProvider menu)
NetworkHooks.openScreen((ServerPlayer)player, menu,master.getBlockPos());
return InteractionResult.SUCCESS;
}
if (tile instanceof MenuProvider menu) {
NetworkHooks.openScreen((ServerPlayer)player, menu,tile.getBlockPos());
return InteractionResult.SUCCESS;
}
}
return superResult;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.teammoeg.chorda.block.entity;

import net.minecraft.world.level.block.state.BlockState;

public interface BlockStateAccess {
public BlockState getBlock();
public void setBlock(BlockState state);
}
130 changes: 123 additions & 7 deletions src/main/java/com/teammoeg/chorda/block/entity/CBlockEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,139 @@

package com.teammoeg.chorda.block.entity;

import blusunrize.immersiveengineering.common.blocks.IEBaseBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.core.BlockPos;

public abstract class CBlockEntity extends IEBaseBlockEntity implements SyncableBlockEntity {

import net.minecraft.world.level.block.state.BlockState;
/**
* CBlockEntity
* blockentity with our basic code, for convenience, some code are inspired by Immersive Engineering
* */
public abstract class CBlockEntity extends BlockEntity implements SyncableBlockEntity,BlockStateAccess {
protected boolean isUnloaded;
public CBlockEntity(BlockEntityType<? extends BlockEntity> type, BlockPos pos, BlockState state) {
super(type, pos, state);
}
@Override
public void load(CompoundTag nbtIn)
{
super.load(nbtIn);
this.readCustomNBT(nbtIn, false);
}

public abstract void readCustomNBT(CompoundTag nbt, boolean descPacket);

@Override
protected void saveAdditional(CompoundTag nbt)
{
super.saveAdditional(nbt);
this.writeCustomNBT(nbt, false);
}

public abstract void writeCustomNBT(CompoundTag nbt, boolean descPacket);

@Override
public ClientboundBlockEntityDataPacket getUpdatePacket()
{
return ClientboundBlockEntityDataPacket.create(this, be -> {
CompoundTag nbttagcompound = new CompoundTag();
this.writeCustomNBT(nbttagcompound, true);
return nbttagcompound;
});
}

@Override
public void onDataPacket(Connection net, ClientboundBlockEntityDataPacket pkt)
{
CompoundTag nonNullTag = pkt.getTag()!=null?pkt.getTag(): new CompoundTag();
this.readCustomNBT(nonNullTag, true);
}

@Override
public void handleUpdateTag(CompoundTag tag)
{
this.readCustomNBT(tag, true);
}

@Override
public CompoundTag getUpdateTag()
{
CompoundTag nbt = super.getUpdateTag();
writeCustomNBT(nbt, true);
return nbt;
}
@Override
public boolean triggerEvent(int id, int type)
{
if(id==0||id==255)
{
syncData();
return true;
}
else if(id==254)
{
BlockState state = level.getBlockState(worldPosition);
level.sendBlockUpdated(worldPosition, state, state, 3);
return true;
}
return super.triggerEvent(id, type);
}
public void syncData() {
this.setChanged();
// level.markAndNotifyBlock(worldPosition, level.getChunkAt(pos), getBlockState(), getBlockState(), 4, 128);
level.sendBlockUpdated(this.getBlockPos(), this.getBlockState(), this.getBlockState(), 3);
level.updateNeighborsAt(this.getBlockPos(), this.getBlockState().getBlock());

}
@Override
public final void setRemoved()
{
if(!isUnloaded)
onRemoved();
super.setRemoved();
}
public void onRemoved(){

}
@Override
public void onLoad()
{
super.onLoad();
isUnloaded = false;
}
@Override
public void onChunkUnloaded()
{
super.onChunkUnloaded();
isUnloaded = true;
onUnloaded();
}
public void onUnloaded(){
}
protected void setChunkUnsaved()
{
if(this.level.hasChunkAt(this.worldPosition))
this.level.getChunkAt(this.worldPosition).setUnsaved(true);
}
@Override
public void setChanged()
{
setChunkUnsaved();
BlockState state = getBlockState();
if(state.hasAnalogOutputSignal())
this.level.updateNeighbourForOutputSignal(this.worldPosition, state.getBlock());
}
@Override
public BlockState getBlock() {
return this.getBlockState();
}
@Override
public void setBlock(BlockState state) {
this.level.setBlock(this.worldPosition, state, 4);
this.setBlockState(state);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
*/

package com.teammoeg.chorda.capability;
package com.teammoeg.chorda.capability.capabilities;

import com.teammoeg.chorda.block.entity.SyncableBlockEntity;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
*/

package com.teammoeg.chorda.capability;
package com.teammoeg.chorda.capability.capabilities;

import org.jetbrains.annotations.NotNull;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
*/

package com.teammoeg.chorda.capability;
package com.teammoeg.chorda.capability.capabilities;

import net.minecraft.core.Direction;
import net.minecraftforge.common.capabilities.Capability;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.teammoeg.chorda.capability.capabilities;

import java.util.function.Supplier;

import org.jetbrains.annotations.NotNull;

import lombok.Getter;
import lombok.Setter;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.items.IItemHandlerModifiable;

public class ItemHandlerWrapper implements IItemHandlerModifiable {
private final Supplier<IItemHandlerModifiable> intern;
public void setStackInSlot(int slot, @NotNull ItemStack stack) {
intern.get().setStackInSlot(slot, stack);
}
public int getSlots() {
return intern.get().getSlots();
}
public @NotNull ItemStack getStackInSlot(int slot) {
return intern.get().getStackInSlot(slot);
}
public @NotNull ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) {
return intern.get().insertItem(slot, stack, simulate);
}
public @NotNull ItemStack extractItem(int slot, int amount, boolean simulate) {
return intern.get().extractItem(slot, amount, simulate);
}
public int getSlotLimit(int slot) {
return intern.get().getSlotLimit(slot);
}
public boolean isItemValid(int slot, @NotNull ItemStack stack) {
return intern.get().isItemValid(slot, stack);
}
public ItemHandlerWrapper(Supplier<IItemHandlerModifiable> intern) {
super();
this.intern = intern;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,31 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.network.chat.Component;

public class ImageButton extends Button {
public class HoverableImageButton extends Button {
int xTexStart;
int yTexStart;
private final int textureWidth;
private final int textureHeight;
int state;
ResourceLocation TEXTURE;
public ImageButton(ResourceLocation texture,int xIn, int yIn, int widthIn, int heightIn, int xTexStartIn, int yTexStartIn,
public HoverableImageButton(ResourceLocation texture,int xIn, int yIn, int widthIn, int heightIn, int xTexStartIn, int yTexStartIn,
Button.OnPress onPressIn) {
this(texture,xIn, yIn, widthIn, heightIn, xTexStartIn, yTexStartIn, null, onPressIn);
}

public ImageButton(ResourceLocation texture,int xIn, int yIn, int widthIn, int heightIn, int xTexStartIn, int yTexStartIn,
public HoverableImageButton(ResourceLocation texture,int xIn, int yIn, int widthIn, int heightIn, int xTexStartIn, int yTexStartIn,
Tooltip tt, Button.OnPress onPressIn) {

this(texture,xIn, yIn, widthIn, heightIn, xTexStartIn, yTexStartIn, 256, 256, onPressIn, tt,
Components.empty());
}

public ImageButton(ResourceLocation texture,int x, int y, int width, int height, int xTexStart, int yTexStart, int textureWidth,
public HoverableImageButton(ResourceLocation texture,int x, int y, int width, int height, int xTexStart, int yTexStart, int textureWidth,
int textureHeight, Button.OnPress onPress, Component title) {
this(texture,x, y, width, height, xTexStart, yTexStart, textureWidth, textureHeight, onPress, null, title);
}

public ImageButton(ResourceLocation texture,int p_i244513_1_, int p_i244513_2_, int p_i244513_3_, int p_i244513_4_, int p_i244513_5_,
public HoverableImageButton(ResourceLocation texture,int p_i244513_1_, int p_i244513_2_, int p_i244513_3_, int p_i244513_4_, int p_i244513_5_,
int p_i244513_6_, int p_i244513_9_, int p_i244513_10_, Button.OnPress p_i244513_11_,
Tooltip p_i244513_12_, Component p_i244513_13_) {
super(Button.builder(p_i244513_13_, p_i244513_11_).bounds(p_i244513_2_, p_i244513_3_, p_i244513_4_, p_i244513_5_).tooltip(p_i244513_12_));
Expand All @@ -67,8 +67,8 @@ public void setPosition(int xIn, int yIn) {
this.setX(xIn);
this.setY(yIn);
}

public void renderButton(GuiGraphics matrixStack, int mouseX, int mouseY, float partialTicks) {
@Override
public void renderWidget(GuiGraphics matrixStack, int mouseX, int mouseY, float partialTicks) {
int i = 0, j = state * this.height;

if (this.isHovered()) {
Expand Down
Loading

0 comments on commit 2c816a5

Please sign in to comment.