Skip to content

Commit

Permalink
refactor: Refactored containers and tabs a bit more to limit code dup…
Browse files Browse the repository at this point in the history
…lication
  • Loading branch information
P3pp3rF1y committed Nov 20, 2020
1 parent fc17269 commit 2896527
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class UpgradeSettingsControl extends CompositeWidget<UpgradeSettingsTab<?
public UpgradeSettingsControl(Position position, BackpackScreen screen) {
super(position);
int i = 0;
for (UpgradeContainerBase<?> container : screen.getContainer().getUpgradeContainers()) {
UpgradeSettingsTab<UpgradeContainerBase<?>> tab = addChild(UpgradeSettingsTabManager.getTab(container, new Position(x, getTopY(i)), screen));
for (UpgradeContainerBase<?, ?> container : screen.getContainer().getUpgradeContainers()) {
UpgradeSettingsTab<UpgradeContainerBase<?, ?>> tab = addChild(UpgradeSettingsTabManager.getTab(container, new Position(x, getTopY(i)), screen));
tab.setHandlers(t -> {
if (differentTabIsOpen(t)) {
openTab.close();
Expand All @@ -44,7 +44,7 @@ public UpgradeSettingsControl(Position position, BackpackScreen screen) {
}
}

private boolean isNotCovered(UpgradeSettingsTab<?> open, UpgradeSettingsTab<UpgradeContainerBase<?>> t, boolean checkFullyCovered) {
private boolean isNotCovered(UpgradeSettingsTab<?> open, UpgradeSettingsTab<UpgradeContainerBase<?, ?>> t, boolean checkFullyCovered) {
if (checkFullyCovered) {
return open.getBottomY() < t.getBottomY() || open.getTopY() > t.getTopY();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.function.Predicate;

@OnlyIn(Dist.CLIENT)
public abstract class UpgradeSettingsTab<C extends UpgradeContainerBase<?>> extends CompositeWidget<Widget> {
public abstract class UpgradeSettingsTab<C extends UpgradeContainerBase<?, ?>> extends CompositeWidget<Widget> {
private static final int TEXTURE_WIDTH = 256;
private static final int TEXTURE_HEIGHT = 256;
public static final int DEFAULT_HEIGHT = 24;
Expand All @@ -35,27 +35,31 @@ public abstract class UpgradeSettingsTab<C extends UpgradeContainerBase<?>> exte
private boolean isOpen = false;
private final C upgradeContainer;
private final Dimension openTabDimension;
private final int slotsPerRow;
private final ITextComponent tabLabel;
private Consumer<UpgradeSettingsTab<C>> onOpen = t -> {};
private Consumer<UpgradeSettingsTab<C>> onClose = t -> {};
private Predicate<UpgradeSettingsTab<C>> shouldRender = t -> true;
private Predicate<UpgradeSettingsTab<C>> shouldShowTooltip = t -> true;
private final List<Widget> hideableChildren = new ArrayList<>();

protected UpgradeSettingsTab(C upgradeContainer, Position position, Dimension openTabDimension, BackpackScreen screen) {
protected UpgradeSettingsTab(C upgradeContainer, Position position, Dimension openTabDimension, BackpackScreen screen, int slotsPerRow, ITextComponent tabLabel, ITextComponent closedTooltip) {
super(position);
this.upgradeContainer = upgradeContainer;
this.openTabDimension = openTabDimension;
closedTooltip = ImmutableList.of(getClosedTooltip().func_241878_f());
this.slotsPerRow = slotsPerRow;
this.tabLabel = tabLabel;
this.closedTooltip = ImmutableList.of(closedTooltip.func_241878_f());
addChild(new ItemButton(new Position(x + 1, y + 4), this::onTabIconClicked, getContainer().getUpgradeStack()) {
@Override
protected void renderWidget(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
super.renderWidget(matrixStack, mouseX, mouseY, partialTicks);
if (!isOpen && shouldShowTooltip.test(UpgradeSettingsTab.this) && isMouseOver(mouseX, mouseY)) {
GuiHelper.setTooltipToRender(closedTooltip);
GuiHelper.setTooltipToRender(UpgradeSettingsTab.this.closedTooltip);
}
}
});
addHideableChild(new Label(new Position(x + 20, y + 8), getTabLabel()));
addHideableChild(new Label(new Position(x + 20, y + 8), tabLabel));
slotsLeftX = x + 4;
slotsTopY = y + 46;
this.screen = screen;
Expand Down Expand Up @@ -105,10 +109,6 @@ protected void renderBg(MatrixStack matrixStack, Minecraft minecraft, int mouseX
blit(matrixStack, x - 3, y, TEXTURE_WIDTH / 2, TEXTURE_HEIGHT - height, 3, height);
}

protected abstract ITextComponent getTabLabel();

protected abstract ITextComponent getClosedTooltip();

@Override
public int getWidth() {
return width;
Expand Down Expand Up @@ -145,14 +145,12 @@ protected void onTabOpen() {
protected void moveSlotsToTab() {
int upgradeSlotNumber = 0;
for (Slot slot : getContainer().getSlots()) {
slot.xPos = slotsLeftX - screen.getGuiLeft() + (upgradeSlotNumber % getSlotsPerRow()) * 18;
slot.yPos = slotsTopY - screen.getGuiTop() + (upgradeSlotNumber / getSlotsPerRow()) * 18;
slot.xPos = slotsLeftX - screen.getGuiLeft() + (upgradeSlotNumber % slotsPerRow) * 18;
slot.yPos = slotsTopY - screen.getGuiTop() + (upgradeSlotNumber / slotsPerRow) * 18;
upgradeSlotNumber++;
}
}

protected abstract int getSlotsPerRow();

protected void moveSlotsOutOfView() {
getContainer().getSlots().forEach(slot -> {
slot.xPos = -100;
Expand Down Expand Up @@ -180,10 +178,6 @@ private void setOpen(boolean isOpen) {
}
}

public boolean isOpen() {
return isOpen;
}

public Rectangle2d getRectangle() {
return new Rectangle2d(x, y, width, height);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ private UpgradeSettingsTabManager() {}

private static final Map<UpgradeContainerType<?, ?>, IUpgradeSettingsFactory<?, ?>> UPGRADE_TABS = new HashMap<>();

public static <W extends IUpgradeWrapper, C extends UpgradeContainerBase<W>, S extends UpgradeSettingsTab<C>> void register(UpgradeContainerType<W, C> containerType, IUpgradeSettingsFactory<C, S> upgradeSettingsFactory) {
public static <W extends IUpgradeWrapper, C extends UpgradeContainerBase<W, C>, S extends UpgradeSettingsTab<C>> void register(UpgradeContainerType<W, C> containerType, IUpgradeSettingsFactory<C, S> upgradeSettingsFactory) {
UPGRADE_TABS.put(containerType, upgradeSettingsFactory);
}

public static <C extends UpgradeContainerBase<?>> UpgradeSettingsTab<C> getTab(C container, Position position, BackpackScreen screen) {
public static <C extends UpgradeContainerBase<?, ?>> UpgradeSettingsTab<C> getTab(C container, Position position, BackpackScreen screen) {
return getFactory(container).create(container, position, screen);
}

@SuppressWarnings("unchecked")
private static <C extends UpgradeContainerBase<?>, S extends UpgradeSettingsTab<C>> IUpgradeSettingsFactory<C, S> getFactory(C container) {
private static <C extends UpgradeContainerBase<?, ?>, S extends UpgradeSettingsTab<C>> IUpgradeSettingsFactory<C, S> getFactory(C container) {
return (IUpgradeSettingsFactory<C, S>) getFactory(container.getType());
}

@SuppressWarnings("unchecked")
private static <W extends IUpgradeWrapper, C extends UpgradeContainerBase<W>, S extends UpgradeSettingsTab<C>> IUpgradeSettingsFactory<C, S> getFactory(UpgradeContainerType<W, C> containerType) {
private static <W extends IUpgradeWrapper, C extends UpgradeContainerBase<W, C>, S extends UpgradeSettingsTab<C>> IUpgradeSettingsFactory<C, S> getFactory(UpgradeContainerType<W, C> containerType) {
return (IUpgradeSettingsFactory<C, S>) UPGRADE_TABS.get(containerType);
}

public interface IUpgradeSettingsFactory<C extends UpgradeContainerBase<?>, S extends UpgradeSettingsTab<C>> {
public interface IUpgradeSettingsFactory<C extends UpgradeContainerBase<?, ?>, S extends UpgradeSettingsTab<C>> {
S create(C container, Position position, BackpackScreen screen);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class BackpackContainer extends Container {
private final IBackpackWrapper backpackWrapper;
private int backpackSlotNumber = -1;

private final Map<Integer, UpgradeContainerBase<?>> upgradeContainers = new LinkedHashMap<>();
private final Map<Integer, UpgradeContainerBase<?, ?>> upgradeContainers = new LinkedHashMap<>();
private Consumer<BackpackContainer> upgradeChangeListener = null;

public BackpackContainer(int windowId, PlayerEntity player, String handlerName, int backpackSlot) {
Expand Down Expand Up @@ -70,7 +70,7 @@ public BackpackContainer(int windowId, PlayerEntity player, BlockPos pos) {

private void removeUpgradeSettingsSlots() {
List<Integer> slotNumbersToRemove = new ArrayList<>();
for (UpgradeContainerBase<?> container : upgradeContainers.values()) {
for (UpgradeContainerBase<?, ?> container : upgradeContainers.values()) {
container.getSlots().forEach(slot -> {
slotNumbersToRemove.add(slot.slotNumber);
inventorySlots.remove(slot);
Expand All @@ -87,7 +87,7 @@ private void addUpgradeSettingsContainers(boolean isClientSide) {
upgradeHandler.getSlotWrappers().forEach((slot, wrapper) -> UpgradeContainerRegistry.instantiateContainer(slot, wrapper, isClientSide)
.ifPresent(container -> upgradeContainers.put(slot, container)));

for (UpgradeContainerBase<?> container : upgradeContainers.values()) {
for (UpgradeContainerBase<?, ?> container : upgradeContainers.values()) {
container.getSlots().forEach(this::addSlot);
}
}
Expand Down Expand Up @@ -275,7 +275,7 @@ public int getFirstUpgradeSettingsSlot() {
return getNumberOfSlots() + getNumberOfUpgradeSlots() + NUMBER_OF_PLAYER_SLOTS;
}

public Collection<UpgradeContainerBase<?>> getUpgradeContainers() {
public Collection<UpgradeContainerBase<?, ?>> getUpgradeContainers() {
return upgradeContainers.values();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,27 @@
import java.util.List;
import java.util.function.Supplier;

public abstract class UpgradeContainerBase<T extends IUpgradeWrapper> implements IServerUpdater {
public abstract class UpgradeContainerBase<W extends IUpgradeWrapper, C extends UpgradeContainerBase<W, C>> implements IServerUpdater {
protected final ArrayList<Slot> slots = new ArrayList<>();
private final int containerId;
protected final T upgradeWrapper;
protected final W upgradeWrapper;
protected final boolean isClientSide;
private final UpgradeContainerType<W, C> type;

protected UpgradeContainerBase(int containerId, T upgradeWrapper, boolean isClientSide) {
protected UpgradeContainerBase(int containerId, W upgradeWrapper, boolean isClientSide, UpgradeContainerType<W, C> type) {
this.containerId = containerId;
this.upgradeWrapper = upgradeWrapper;
this.isClientSide = isClientSide;
this.type = type;
}

public List<Slot> getSlots() {
return slots;
}

public abstract UpgradeContainerType<T, ? extends UpgradeContainerBase<T>> getType();
public UpgradeContainerType<W, C> getType() {
return type;
}

@Override
public void sendBooleanToServer(String key, boolean value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@
public class UpgradeContainerRegistry {
private UpgradeContainerRegistry() {}

private static final Map<ResourceLocation, UpgradeContainerType<? extends IUpgradeWrapper, ? extends UpgradeContainerBase<?>>> UPGRADE_CONTAINERS = new HashMap<>();
private static final Map<ResourceLocation, UpgradeContainerType<? extends IUpgradeWrapper, ? extends UpgradeContainerBase<?, ?>>> UPGRADE_CONTAINERS = new HashMap<>();

public static void register(ResourceLocation upgradeName, UpgradeContainerType<? extends IUpgradeWrapper, ? extends UpgradeContainerBase<?>> containerFactory) {
public static void register(ResourceLocation upgradeName, UpgradeContainerType<? extends IUpgradeWrapper, ? extends UpgradeContainerBase<?, ?>> containerFactory) {
UPGRADE_CONTAINERS.put(upgradeName, containerFactory);
}

public static <W extends IUpgradeWrapper> Optional<UpgradeContainerBase<W>> instantiateContainer(int containerId, W wrapper, boolean isClientSide) {
public static <W extends IUpgradeWrapper, C extends UpgradeContainerBase<W, C>> Optional<UpgradeContainerBase<W, C>> instantiateContainer(int containerId, W wrapper, boolean isClientSide) {
ResourceLocation upgradeName = wrapper.getUpgradeStack().getItem().getRegistryName();
if (!(wrapper.getUpgradeStack().getItem() instanceof IBackpackUpgradeItem<?>) || !UPGRADE_CONTAINERS.containsKey(upgradeName)) {
return Optional.empty();
}
//noinspection unchecked,ConstantConditions
return Optional.of((UpgradeContainerBase<W>) getContainerType(upgradeName).create(containerId, wrapper, isClientSide));
return Optional.of((UpgradeContainerBase<W, C>) getContainerType(upgradeName).create(containerId, wrapper, isClientSide));
}

private static <W extends IUpgradeWrapper, T extends UpgradeContainerBase<W>> UpgradeContainerType<W, T> getContainerType(ResourceLocation upgradeName) {
private static <W extends IUpgradeWrapper, C extends UpgradeContainerBase<W, C>> UpgradeContainerType<W, C> getContainerType(ResourceLocation upgradeName) {
//noinspection unchecked
return (UpgradeContainerType<W, T>) UPGRADE_CONTAINERS.get(upgradeName);
return (UpgradeContainerType<W, C>) UPGRADE_CONTAINERS.get(upgradeName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

import net.p3pp3rf1y.sophisticatedbackpacks.api.IUpgradeWrapper;

public class UpgradeContainerType<W extends IUpgradeWrapper, T extends UpgradeContainerBase<W>> {
private final IFactory<W, T> factory;
public class UpgradeContainerType<W extends IUpgradeWrapper, C extends UpgradeContainerBase<W, C>> {
private final IFactory<W, C> factory;

public UpgradeContainerType(IFactory<W, T> factory) {
public UpgradeContainerType(IFactory<W, C> factory) {
this.factory = factory;
}

public T create(int containerId, W wrapper, boolean isClientSide) {
return factory.create(containerId, wrapper, isClientSide);
public C create(int containerId, W wrapper, boolean isClientSide) {
return factory.create(containerId, wrapper, isClientSide, this);
}

public interface IFactory<W extends IUpgradeWrapper, T extends UpgradeContainerBase<W>> {
T create(int containerId, W upgrade, boolean isClientSide);
public interface IFactory<W extends IUpgradeWrapper, C extends UpgradeContainerBase<W, C>> {
C create(int containerId, W upgrade, boolean isClientSide, UpgradeContainerType<W, C> type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ public static void registerHandlers(IEventBus modBus) {
}

public static void registerContainers(RegistryEvent.Register<ContainerType<?>> evt) {
UpgradeContainerRegistry.register(PICKUP_UPGRADE.getId(), PickupUpgradeContainer.Basic.TYPE);
UpgradeContainerRegistry.register(ADVANCED_PICKUP_UPGRADE.getId(), PickupUpgradeContainer.Advanced.TYPE);
UpgradeContainerRegistry.register(FILTER_UPGRADE.getId(), FilterUpgradeContainer.Basic.TYPE);
UpgradeContainerRegistry.register(ADVANCED_FILTER_UPGRADE.getId(), FilterUpgradeContainer.Advanced.TYPE);
UpgradeContainerRegistry.register(PICKUP_UPGRADE.getId(), PickupUpgradeContainer.BASIC_TYPE);
UpgradeContainerRegistry.register(ADVANCED_PICKUP_UPGRADE.getId(), PickupUpgradeContainer.ADVANCED_TYPE);
UpgradeContainerRegistry.register(FILTER_UPGRADE.getId(), FilterUpgradeContainer.BASIC_TYPE);
UpgradeContainerRegistry.register(ADVANCED_FILTER_UPGRADE.getId(), FilterUpgradeContainer.ADVANCED_TYPE);

DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {
ScreenManager.registerFactory(BACKPACK_ITEM_CONTAINER_TYPE.get(), BackpackScreen::new);
ScreenManager.registerFactory(BACKPACK_BLOCK_CONTAINER_TYPE.get(), BackpackScreen::new);

UpgradeSettingsTabManager.register(PickupUpgradeContainer.Basic.TYPE, PickupUpgradeTab.Basic::new);
UpgradeSettingsTabManager.register(PickupUpgradeContainer.Advanced.TYPE, PickupUpgradeTab.Advanced::new);
UpgradeSettingsTabManager.register(FilterUpgradeContainer.Basic.TYPE, FilterUpgradeTab.Basic::new);
UpgradeSettingsTabManager.register(FilterUpgradeContainer.Advanced.TYPE, FilterUpgradeTab.Advanced::new);
UpgradeSettingsTabManager.register(PickupUpgradeContainer.BASIC_TYPE, PickupUpgradeTab.Basic::new);
UpgradeSettingsTabManager.register(PickupUpgradeContainer.ADVANCED_TYPE, PickupUpgradeTab.Advanced::new);
UpgradeSettingsTabManager.register(FilterUpgradeContainer.BASIC_TYPE, FilterUpgradeTab.Basic::new);
UpgradeSettingsTabManager.register(FilterUpgradeContainer.ADVANCED_TYPE, FilterUpgradeTab.Advanced::new);
});

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package net.p3pp3rf1y.sophisticatedbackpacks.upgrades;

import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.items.ItemStackHandler;
import net.p3pp3rf1y.sophisticatedbackpacks.util.FilterItemStackHandler;
import net.p3pp3rf1y.sophisticatedbackpacks.util.InventoryHelper;
Expand Down Expand Up @@ -30,6 +33,21 @@ protected void onContentsChanged(int slot) {
upgrade.setTagInfo("filters", serializeNBT());
save();
}

@Override
public void deserializeNBT(CompoundNBT nbt) {
setSize(filterSlotCount);
ListNBT tagList = nbt.getList("Items", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < tagList.size(); i++) {
CompoundNBT itemTags = tagList.getCompound(i);
int slot = itemTags.getInt("Slot");

if (slot >= 0 && slot < stacks.size()) {
stacks.set(slot, ItemStack.read(itemTags));
}
}
onLoad();
}
};
NBTHelper.getCompound(upgrade, "filters").ifPresent(filterHandler::deserializeNBT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import net.p3pp3rf1y.sophisticatedbackpacks.upgrades.IFilteredUpgradeContainer;
import net.p3pp3rf1y.sophisticatedbackpacks.util.NBTHelper;

public abstract class FilterUpgradeContainer extends UpgradeContainerBase<FilterUpgradeWrapper> implements IFilteredUpgradeContainer {
public class FilterUpgradeContainer extends UpgradeContainerBase<FilterUpgradeWrapper, FilterUpgradeContainer> implements IFilteredUpgradeContainer {
public static final UpgradeContainerType<FilterUpgradeWrapper, FilterUpgradeContainer> BASIC_TYPE = new UpgradeContainerType<>(FilterUpgradeContainer::new);
public static final UpgradeContainerType<FilterUpgradeWrapper, FilterUpgradeContainer> ADVANCED_TYPE = new UpgradeContainerType<>(FilterUpgradeContainer::new);
private static final String DATA_DIRECTION = "direction";
private final FilterLogicContainer filterLogicContainer;

private FilterUpgradeContainer(int containerId, FilterUpgradeWrapper wrapper, boolean isClientSide) {
super(containerId, wrapper, isClientSide);
private FilterUpgradeContainer(int containerId, FilterUpgradeWrapper wrapper, boolean isClientSide, UpgradeContainerType<FilterUpgradeWrapper, FilterUpgradeContainer> type) {
super(containerId, wrapper, isClientSide, type);
filterLogicContainer = new FilterLogicContainer(wrapper.getFilterLogic(), this, slots::add);
}

Expand Down Expand Up @@ -40,30 +42,4 @@ public void handleMessage(CompoundNBT data) {
setDirection(Direction.fromName(data.getString(DATA_DIRECTION)));
}
}

public static class Basic extends FilterUpgradeContainer {
public static final UpgradeContainerType<FilterUpgradeWrapper, FilterUpgradeContainer> TYPE = new UpgradeContainerType<>(Basic::new);

private Basic(int containerId, FilterUpgradeWrapper wrapper, boolean isClientSide) {
super(containerId, wrapper, isClientSide);
}

@Override
public UpgradeContainerType<FilterUpgradeWrapper, FilterUpgradeContainer> getType() {
return TYPE;
}
}

public static class Advanced extends FilterUpgradeContainer {
public static final UpgradeContainerType<FilterUpgradeWrapper, FilterUpgradeContainer> TYPE = new UpgradeContainerType<>(Advanced::new);

private Advanced(int containerId, FilterUpgradeWrapper wrapper, boolean isClientSide) {
super(containerId, wrapper, isClientSide);
}

@Override
public UpgradeContainerType<FilterUpgradeWrapper, FilterUpgradeContainer> getType() {
return TYPE;
}
}
}
Loading

0 comments on commit 2896527

Please sign in to comment.