Skip to content

Commit

Permalink
Fix pipe block entity
Browse files Browse the repository at this point in the history
  • Loading branch information
WenXin20 committed Jul 21, 2024
1 parent 92cecb6 commit dfba37d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import com.wenxin2.warp_pipes.init.ModRegistry;
import com.wenxin2.warp_pipes.init.SoundRegistry;
import com.wenxin2.warp_pipes.inventory.WarpPipeMenu;
import java.util.Optional;
import java.util.UUID;
import java.util.function.UnaryOperator;
import javax.annotation.Nullable;
import net.minecraft.commands.CommandSource;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
Expand Down Expand Up @@ -72,7 +74,7 @@ public class WarpPipeBlockEntity extends BlockEntity implements MenuProvider, Na
public Component name;
private LockCode lockKey = LockCode.NO_LOCK;
@Nullable
public BlockPos destinationPos;
public Optional<BlockPos> destinationPos;
public String dimensionTag;
public int spoutHeight = 4;
public int bubblesDistance = 3;
Expand Down Expand Up @@ -264,18 +266,18 @@ public boolean hasDestinationPos() {
return this.destinationPos != null;
}

public void setDestinationPos(@Nullable BlockPos pos) {
public void setDestinationPos(@Nullable Optional<BlockPos> pos) {
this.destinationPos = pos;
if (this.level != null && pos != null) {
if (this.level != null && pos != null && pos.isPresent()) {
BlockState state = this.getBlockState();
this.level.setBlock(this.getBlockPos(), state, 4);
}
}

@Nullable
public BlockPos getDestinationPos() {
if (this.destinationPos != null) {
return this.destinationPos;
if (this.destinationPos != null && this.destinationPos.isPresent()) {
return this.destinationPos.get();
}
return null;
}
Expand Down Expand Up @@ -359,8 +361,8 @@ private Component loadLine(Component text) {
}

@Override
public void load(CompoundTag tag) {
super.load(tag);
public void loadAdditional(CompoundTag tag, HolderLookup.Provider provider) {
super.loadAdditional(tag, provider);
this.lockKey = LockCode.fromTag(tag);
this.spoutHeight = tag.getInt(SPOUT_HEIGHT);
this.bubblesDistance = tag.getInt(BUBBLES_DISTANCE);
Expand All @@ -373,7 +375,7 @@ public void load(CompoundTag tag) {
this.displayTextBelow = tag.getBoolean(DISPLAY_TEXT_BELOW);

if (tag.contains(CUSTOM_NAME, 8)) {
this.name = Component.Serializer.fromJson(tag.getString(CUSTOM_NAME));
this.name = Component.Serializer.fromJson(tag.getString(CUSTOM_NAME), provider);
}

if (tag.contains(PIPE_NAME)) {
Expand All @@ -383,7 +385,7 @@ public void load(CompoundTag tag) {
}

if (tag.contains(WARP_POS)) {
this.destinationPos = NbtUtils.readBlockPos(tag.getCompound(WARP_POS));
this.destinationPos = NbtUtils.readBlockPos(tag.getCompound(WARP_POS), "warp_pos");
this.setDestinationPos(this.destinationPos);
}

Expand All @@ -403,8 +405,8 @@ public void load(CompoundTag tag) {
}

@Override
public void saveAdditional(CompoundTag tag) {
super.saveAdditional(tag);
public void saveAdditional(CompoundTag tag, HolderLookup.Provider provider) {
super.saveAdditional(tag, provider);
this.lockKey.addToTag(tag);
tag.putInt(BUBBLES_DISTANCE, this.bubblesDistance);
tag.putInt(SPOUT_HEIGHT, this.spoutHeight);
Expand All @@ -418,15 +420,15 @@ public void saveAdditional(CompoundTag tag) {
tag.putBoolean(DISPLAY_TEXT_BELOW, this.displayTextBelow);

if (this.name != null) {
tag.putString(CUSTOM_NAME, Component.Serializer.toJson(this.name));
tag.putString(CUSTOM_NAME, Component.Serializer.toJson(this.name, provider));
}

PipeText.DIRECT_CODEC.encodeStart(NbtOps.INSTANCE, this.pipeName).resultOrPartial(LOGGER::error).ifPresent((pipeName) -> {
tag.put(PIPE_NAME, pipeName);
});

if (this.hasDestinationPos() && this.destinationPos != null) {
tag.put(WARP_POS, NbtUtils.writeBlockPos(this.destinationPos));
if (this.hasDestinationPos() && this.destinationPos != null && this.destinationPos.isPresent()) {
tag.put(WARP_POS, NbtUtils.writeBlockPos(this.destinationPos.get()));
}

if (this.dimensionTag != null) {
Expand All @@ -444,10 +446,10 @@ public void saveAdditional(CompoundTag tag) {

@NotNull
@Override
public CompoundTag getUpdateTag() {
CompoundTag tag = super.getUpdateTag();
public CompoundTag getUpdateTag(HolderLookup.Provider provider) {
CompoundTag tag = super.getUpdateTag(provider);

this.saveAdditional(tag);
this.saveAdditional(tag, provider);
return tag;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/wenxin2/warp_pipes/items/LinkerItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public void link(BlockPos pos, Level world, ItemStack stack, WarpPipeBlockEntity
UUID uuid = warpPipeBE.getUuid();
UUID uuidGlobal = warpPipeBEGlobal.getUuid();

warpPipeBE.setDestinationPos(warpPipeBEGlobal.getBlockPos());
warpPipeBE.setDestinationPos(Optional.of(warpPipeBEGlobal.getBlockPos()));
if (uuid != null)
warpPipeBE.setWarpUuid(getWarpUUID(stack));
warpPipeBE.setChanged();
Expand All @@ -251,7 +251,7 @@ public void link(BlockPos pos, Level world, ItemStack stack, WarpPipeBlockEntity
// System.out.println("Global Dimension: " + warpPipeBEGlobal.getLevel().dimension());
} /*else System.out.println("World is null!");*/

warpPipeBEGlobal.setDestinationPos(pos);
warpPipeBEGlobal.setDestinationPos(Optional.ofNullable(pos));
warpPipeBEGlobal.setDestinationDim(world.dimension());
if (uuidGlobal != null)
warpPipeBEGlobal.setWarpUuid(warpPipeBE.getUuid());
Expand Down

0 comments on commit dfba37d

Please sign in to comment.