Skip to content

Commit

Permalink
Fixed sync issue when placing advanced drawbridge and fixed NBT being…
Browse files Browse the repository at this point in the history
… overwritten on by setDefaultNBT
  • Loading branch information
fuj1n committed Jan 11, 2025
1 parent 6e2e120 commit f007852
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ public void setDefaultNBT(CompoundTag nbt, CompoundTag blockState) {
// Firestarter does not have an inventory
// super.setDefaultNBT(nbt, blockState);

if(!nbt.contains("extinguish"))
nbt.putBoolean("extinguish", true);
nbt.putBoolean("extinguish", true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
RedstoneMachineBlockEntity machine = (RedstoneMachineBlockEntity) te;
ItemStack item = new ItemStack(this, 1);

writeAdditionalItemData(state, builder.getLevel(), new BlockPos(builder.getOptionalParameter(LootContextParams.ORIGIN)), item);

if (dropState)
machine.storeTileData(item);

writeAdditionalItemData(state, builder.getLevel(), new BlockPos(builder.getOptionalParameter(LootContextParams.ORIGIN)), item);

drops.add(item);
return drops;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.level.Level;
import net.minecraft.server.level.ServerLevel;
Expand All @@ -28,7 +27,6 @@
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.items.wrapper.InvWrapper;
import slimeknights.tmechworks.TMechworks;
import slimeknights.tmechworks.common.MechworksContent;
import slimeknights.tmechworks.common.MechworksTags;
import slimeknights.tmechworks.common.blocks.DrawbridgeBlock;
Expand Down Expand Up @@ -339,7 +337,7 @@ public void onStatsUpdated() {
slots.resize(blockSlots);
slots.overrideStackLimit(stats.isAdvanced ? 1 : 64);

BlockState state = getLevel().getBlockState(getBlockPos());
BlockState state = getBlockState();
getLevel().setBlockAndUpdate(getBlockPos(), state.setValue(DrawbridgeBlock.ADVANCED, stats.isAdvanced));
}

Expand All @@ -353,6 +351,17 @@ public void load(CompoundTag tags) {
isExtended = stats.getBoolean("Extended");
isMoving = stats.getBoolean("Moving");
cooldown = stats.getFloat("Cooldown");

if(tags.contains("DrawbridgeStats")) {
CompoundTag statsTag = tags.getCompound("DrawbridgeStats");

this.stats = new DrawbridgeStats();
this.stats.extendLength = statsTag.getInt("ExtendLength");
this.stats.extendDelay = statsTag.getFloat("ExtendDelay");
this.stats.isAdvanced = statsTag.getBoolean("IsAdvanced");

onStatsUpdated();
}
}

@Nonnull
Expand All @@ -370,6 +379,23 @@ public void saveSynced(CompoundTag tags) {
tags.put("DrawbridgeState", state);
}

@Override
public CompoundTag getUpdateTag() {
CompoundTag base = super.getUpdateTag();

if(stats != null) {
CompoundTag stats = new CompoundTag();

stats.putInt("ExtendLength", this.stats.extendLength);
stats.putFloat("ExtendDelay", this.stats.extendDelay);
stats.putBoolean("IsAdvanced", this.stats.isAdvanced);

base.put("DrawbridgeStats", stats);
}

return base;
}

@Override
public void readItemData(CompoundTag tags) {
super.readItemData(tags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void updateRedstone() {
Direction facing = Direction.NORTH;

if (hasFacingDirection()) {
facing = getLevel().getBlockState(getBlockPos()).getValue(RedstoneMachineBlock.FACING);
facing = getBlockState().getValue(RedstoneMachineBlock.FACING);
}

int oldPow = redstoneState;
Expand Down Expand Up @@ -209,8 +209,6 @@ public CompoundTag writeItemData(CompoundTag tags) {
* Reads inventory information
*/
public void readItemData(CompoundTag tags) {
super.load(tags);

if (tags.contains("Disguise")) {
CompoundTag itemNBT = tags.getCompound("Disguise");

Expand All @@ -234,17 +232,11 @@ public void saveSynced(CompoundTag tags) {

@Override
public void load(CompoundTag tags) {
super.load(tags);
readItemData(tags);

redstoneState = tags.getInt("Redstone");
}

@Override
public void writeInventoryToNBT(CompoundTag tag) {
if (!isEmpty())
super.writeInventoryToNBT(tag);
}

@Override
protected boolean shouldSyncOnUpdate() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import slimeknights.tmechworks.common.blocks.IBlockItemConstruct;
import slimeknights.tmechworks.common.blocks.RedstoneMachineBlock;
import slimeknights.tmechworks.library.TranslationUtil;
Expand All @@ -29,28 +28,33 @@ public MechworksBlockItem(Block blockIn, Properties builder) {
}
}

@Nullable
@Override
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundTag nbt) {
public ItemStack getDefaultInstance() {
Block block = getBlock();

if(!(block instanceof RedstoneMachineBlock))
return super.initCapabilities(stack, nbt);
if (!(block instanceof RedstoneMachineBlock)) {
return super.getDefaultInstance();
}

nbt = stack.getTag();
ItemStack stack = super.getDefaultInstance();

if(nbt == null)
CompoundTag nbt = stack.getTag();
if (nbt == null) {
nbt = new CompoundTag();
}

CompoundTag blockState = new CompoundTag();

CompoundTag tags = new CompoundTag();
((RedstoneMachineBlock)block).setDefaultNBT(nbt, tags);
((RedstoneMachineBlock) block).setDefaultNBT(nbt, blockState);

if(!tags.isEmpty() && !nbt.contains("BlockEntityTag"))
nbt.put("BlockEntityTag", tags);
if(!nbt.isEmpty())
if (!blockState.isEmpty() && !nbt.contains("BlockEntityTag")) {
nbt.put("BlockEntityTag", blockState);
}
if (!nbt.isEmpty()) {
stack.setTag(nbt);
}

return null;
return stack;
}

public MechworksBlockItem setTooltipFormat(Object... format){
Expand Down

0 comments on commit f007852

Please sign in to comment.