Skip to content

Commit 2c816a5

Browse files
committed
wardrobe ui and independ CBlockEntity
1 parent 022ab2f commit 2c816a5

26 files changed

+565
-145
lines changed

src/main/java/com/teammoeg/chorda/block/CBlockInterfaces.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@
1919

2020
package com.teammoeg.chorda.block;
2121

22+
import com.teammoeg.chorda.block.entity.BlockStateAccess;
23+
2224
import blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces;
2325
import net.minecraft.world.level.block.state.BlockState;
2426
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
2527

2628
public class CBlockInterfaces {
27-
public interface IActiveState extends IEBlockInterfaces.BlockstateProvider {
29+
public interface IActiveState extends BlockStateAccess {
2830
default boolean getIsActive() {
29-
BlockState state = this.getState();
31+
BlockState state = this.getBlock();
3032
return state.hasProperty(BlockStateProperties.LIT) ? state.getValue(BlockStateProperties.LIT) : false;
3133
}
3234

@@ -36,10 +38,10 @@ default boolean getIsActive() {
3638
* @return true if the state was changed, false otherwise
3739
*/
3840
default boolean setActive(boolean active) {
39-
BlockState state = this.getState();
41+
BlockState state = this.getBlock();
4042
if (state.getValue(BlockStateProperties.LIT) != active) {
4143
BlockState newState = state.setValue(BlockStateProperties.LIT, active);
42-
this.setState(newState);
44+
this.setBlock(newState);
4345
return true;
4446
}
4547
return false;

src/main/java/com/teammoeg/chorda/block/CGuiBlock.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,17 @@ public InteractionResult use(BlockState state, Level world, BlockPos pos, Player
9292
if (b)
9393
return InteractionResult.SUCCESS;
9494
}
95-
if (tile instanceof IInteractionObjectIE interaction && hand == InteractionHand.MAIN_HAND && !player.isShiftKeyDown()) {
96-
BlockEntity master = interaction.getGuiMaster();
97-
if (master instanceof MenuProvider menu)
98-
NetworkHooks.openScreen((ServerPlayer)player, menu,master.getBlockPos());
99-
return InteractionResult.SUCCESS;
95+
if(hand == InteractionHand.MAIN_HAND && !player.isShiftKeyDown()) {
96+
if (tile instanceof IInteractionObjectIE interaction) {
97+
BlockEntity master = interaction.getGuiMaster();
98+
if (master instanceof MenuProvider menu)
99+
NetworkHooks.openScreen((ServerPlayer)player, menu,master.getBlockPos());
100+
return InteractionResult.SUCCESS;
101+
}
102+
if (tile instanceof MenuProvider menu) {
103+
NetworkHooks.openScreen((ServerPlayer)player, menu,tile.getBlockPos());
104+
return InteractionResult.SUCCESS;
105+
}
100106
}
101107
return superResult;
102108
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.teammoeg.chorda.block.entity;
2+
3+
import net.minecraft.world.level.block.state.BlockState;
4+
5+
public interface BlockStateAccess {
6+
public BlockState getBlock();
7+
public void setBlock(BlockState state);
8+
}

src/main/java/com/teammoeg/chorda/block/entity/CBlockEntity.java

Lines changed: 123 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,139 @@
1919

2020
package com.teammoeg.chorda.block.entity;
2121

22-
import blusunrize.immersiveengineering.common.blocks.IEBaseBlockEntity;
23-
import net.minecraft.world.level.block.state.BlockState;
22+
import net.minecraft.core.BlockPos;
23+
import net.minecraft.nbt.CompoundTag;
24+
import net.minecraft.network.Connection;
25+
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
2426
import net.minecraft.world.level.block.entity.BlockEntity;
2527
import net.minecraft.world.level.block.entity.BlockEntityType;
26-
import net.minecraft.core.BlockPos;
27-
28-
public abstract class CBlockEntity extends IEBaseBlockEntity implements SyncableBlockEntity {
29-
28+
import net.minecraft.world.level.block.state.BlockState;
29+
/**
30+
* CBlockEntity
31+
* blockentity with our basic code, for convenience, some code are inspired by Immersive Engineering
32+
* */
33+
public abstract class CBlockEntity extends BlockEntity implements SyncableBlockEntity,BlockStateAccess {
34+
protected boolean isUnloaded;
3035
public CBlockEntity(BlockEntityType<? extends BlockEntity> type, BlockPos pos, BlockState state) {
3136
super(type, pos, state);
3237
}
38+
@Override
39+
public void load(CompoundTag nbtIn)
40+
{
41+
super.load(nbtIn);
42+
this.readCustomNBT(nbtIn, false);
43+
}
44+
45+
public abstract void readCustomNBT(CompoundTag nbt, boolean descPacket);
46+
47+
@Override
48+
protected void saveAdditional(CompoundTag nbt)
49+
{
50+
super.saveAdditional(nbt);
51+
this.writeCustomNBT(nbt, false);
52+
}
53+
54+
public abstract void writeCustomNBT(CompoundTag nbt, boolean descPacket);
55+
56+
@Override
57+
public ClientboundBlockEntityDataPacket getUpdatePacket()
58+
{
59+
return ClientboundBlockEntityDataPacket.create(this, be -> {
60+
CompoundTag nbttagcompound = new CompoundTag();
61+
this.writeCustomNBT(nbttagcompound, true);
62+
return nbttagcompound;
63+
});
64+
}
65+
66+
@Override
67+
public void onDataPacket(Connection net, ClientboundBlockEntityDataPacket pkt)
68+
{
69+
CompoundTag nonNullTag = pkt.getTag()!=null?pkt.getTag(): new CompoundTag();
70+
this.readCustomNBT(nonNullTag, true);
71+
}
72+
73+
@Override
74+
public void handleUpdateTag(CompoundTag tag)
75+
{
76+
this.readCustomNBT(tag, true);
77+
}
3378

79+
@Override
80+
public CompoundTag getUpdateTag()
81+
{
82+
CompoundTag nbt = super.getUpdateTag();
83+
writeCustomNBT(nbt, true);
84+
return nbt;
85+
}
86+
@Override
87+
public boolean triggerEvent(int id, int type)
88+
{
89+
if(id==0||id==255)
90+
{
91+
syncData();
92+
return true;
93+
}
94+
else if(id==254)
95+
{
96+
BlockState state = level.getBlockState(worldPosition);
97+
level.sendBlockUpdated(worldPosition, state, state, 3);
98+
return true;
99+
}
100+
return super.triggerEvent(id, type);
101+
}
34102
public void syncData() {
35103
this.setChanged();
36-
// level.markAndNotifyBlock(worldPosition, level.getChunkAt(pos), getBlockState(), getBlockState(), 4, 128);
37104
level.sendBlockUpdated(this.getBlockPos(), this.getBlockState(), this.getBlockState(), 3);
105+
level.updateNeighborsAt(this.getBlockPos(), this.getBlockState().getBlock());
38106

39107
}
108+
@Override
109+
public final void setRemoved()
110+
{
111+
if(!isUnloaded)
112+
onRemoved();
113+
super.setRemoved();
114+
}
115+
public void onRemoved(){
116+
117+
}
118+
@Override
119+
public void onLoad()
120+
{
121+
super.onLoad();
122+
isUnloaded = false;
123+
}
124+
@Override
125+
public void onChunkUnloaded()
126+
{
127+
super.onChunkUnloaded();
128+
isUnloaded = true;
129+
onUnloaded();
130+
}
131+
public void onUnloaded(){
132+
}
133+
protected void setChunkUnsaved()
134+
{
135+
if(this.level.hasChunkAt(this.worldPosition))
136+
this.level.getChunkAt(this.worldPosition).setUnsaved(true);
137+
}
138+
@Override
139+
public void setChanged()
140+
{
141+
setChunkUnsaved();
142+
BlockState state = getBlockState();
143+
if(state.hasAnalogOutputSignal())
144+
this.level.updateNeighbourForOutputSignal(this.worldPosition, state.getBlock());
145+
}
146+
@Override
147+
public BlockState getBlock() {
148+
return this.getBlockState();
149+
}
150+
@Override
151+
public void setBlock(BlockState state) {
152+
this.level.setBlock(this.worldPosition, state, 4);
153+
this.setBlockState(state);
154+
155+
}
40156

41157
}

src/main/java/com/teammoeg/chorda/capability/ChangeDetectedFluidHandler.java renamed to src/main/java/com/teammoeg/chorda/capability/capabilities/ChangeDetectedFluidHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
*/
1919

20-
package com.teammoeg.chorda.capability;
20+
package com.teammoeg.chorda.capability.capabilities;
2121

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

src/main/java/com/teammoeg/chorda/capability/ChangeDetectedItemHandler.java renamed to src/main/java/com/teammoeg/chorda/capability/capabilities/ChangeDetectedItemHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
*/
1919

20-
package com.teammoeg.chorda.capability;
20+
package com.teammoeg.chorda.capability.capabilities;
2121

2222
import org.jetbrains.annotations.NotNull;
2323

src/main/java/com/teammoeg/chorda/capability/CurioCapabilityProvider.java renamed to src/main/java/com/teammoeg/chorda/capability/capabilities/CurioCapabilityProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
*/
1919

20-
package com.teammoeg.chorda.capability;
20+
package com.teammoeg.chorda.capability.capabilities;
2121

2222
import net.minecraft.core.Direction;
2323
import net.minecraftforge.common.capabilities.Capability;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.teammoeg.chorda.capability.capabilities;
2+
3+
import java.util.function.Supplier;
4+
5+
import org.jetbrains.annotations.NotNull;
6+
7+
import lombok.Getter;
8+
import lombok.Setter;
9+
import net.minecraft.world.item.ItemStack;
10+
import net.minecraftforge.items.IItemHandlerModifiable;
11+
12+
public class ItemHandlerWrapper implements IItemHandlerModifiable {
13+
private final Supplier<IItemHandlerModifiable> intern;
14+
public void setStackInSlot(int slot, @NotNull ItemStack stack) {
15+
intern.get().setStackInSlot(slot, stack);
16+
}
17+
public int getSlots() {
18+
return intern.get().getSlots();
19+
}
20+
public @NotNull ItemStack getStackInSlot(int slot) {
21+
return intern.get().getStackInSlot(slot);
22+
}
23+
public @NotNull ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) {
24+
return intern.get().insertItem(slot, stack, simulate);
25+
}
26+
public @NotNull ItemStack extractItem(int slot, int amount, boolean simulate) {
27+
return intern.get().extractItem(slot, amount, simulate);
28+
}
29+
public int getSlotLimit(int slot) {
30+
return intern.get().getSlotLimit(slot);
31+
}
32+
public boolean isItemValid(int slot, @NotNull ItemStack stack) {
33+
return intern.get().isItemValid(slot, stack);
34+
}
35+
public ItemHandlerWrapper(Supplier<IItemHandlerModifiable> intern) {
36+
super();
37+
this.intern = intern;
38+
}
39+
40+
41+
}

src/main/java/com/teammoeg/chorda/client/widget/ImageButton.java renamed to src/main/java/com/teammoeg/chorda/client/widget/HoverableImageButton.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,31 @@
2828
import net.minecraft.resources.ResourceLocation;
2929
import net.minecraft.network.chat.Component;
3030

31-
public class ImageButton extends Button {
31+
public class HoverableImageButton extends Button {
3232
int xTexStart;
3333
int yTexStart;
3434
private final int textureWidth;
3535
private final int textureHeight;
3636
int state;
3737
ResourceLocation TEXTURE;
38-
public ImageButton(ResourceLocation texture,int xIn, int yIn, int widthIn, int heightIn, int xTexStartIn, int yTexStartIn,
38+
public HoverableImageButton(ResourceLocation texture,int xIn, int yIn, int widthIn, int heightIn, int xTexStartIn, int yTexStartIn,
3939
Button.OnPress onPressIn) {
4040
this(texture,xIn, yIn, widthIn, heightIn, xTexStartIn, yTexStartIn, null, onPressIn);
4141
}
4242

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

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

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

55-
public ImageButton(ResourceLocation texture,int p_i244513_1_, int p_i244513_2_, int p_i244513_3_, int p_i244513_4_, int p_i244513_5_,
55+
public HoverableImageButton(ResourceLocation texture,int p_i244513_1_, int p_i244513_2_, int p_i244513_3_, int p_i244513_4_, int p_i244513_5_,
5656
int p_i244513_6_, int p_i244513_9_, int p_i244513_10_, Button.OnPress p_i244513_11_,
5757
Tooltip p_i244513_12_, Component p_i244513_13_) {
5858
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_));
@@ -67,8 +67,8 @@ public void setPosition(int xIn, int yIn) {
6767
this.setX(xIn);
6868
this.setY(yIn);
6969
}
70-
71-
public void renderButton(GuiGraphics matrixStack, int mouseX, int mouseY, float partialTicks) {
70+
@Override
71+
public void renderWidget(GuiGraphics matrixStack, int mouseX, int mouseY, float partialTicks) {
7272
int i = 0, j = state * this.height;
7373

7474
if (this.isHovered()) {

0 commit comments

Comments
 (0)