Skip to content

Commit

Permalink
refactor: ♻️ A bit of change to what kind of entities can run upgrade…
Browse files Browse the repository at this point in the history
…s + a bit more refactoring in prep for new features
  • Loading branch information
P3pp3rF1y committed Dec 23, 2024
1 parent 16339cd commit 1000bde
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 77 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ loader_version_range=[4,)
mod_id=sophisticatedstorage
mod_name=Sophisticated Storage
mod_license=GNU General Public License v3.0
mod_version=1.0.2
mod_version=1.0.3
mod_group_id=sophisticatedstorage
mod_authors=P3pp3rF1y, Ridanisaurus
mod_description=Fancy and functional storage containers.
Expand All @@ -35,7 +35,7 @@ jade_cf_file_id=5109393
chipped_cf_file_id=5506938
resourcefullib_cf_file_id=5483169
athena_cf_file_id=5431579
sc_version=[1.21.1-1.0.0,1.22)
sc_version=[1.21.1-1.0.4,1.22)
sb_version=[1.21,1.22)

#publish
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ protected BarrelBlockEntity(BlockPos pos, BlockState state, BlockEntityType<? ex
});
}

public void setDynamicRenderTracker(IDynamicRenderTracker dynamicRenderTracker) {
this.dynamicRenderTracker = dynamicRenderTracker;
}

public BarrelBlockEntity(BlockPos pos, BlockState state) {
this(pos, state, ModBlocks.BARREL_BLOCK_ENTITY_TYPE.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ private static boolean isCatSittingOnChest(LevelAccessor level, BlockPos pos) {
return false;
}

@SuppressWarnings("deprecation")
@Override
public RenderShape getRenderShape(BlockState state) {
return RenderShape.ENTITYBLOCK_ANIMATED;
Expand Down Expand Up @@ -360,12 +359,12 @@ public boolean onDestroyedByPlayer(BlockState state, Level level, BlockPos pos,
BlockPos otherPartPos = pos.relative(getConnectedDirection(state));
level.getBlockEntity(otherPartPos, ModBlocks.CHEST_BLOCK_ENTITY_TYPE.get())
.ifPresent(mainBe -> {
be.getStorageWrapper().load(level.registryAccess(), mainBe.getStorageWrapper().save(new CompoundTag()));
be.getStorageWrapper().load(mainBe.getStorageWrapper().save(new CompoundTag()));

//remove main chest contents
CompoundTag contentsTag = new CompoundTag();
contentsTag.put(StorageWrapper.CONTENTS_TAG, new CompoundTag());
mainBe.getStorageWrapper().load(level.registryAccess(), contentsTag);
mainBe.getStorageWrapper().load(contentsTag);
});
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.p3pp3rf1y.sophisticatedcore.api.IStorageWrapper;
import net.p3pp3rf1y.sophisticatedcore.renderdata.RenderInfo;
import net.p3pp3rf1y.sophisticatedcore.settings.SettingsHandler;
import net.p3pp3rf1y.sophisticatedcore.settings.itemdisplay.ItemDisplaySettingsCategory;
Expand Down Expand Up @@ -256,13 +257,14 @@ public boolean isLookingAtFront(Player player, BlockPos pos, BlockState state) {
public void setPlacedBy(Level level, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) {
super.setPlacedBy(level, pos, state, placer, stack);

WorldHelper.getBlockEntity(level, pos, LimitedBarrelBlockEntity.class).ifPresent(be -> {
StorageWrapper storageWrapper = be.getStorageWrapper();
SettingsHandler settingsHandler = storageWrapper.getSettingsHandler();
settingsHandler.getTypeCategory(ItemDisplaySettingsCategory.class).selectSlots(0, storageWrapper.getNumberOfInventorySlots());
settingsHandler.getTypeCategory(NoSortSettingsCategory.class).selectSlots(0, storageWrapper.getNumberOfInventorySlots());
settingsHandler.getTypeCategory(MemorySettingsCategory.class).setIgnoreNbt(false);
});
WorldHelper.getBlockEntity(level, pos, LimitedBarrelBlockEntity.class).ifPresent(be -> setupDefaultSettings(be.getStorageWrapper(), be.getStorageWrapper().getNumberOfInventorySlots()));
}

public static void setupDefaultSettings(IStorageWrapper storageWrapper, int numberOfInventorySlots) {
SettingsHandler settingsHandler = storageWrapper.getSettingsHandler();
settingsHandler.getTypeCategory(ItemDisplaySettingsCategory.class).selectSlots(0, numberOfInventorySlots);
settingsHandler.getTypeCategory(NoSortSettingsCategory.class).selectSlots(0, numberOfInventorySlots);
settingsHandler.getTypeCategory(MemorySettingsCategory.class).setIgnoreNbt(false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public class LimitedBarrelBlockEntity extends BarrelBlockEntity implements ICountDisplay, IFillLevelDisplay {
private static final String SLOT_COUNTS_TAG = "slotCounts";
private static final String SLOT_FILL_LEVELS_TAG = "slotFillLevels";
private static final Consumer<VoidUpgradeWrapper> VOID_UPGRADE_VOIDING_OVERFLOW_OF_EVERYTHING_BY_DEFAULT = voidUpgrade -> {
public static final Consumer<VoidUpgradeWrapper> VOID_UPGRADE_VOIDING_OVERFLOW_OF_EVERYTHING_BY_DEFAULT = voidUpgrade -> {
voidUpgrade.getFilterLogic().setAllowByDefault(false);
voidUpgrade.setShouldVoidOverflowDefaultOrLoadFromNbt(true);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
}

private void loadStorageWrapper(CompoundTag tag, HolderLookup.Provider registries) {
NBTHelper.getCompound(tag, STORAGE_WRAPPER_TAG).ifPresent(wrapperTag -> storageWrapper.load(registries, wrapperTag));
NBTHelper.getCompound(tag, STORAGE_WRAPPER_TAG).ifPresent(storageWrapper::load);
}

@Override
Expand Down Expand Up @@ -308,7 +308,7 @@ public ClientboundBlockEntityDataPacket getUpdatePacket() {
@Override
public void onDataPacket(Connection net, ClientboundBlockEntityDataPacket pkt, HolderLookup.Provider registries) {
CompoundTag tag = pkt.getTag();
if (tag == null) {
if (tag.isEmpty()) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.p3pp3rf1y.sophisticatedstorage.block;

import net.minecraft.core.HolderLookup;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
Expand Down Expand Up @@ -53,20 +52,21 @@ public abstract class StorageWrapper implements IStorageWrapper {
private CompoundTag settingsNbt = new CompoundTag();
private final SettingsHandler settingsHandler;
private final RenderInfo renderInfo;

private CompoundTag renderInfoNbt = new CompoundTag();

@Nullable
protected UUID contentsUuid = null;

private int openTabId = -1;
protected int numberOfInventorySlots = 0;

protected int numberOfInventorySlots = 0;
protected int numberOfUpgradeSlots = -1;

private SortBy sortBy = SortBy.NAME;
private int columnsTaken = 0;
private int mainColor = -1;
private int accentColor = -1;

private Runnable upgradeCachesInvalidatedHandler = () -> {
};

Expand Down Expand Up @@ -191,9 +191,9 @@ CompoundTag saveData(CompoundTag tag) {
return tag;
}

public void load(HolderLookup.Provider registries, CompoundTag tag) {
public void load(CompoundTag tag) {
loadContents(tag);
loadData(registries, tag);
loadData(tag);

initInventoryHandler();
getUpgradeHandler().refreshUpgradeWrappers();
Expand All @@ -202,11 +202,11 @@ public void load(HolderLookup.Provider registries, CompoundTag tag) {
}
}

private void loadData(HolderLookup.Provider registries, CompoundTag tag) {
private void loadData(CompoundTag tag) {
settingsNbt = tag.getCompound("settings");
settingsHandler.reloadFrom(settingsNbt);
renderInfoNbt = tag.getCompound("renderInfo");
renderInfo.deserializeFrom(registries, renderInfoNbt);
renderInfo.deserializeFrom(renderInfoNbt);
contentsUuid = NBTHelper.getTagValue(tag, UUID_TAG, CompoundTag::get).map(NbtUtils::loadUUID).orElse(null);
openTabId = NBTHelper.getInt(tag, OPEN_TAB_ID_TAG).orElse(-1);
sortBy = NBTHelper.getString(tag, "sortBy").map(SortBy::fromName).orElse(SortBy.NAME);
Expand All @@ -229,7 +229,7 @@ private void loadContents(CompoundTag tag) {
}

@Override
public void setSaveHandler(Runnable saveHandler) {
public void setContentsChangeHandler(Runnable contentsChangeHandler) {
//noop
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.p3pp3rf1y.sophisticatedstorage.client.gui;

import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
Expand All @@ -11,9 +12,12 @@
import net.p3pp3rf1y.sophisticatedcore.client.gui.utils.GuiHelper;
import net.p3pp3rf1y.sophisticatedcore.client.gui.utils.TextureBlitData;
import net.p3pp3rf1y.sophisticatedcore.client.gui.utils.UV;
import net.p3pp3rf1y.sophisticatedcore.common.gui.StorageContainerMenuBase;
import net.p3pp3rf1y.sophisticatedstorage.SophisticatedStorage;
import net.p3pp3rf1y.sophisticatedstorage.common.gui.StorageContainerMenu;

import java.util.function.IntFunction;

public class LimitedBarrelScreen extends StorageScreen {
public static final ResourceLocation GUI_BACKGROUNDS = SophisticatedStorage.getRL("textures/gui/limited_barrels.png");
public static final TextureBlitData LIMITED_I_BACKGROUND = new TextureBlitData(GUI_BACKGROUNDS, Dimension.SQUARE_256, new UV(0, 0), new Dimension(84, 82));
Expand Down Expand Up @@ -42,22 +46,27 @@ public static void drawSlotBg(AbstractContainerScreen<?> screen, GuiGraphics gui
@Override
protected void renderLabels(GuiGraphics guiGraphics, int mouseX, int mouseY) {
super.renderLabels(guiGraphics, mouseX, mouseY);
switch (getMenu().getNumberOfStorageInventorySlots()) {
case 1 -> renderBar(guiGraphics, imageWidth / 2 + 37, 18 + 6, getMenu().getSlotFillPercentage(0), LARGE_BAR_FILL, false);
renderBars(font, imageWidth, getMenu(), guiGraphics, getMenu()::getSlotFillPercentage);
}

public static void renderBars(Font font, int imageWidth, StorageContainerMenuBase<?> menu, GuiGraphics guiGraphics, IntFunction<Float> getSlotFillPercentage) {
switch (menu.getNumberOfStorageInventorySlots()) {
case 1 ->
renderBar(font, guiGraphics, imageWidth / 2 + 37, 18 + 6, getSlotFillPercentage.apply(0), LARGE_BAR_FILL, false);
case 2 -> {
renderBar(guiGraphics, imageWidth / 2 + 37, 18 + 6, getMenu().getSlotFillPercentage(0), SMALL_BAR_FILL, false);
renderBar(guiGraphics, imageWidth / 2 + 37, 18 + 6 + 40, getMenu().getSlotFillPercentage(1), SMALL_BAR_FILL, false);
renderBar(font, guiGraphics, imageWidth / 2 + 37, 18 + 6, getSlotFillPercentage.apply(0), SMALL_BAR_FILL, false);
renderBar(font, guiGraphics, imageWidth / 2 + 37, 18 + 6 + 40, getSlotFillPercentage.apply(1), SMALL_BAR_FILL, false);
}
case 3 -> {
renderBar(guiGraphics, imageWidth / 2 + 37, 18 + 6, getMenu().getSlotFillPercentage(0), SMALL_BAR_FILL, false);
renderBar(guiGraphics, imageWidth / 2 - 37 - 5, 18 + 6 + 40, getMenu().getSlotFillPercentage(1), SMALL_BAR_FILL, true);
renderBar(guiGraphics, imageWidth / 2 + 37, 18 + 6 + 40, getMenu().getSlotFillPercentage(2), SMALL_BAR_FILL, false);
renderBar(font, guiGraphics, imageWidth / 2 + 37, 18 + 6, getSlotFillPercentage.apply(0), SMALL_BAR_FILL, false);
renderBar(font, guiGraphics, imageWidth / 2 - 37 - 5, 18 + 6 + 40, getSlotFillPercentage.apply(1), SMALL_BAR_FILL, true);
renderBar(font, guiGraphics, imageWidth / 2 + 37, 18 + 6 + 40, getSlotFillPercentage.apply(2), SMALL_BAR_FILL, false);
}
case 4 -> {
renderBar(guiGraphics, imageWidth / 2 - 37 - 5, 18 + 6, getMenu().getSlotFillPercentage(0), SMALL_BAR_FILL, true);
renderBar(guiGraphics, imageWidth / 2 + 37, 18 + 6, getMenu().getSlotFillPercentage(1), SMALL_BAR_FILL, false);
renderBar(guiGraphics, imageWidth / 2 - 37 - 5, 18 + 6 + 40, getMenu().getSlotFillPercentage(2), SMALL_BAR_FILL, true);
renderBar(guiGraphics, imageWidth / 2 + 37, 18 + 6 + 40, getMenu().getSlotFillPercentage(3), SMALL_BAR_FILL, false);
renderBar(font, guiGraphics, imageWidth / 2 - 37 - 5, 18 + 6, getSlotFillPercentage.apply(0), SMALL_BAR_FILL, true);
renderBar(font, guiGraphics, imageWidth / 2 + 37, 18 + 6, getSlotFillPercentage.apply(1), SMALL_BAR_FILL, false);
renderBar(font, guiGraphics, imageWidth / 2 - 37 - 5, 18 + 6 + 40, getSlotFillPercentage.apply(2), SMALL_BAR_FILL, true);
renderBar(font, guiGraphics, imageWidth / 2 + 37, 18 + 6 + 40, getSlotFillPercentage.apply(3), SMALL_BAR_FILL, false);
}
}
}
Expand Down Expand Up @@ -127,7 +136,7 @@ public static void updateSlotPositions(AbstractContainerMenu menu, int slotNumbe
}
}

private void renderBar(GuiGraphics guiGraphics, int x, int y, float percentage, TextureBlitData barTexture, boolean left) {
private static void renderBar(Font font, GuiGraphics guiGraphics, int x, int y, float percentage, TextureBlitData barTexture, boolean left) {
int barHeight = (int) (barTexture.getHeight() * percentage);
int yOffset = barTexture.getHeight() - barHeight;

Expand All @@ -136,7 +145,7 @@ private void renderBar(GuiGraphics guiGraphics, int x, int y, float percentage,
String text = (int) (percentage * 100) + "%";
int percentageX = x;
if (left) {
percentageX -= 2 + minecraft.font.width(text);
percentageX -= 2 + font.width(text);
} else {
percentageX += 6;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import net.minecraft.world.inventory.InventoryMenu;
import net.minecraft.world.item.*;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.WoodType;
import net.neoforged.neoforge.client.ChunkRenderTypeSet;
Expand Down Expand Up @@ -337,8 +335,7 @@ private BakedModel getDynamicModel(@Nullable String woodName, Map<DynamicBarrelB
}

private BlockState getDefaultBlockState(ResourceLocation blockName) {
Block block = BuiltInRegistries.BLOCK.get(blockName);
return block != null ? block.defaultBlockState() : Blocks.AIR.defaultBlockState();
return BuiltInRegistries.BLOCK.get(blockName).defaultBlockState();
}

private Map<BarrelMaterial, ResourceLocation> getMaterials(ModelData extraData) {
Expand Down Expand Up @@ -716,31 +713,32 @@ public TextureAtlasSprite getParticleIcon(ModelData data) {
@Override
public ModelData getModelData(BlockAndTintGetter world, BlockPos pos, BlockState state, ModelData tileData) {
return WorldHelper.getBlockEntity(world, pos, BarrelBlockEntity.class)
.map(be -> {

ModelData.Builder builder = ModelData.builder();
boolean hasMainColor = be.getStorageWrapper().hasMainColor();
builder.with(HAS_MAIN_COLOR, hasMainColor);
boolean hasAccentColor = be.getStorageWrapper().hasAccentColor();
builder.with(HAS_ACCENT_COLOR, hasAccentColor);
if (!be.hasFullyDynamicRenderer()) {
builder.with(DISPLAY_ITEMS, be.getStorageWrapper().getRenderInfo().getItemDisplayRenderInfo().getDisplayItems());
builder.with(INACCESSIBLE_SLOTS, be.getStorageWrapper().getRenderInfo().getItemDisplayRenderInfo().getInaccessibleSlots());
}
builder.with(IS_PACKED, be.isPacked());
builder.with(SHOWS_LOCK, be.isLocked() && be.shouldShowLock());
builder.with(SHOWS_TIER, be.shouldShowTier());
Optional<WoodType> woodType = be.getWoodType();
if (woodType.isPresent() || !(hasMainColor && hasAccentColor)) {
builder.with(WOOD_NAME, woodType.orElse(WoodType.ACACIA).name());
}
.map(BarrelBakedModelBase::getModelDataFromBlockEntity).orElse(ModelData.EMPTY);
}

Map<BarrelMaterial, ResourceLocation> materials = be.getMaterials();
if (!materials.isEmpty()) {
builder.with(MATERIALS, materials);
}
return builder.build();
}).orElse(ModelData.EMPTY);
public static ModelData getModelDataFromBlockEntity(BarrelBlockEntity be) {
ModelData.Builder builder = ModelData.builder();
boolean hasMainColor = be.getStorageWrapper().hasMainColor();
builder.with(HAS_MAIN_COLOR, hasMainColor);
boolean hasAccentColor = be.getStorageWrapper().hasAccentColor();
builder.with(HAS_ACCENT_COLOR, hasAccentColor);
if (!be.hasFullyDynamicRenderer()) {
builder.with(DISPLAY_ITEMS, be.getStorageWrapper().getRenderInfo().getItemDisplayRenderInfo().getDisplayItems());
builder.with(INACCESSIBLE_SLOTS, be.getStorageWrapper().getRenderInfo().getItemDisplayRenderInfo().getInaccessibleSlots());
}
builder.with(IS_PACKED, be.isPacked());
builder.with(SHOWS_LOCK, be.isLocked() && be.shouldShowLock());
builder.with(SHOWS_TIER, be.shouldShowTier());
Optional<WoodType> woodType = be.getWoodType();
if (woodType.isPresent() || !(hasMainColor && hasAccentColor)) {
builder.with(WOOD_NAME, woodType.orElse(WoodType.ACACIA).name());
}

Map<BarrelMaterial, ResourceLocation> materials = be.getMaterials();
if (!materials.isEmpty()) {
builder.with(MATERIALS, materials);
}
return builder.build();
}

@Override
Expand All @@ -763,7 +761,7 @@ private static class BarrelItemOverrides extends ItemOverrides {
private final BarrelBakedModelBase barrelBakedModel;
@Nullable
private final BakedModel flatTopModel;
private Cache<Integer, BakedModel> resolvedModels = CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.MINUTES).build();
private final Cache<Integer, BakedModel> resolvedModels = CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.MINUTES).build();

public BarrelItemOverrides(BarrelBakedModelBase barrelBakedModel, @Nullable BakedModel flatTopModel) {
this.barrelBakedModel = barrelBakedModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public void render(T blockEntity, float partialTick, PoseStack poseStack, MultiB
return;
}

packedLight = LevelRenderer.getLightColor(blockEntity.getLevel(), blockEntity.getBlockPos().relative(storageBlock.getFacing(blockState)));
if (blockEntity.getLevel() != null) {
packedLight = LevelRenderer.getLightColor(blockEntity.getLevel(), blockEntity.getBlockPos().relative(storageBlock.getFacing(blockState)));
}

renderFrontFace(blockEntity, poseStack, bufferSource, packedLight, packedOverlay, flatTop, blockState);
renderHiddenTier(blockEntity, poseStack, bufferSource, packedLight, packedOverlay);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void render(ChestBlockEntity chestEntity, float partialTick, PoseStack po
poseStack.scale(1.01f, 1.01f, 1.01f);
subRenderer.renderBottomAndLid(poseStack, bufferSource, lidAngle, packedLight, packedOverlay, StorageTextureManager.ChestMaterial.PACKED);
poseStack.popPose();
} else if (shouldRenderFrontFace(chestEntity.getBlockPos())) {
} else if (chestEntity.getBlockPos() == BlockPos.ZERO || shouldRenderFrontFace(chestEntity.getBlockPos())) { //special condition for chests that are not in the world
poseStack.pushPose();
poseStack.translate(0.5, 0.5, 0.5);
poseStack.mulPose(Axis.YP.rotationDegrees(180));
Expand Down
Loading

0 comments on commit 1000bde

Please sign in to comment.