Skip to content

Commit

Permalink
Fix #864
Browse files Browse the repository at this point in the history
  • Loading branch information
shedaniel committed Jun 27, 2022
1 parent c20ebf2 commit 1f6fa90
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@ private static Jankson buildJankson(Jankson.Builder builder) {
}
});

// CompoundTag
builder.registerSerializer(CompoundTag.class, (value, marshaller) -> {
return marshaller.serialize(value.toString());
});
builder.registerDeserializer(String.class, CompoundTag.class, (value, marshaller) -> {
try {
return TagParser.parseTag(value);
} catch (CommandSyntaxException e) {
throw new DeserializationException(e);
}
});

// EntryStackProvider
builder.registerSerializer(EntryStackProvider.class, (stack, marshaller) -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.Mth;
import net.minecraft.world.level.GameType;
import org.jetbrains.annotations.ApiStatus;
Expand Down Expand Up @@ -369,12 +370,16 @@ public boolean isLowerConfigButton() {
@Override
public List<FavoriteEntry> getFavoriteEntries() {
return basics.favorites;
}
}

public List<FavoriteEntry> getHiddenFavoriteEntries() {
return basics.hiddenFavorites;
}

public List<CompoundTag> getDisplayHistory() {
return basics.displayHistory;
}

@Override
public List<EntryStackProvider<?>> getFilteredStackProviders() {
return advanced.filtering.filteredStacks;
Expand Down Expand Up @@ -531,6 +536,7 @@ public void setJEICompatibilityLayerEnabled(boolean value) {
public static class Basics {
@ConfigEntry.Gui.Excluded public List<FavoriteEntry> favorites = new ArrayList<>();
@ConfigEntry.Gui.Excluded public List<FavoriteEntry> hiddenFavorites = new ArrayList<>();
@ConfigEntry.Gui.Excluded public List<CompoundTag> displayHistory = new ArrayList<>();
@Comment("Declares whether cheating mode is on.") @ConfigEntry.Gui.EnumHandler(option = ConfigEntry.Gui.EnumHandler.EnumDisplayOption.BUTTON)
private CheatingMode cheating = CheatingMode.OFF;
private boolean favoritesEnabled = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import me.shedaniel.rei.impl.client.config.ConfigObjectImpl;
import me.shedaniel.rei.impl.client.favorites.FavoriteEntryTypeRegistryImpl;
import me.shedaniel.rei.impl.client.gui.ScreenOverlayImpl;
import me.shedaniel.rei.impl.client.gui.widget.favorites.history.DisplayHistoryManager;
import me.shedaniel.rei.impl.client.gui.widget.favorites.history.DisplayHistoryWidget;
import me.shedaniel.rei.impl.client.gui.widget.favorites.listeners.FavoritesRegionListener;
import me.shedaniel.rei.impl.client.gui.widget.favorites.listeners.FavoritesSystemRegionListener;
Expand Down Expand Up @@ -190,7 +191,7 @@ public void render(PoseStack matrices, int mouseX, int mouseY, float delta) {
boolean draggingDisplay = DraggingContext.getInstance().isDraggingComponent()
&& DraggingContext.getInstance().getDragged().get() instanceof Display;
int topOffsetHeight = 0;
this.favoritesBounds = displayHistory.getEntries().isEmpty() && !draggingDisplay
this.favoritesBounds = DisplayHistoryManager.INSTANCE.getEntries(displayHistory).isEmpty() && !draggingDisplay
? fullBounds : RectangleUtils.excludeZones(this.fullBounds, Stream.of(displayHistory.createBounds(this.excludedBounds)));

systemRegion.getBounds().setBounds(this.favoritesBounds.x, this.favoritesBounds.y + 1, this.favoritesBounds.width, Math.max(1, systemRegion.scrolling.getMaxScrollHeight()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,39 @@
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class DisplayEntry extends WidgetWithBounds {
private final LazyResettable<List<Widget>> widgets = new LazyResettable<>(this::setupWidgets);
private final DisplayHistoryWidget parent;
private final Display display;
private final Dimension size = new Dimension(1, 1);
private boolean hasInitialBounds;
private final ValueAnimator<FloatingRectangle> bounds = ValueAnimator.ofFloatingRectangle();
private final Button plusButton;
private double xOffset = 0;
private boolean reachedStable = false;
private UUID uuid = UUID.randomUUID();

public DisplayEntry(DisplayHistoryWidget parent, Display display, Rectangle initialBounds) {
public DisplayEntry(DisplayHistoryWidget parent, Display display, @Nullable Rectangle initialBounds) {
this.display = display;
this.parent = parent;
this.bounds.setAs(initialBounds.getFloatingBounds());
this.plusButton = Widgets.createButton(new Rectangle(initialBounds.getMaxX() - 16, initialBounds.getMaxY() - 16, 10, 10), Component.literal("+"));
this.hasInitialBounds = initialBounds != null;
if (this.hasInitialBounds) {
this.bounds.setAs(initialBounds.getFloatingBounds());
this.plusButton = Widgets.createButton(new Rectangle(initialBounds.getMaxX() - 16, initialBounds.getMaxY() - 16, 10, 10), Component.literal("+"));
} else {
this.plusButton = Widgets.createButton(new Rectangle(-1000, -1000, 10, 10), new TextComponent("+"));
}
}

public UUID getUuid() {
return uuid;
}

public void markBoundsDirty() {
Expand All @@ -85,10 +98,15 @@ private List<Widget> setupWidgets() {
float x = parentBounds.getCenterX() - displayBounds.width / 2 * scale;
float y = parentBounds.getCenterY() - displayBounds.height / 2 * scale;
FloatingRectangle newBounds = new Rectangle(x, y, displayBounds.width * scale, displayBounds.height * scale).getFloatingBounds();
if (this.size.width == 1 && this.size.height == 1) {
this.bounds.setTo(newBounds, 700);
if (hasInitialBounds) {
if (this.size.width == 1 && this.size.height == 1) {
this.bounds.setTo(newBounds, 700);
} else {
this.bounds.setAs(newBounds);
}
} else {
this.bounds.setAs(newBounds);
hasInitialBounds = true;
}
this.size.setSize(displayBounds.getSize());
return widgets;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* This file is licensed under the MIT License, part of Roughly Enough Items.
* Copyright (c) 2018, 2019, 2020, 2021, 2022 shedaniel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package me.shedaniel.rei.impl.client.gui.widget.favorites.history;

import com.google.common.collect.Iterables;
import me.shedaniel.math.Rectangle;
import me.shedaniel.rei.api.client.registry.category.CategoryRegistry;
import me.shedaniel.rei.api.common.category.CategoryIdentifier;
import me.shedaniel.rei.api.common.display.Display;
import me.shedaniel.rei.api.common.display.DisplaySerializerRegistry;
import me.shedaniel.rei.api.common.plugins.PluginManager;
import me.shedaniel.rei.impl.client.config.ConfigManagerImpl;
import me.shedaniel.rei.impl.common.InternalLogger;
import net.minecraft.Util;
import net.minecraft.nbt.CompoundTag;
import org.jetbrains.annotations.Nullable;

import java.util.*;

public class DisplayHistoryManager {
public static final DisplayHistoryManager INSTANCE = new DisplayHistoryManager();
private Map<String, DisplayEntry> entries = new LinkedHashMap<>();
private long lastCheckTime = -1;

public Collection<DisplayEntry> getEntries(DisplayHistoryWidget parent) {
if ((lastCheckTime == -1 || Util.getMillis() - lastCheckTime > 4000) && !PluginManager.areAnyReloading()) {
updateEntries(parent);
lastCheckTime = Util.getMillis();
}

return Collections.unmodifiableCollection(entries.values());
}

private void updateEntries(DisplayHistoryWidget parent) {
List<CompoundTag> displayHistory = ConfigManagerImpl.getInstance().getConfig().getDisplayHistory();
Map<String, DisplayEntry> copy = new LinkedHashMap<>(entries);
entries.clear();
for (CompoundTag tag : displayHistory) {
String uuid = tag.getString("DisplayHistoryUUID");

DisplayEntry entry = copy.get(uuid);
if (entry != null) {
entries.put(entry.getUuid().toString(), entry);
} else if (tag.getBoolean("DisplayHistoryContains")) {
try {
CategoryIdentifier<?> categoryIdentifier = CategoryIdentifier.of(tag.getString("DisplayHistoryCategory"));
if (CategoryRegistry.getInstance().tryGet(categoryIdentifier).isPresent()) {
Display display = DisplaySerializerRegistry.getInstance().read(categoryIdentifier, tag.getCompound("DisplayHistoryData"));
DisplayEntry newEntry = new DisplayEntry(parent, display, null);
entries.put(newEntry.getUuid().toString(), newEntry);
}
} catch (Exception e) {
InternalLogger.getInstance().warn("Failed to read display history entry", e);
}
}
}
}

public void removeEntry(DisplayEntry entry) {
this.entries.remove(entry.getUuid().toString());
List<CompoundTag> displayHistory = ConfigManagerImpl.getInstance().getConfig().getDisplayHistory();
displayHistory.removeIf(tag -> tag.getString("DisplayHistoryUUID").equals(entry.getUuid().toString()));
save();
}

public void addEntry(DisplayHistoryWidget parent, @Nullable Rectangle bounds, Display display) {
List<CompoundTag> displayHistory = ConfigManagerImpl.getInstance().getConfig().getDisplayHistory();
Iterator<DisplayEntry> iterator = this.entries.values().iterator();
while (iterator.hasNext()) {
DisplayEntry entry = iterator.next();
if (entry.getDisplay() == display) {
displayHistory.removeIf(tag -> tag.getString("DisplayHistoryUUID").equals(entry.getUuid().toString()));
iterator.remove();
}
}
DisplayEntry newEntry = new DisplayEntry(parent, display, bounds);
Map<String, DisplayEntry> copy = new LinkedHashMap<>();
copy.put(newEntry.getUuid().toString(), newEntry);
copy.putAll(this.entries);
this.entries = copy;
while (entries.size() >= 10) {
DisplayEntry entry = Iterables.get(entries.values(), entries.size() - 1);
displayHistory.removeIf(tag -> tag.getString("DisplayHistoryUUID").equals(entry.getUuid().toString()));
this.entries.remove(entry.getUuid().toString());
}

CompoundTag compoundTag = new CompoundTag();
compoundTag.putBoolean("DisplayHistoryContains", false);
compoundTag.putString("DisplayHistoryUUID", newEntry.getUuid().toString());
compoundTag.putString("DisplayHistoryCategory", display.getCategoryIdentifier().toString());
displayHistory.add(compoundTag);

save();
}

private void save() {
List<CompoundTag> displayHistory = ConfigManagerImpl.getInstance().getConfig().getDisplayHistory();
for (CompoundTag compoundTag : displayHistory) {
String uuid = compoundTag.getString("DisplayHistoryUUID");
DisplayEntry entry = entries.get(uuid);

if (entry != null) {
compoundTag.putBoolean("DisplayHistoryContains", false);
Display display = entry.getDisplay();
boolean hasSerializer = DisplaySerializerRegistry.getInstance().hasSerializer(display.getCategoryIdentifier());

if (hasSerializer) {
try {
compoundTag.put("DisplayHistoryData", DisplaySerializerRegistry.getInstance().save(display, new CompoundTag()));
compoundTag.putBoolean("DisplayHistoryContains", true);
} catch (Exception e) {
InternalLogger.getInstance().warn("Failed to save display history entry", e);
}
}
}
}

ConfigManagerImpl.getInstance().saveConfig();
}
}
Loading

0 comments on commit 1f6fa90

Please sign in to comment.