Skip to content

Commit

Permalink
Chat rule location config overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
Emirlol committed Jan 16, 2025
1 parent 15bce9f commit de6bd29
Show file tree
Hide file tree
Showing 10 changed files with 568 additions and 515 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,61 @@
import net.minecraft.client.gui.widget.ElementListWidget;
import net.minecraft.text.Text;

import java.util.Collection;
import java.util.List;

public class ItemTickList extends ElementListWidget<ItemTickList.ItemTickEntry> {
private final List<String> filters;
private final List<String> allItems;
/**
* A checkbox list for filter configuring purposes.
*/
public class ItemTickList<T> extends ElementListWidget<ItemTickList.ItemTickEntry> {
private final Collection<T> filters;
private final Collection<T> allItems;
private final boolean whitelist;

public ItemTickList(MinecraftClient minecraftClient, int width, int height, int y, int entryHeight, List<String> filters, List<String> allItems) {
/**
*
* @param minecraftClient Minecraft client.
* @param width The width of the list.
* @param height The height of the list.
* @param y The y value at which the list should render.
* @param entryHeight Height of a single item
* @param filters The items that will be marked. This should be a subset of allItems.
* @param allItems All possible values
*/
public ItemTickList(MinecraftClient minecraftClient, int width, int height, int y, int entryHeight, Collection<T> filters, Collection<T> allItems) {
super(minecraftClient, width, height, y, entryHeight);
this.filters = filters;
this.allItems = allItems;
this.whitelist = false;
}

/**
* @param minecraftClient Minecraft client.
* @param width The width of the list.
* @param height The height of the list.
* @param y The y value at which the list should render.
* @param entryHeight Height of a single item
* @param filters The items that will be marked. This should be a subset of allItems.
* @param allItems All possible values
* @param whitelist Whether the filter logic works as a whitelist or blacklist, to change whether the boxes for items in the filters collection should be checked. As an example: PowderFilter keeps which items to remove inside the filter (blacklist), while ChatRuleLocation keeps which locations the feature should work in (whitelist).
*/
public ItemTickList(MinecraftClient minecraftClient, int width, int height, int y, int entryHeight, Collection<T> filters, Collection<T> allItems, boolean whitelist) {
super(minecraftClient, width, height, y, entryHeight);
this.filters = filters;
this.allItems = allItems;
this.whitelist = whitelist;
}

public void clearAndInit() {
clearEntries();
init();
}

public ItemTickList init() {
for (String item : allItems) {
public ItemTickList<T> init() {
for (T item : allItems) {
ItemTickEntry entry = new ItemTickEntry(
CheckboxWidget.builder(Text.of(item), client.textRenderer)
.checked(!filters.contains(item))
CheckboxWidget.builder(Text.of(item.toString()), client.textRenderer)
.checked(whitelist == filters.contains(item))
.callback((checkbox1, checked) -> {
if (checked) filters.remove(item);
else filters.add(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected void init() {
assert client != null;
context.drawCenteredTextWithShadow(client.textRenderer, Text.translatable("skyblocker.config.mining.crystalHollows.powderTrackerFilter.screenTitle").formatted(Formatting.BOLD), width / 2, (32 - client.textRenderer.fontHeight) / 2, 0xFFFFFF);
});
ItemTickList itemTickList = addDrawableChild(new ItemTickList(MinecraftClient.getInstance(), width, height - 96, 32, 24, filters, allItems).init());
ItemTickList<String> itemTickList = addDrawableChild(new ItemTickList<>(MinecraftClient.getInstance(), width, height - 96, 32, 24, filters, allItems).init());
//Grid code gratuitously stolen from WaypointsScreen. Same goes for the y and heights above.
GridWidget gridWidget = new GridWidget();
gridWidget.getMainPositioner().marginX(5).marginY(2);
Expand Down
106 changes: 41 additions & 65 deletions src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package de.hysky.skyblocker.skyblock.chat;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.hysky.skyblocker.utils.Location;
import de.hysky.skyblocker.utils.Utils;
import net.minecraft.sound.SoundEvent;
import org.jetbrains.annotations.Nullable;

import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

/**
* Data class to contain all the settings for a chat rule
*/
Expand All @@ -22,7 +23,7 @@ public class ChatRule {
Codec.BOOL.fieldOf("isRegex").forGetter(ChatRule::getRegex),
Codec.BOOL.fieldOf("isIgnoreCase").forGetter(ChatRule::getIgnoreCase),
Codec.STRING.fieldOf("filter").forGetter(ChatRule::getFilter),
Codec.STRING.fieldOf("validLocations").forGetter(ChatRule::getValidLocations),
Location.SET_CODEC.fieldOf("validLocations").forGetter(ChatRule::getValidLocations),
Codec.BOOL.fieldOf("hideMessage").forGetter(ChatRule::getHideMessage),
Codec.BOOL.fieldOf("showActionBar").forGetter(ChatRule::getShowActionBar),
Codec.BOOL.fieldOf("showAnnouncement").forGetter(ChatRule::getShowAnnouncement),
Expand All @@ -33,20 +34,21 @@ public class ChatRule {

private String name;

//inputs
private Boolean enabled;
private Boolean isPartialMatch;
private Boolean isRegex;
private Boolean isIgnoreCase;
// Inputs
private boolean enabled;
private boolean isPartialMatch;
private boolean isRegex;
private boolean isIgnoreCase;
private String filter;
private String validLocations;
private EnumSet<Location> validLocations;

//output
private Boolean hideMessage;
private Boolean showActionBar;
private Boolean showAnnouncement;
// Outputs
private boolean hideMessage;
private boolean showActionBar;
private boolean showAnnouncement;
private String replaceMessage;
private SoundEvent customSound;

/**
* Creates a chat rule with default options.
*/
Expand All @@ -58,7 +60,7 @@ protected ChatRule() {
this.isRegex = false;
this.isIgnoreCase = true;
this.filter = "";
this.validLocations = "";
this.validLocations = EnumSet.noneOf(Location.class);

this.hideMessage = true;
this.showActionBar = false;
Expand All @@ -67,7 +69,7 @@ protected ChatRule() {
this.customSound = null;
}

public ChatRule(String name, Boolean enabled, Boolean isPartialMatch, Boolean isRegex, Boolean isIgnoreCase, String filter, String validLocations, Boolean hideMessage, Boolean showActionBar, Boolean showAnnouncement, String replaceMessage, SoundEvent customSound) {
public ChatRule(String name, boolean enabled, boolean isPartialMatch, boolean isRegex, boolean isIgnoreCase, String filter, EnumSet<Location> validLocations, boolean hideMessage, boolean showActionBar, boolean showAnnouncement, @Nullable String replaceMessage, @Nullable SoundEvent customSound) {
this.name = name;
this.enabled = enabled;
this.isPartialMatch = isPartialMatch;
Expand All @@ -82,7 +84,7 @@ public ChatRule(String name, Boolean enabled, Boolean isPartialMatch, Boolean is
this.customSound = customSound;
}

private ChatRule(String name, Boolean enabled, Boolean isPartialMatch, Boolean isRegex, Boolean isIgnoreCase, String filter, String validLocations, Boolean hideMessage, Boolean showActionBar, Boolean showAnnouncement, Optional<String> replaceMessage, Optional<SoundEvent> customSound) {
private ChatRule(String name, boolean enabled, boolean isPartialMatch, boolean isRegex, boolean isIgnoreCase, String filter, EnumSet<Location> validLocations, boolean hideMessage, boolean showActionBar, boolean showAnnouncement, Optional<String> replaceMessage, Optional<SoundEvent> customSound) {
this(name, enabled, isPartialMatch, isRegex, isIgnoreCase, filter, validLocations, hideMessage, showActionBar, showAnnouncement, replaceMessage.orElse(null), customSound.orElse(null));
}

Expand All @@ -94,35 +96,35 @@ protected void setName(String name) {
this.name = name;
}

protected Boolean getEnabled() {
protected boolean getEnabled() {
return enabled;
}

protected void setEnabled(Boolean enabled) {
protected void setEnabled(boolean enabled) {
this.enabled = enabled;
}

protected Boolean getPartialMatch() {
protected boolean getPartialMatch() {
return isPartialMatch;
}

protected void setPartialMatch(Boolean partialMatch) {
protected void setPartialMatch(boolean partialMatch) {
isPartialMatch = partialMatch;
}

protected Boolean getRegex() {
protected boolean getRegex() {
return isRegex;
}

protected void setRegex(Boolean regex) {
protected void setRegex(boolean regex) {
isRegex = regex;
}

protected Boolean getIgnoreCase() {
protected boolean getIgnoreCase() {
return isIgnoreCase;
}

protected void setIgnoreCase(Boolean ignoreCase) {
protected void setIgnoreCase(boolean ignoreCase) {
isIgnoreCase = ignoreCase;
}

Expand All @@ -134,27 +136,27 @@ protected void setFilter(String filter) {
this.filter = filter;
}

protected Boolean getHideMessage() {
protected boolean getHideMessage() {
return hideMessage;
}

protected void setHideMessage(Boolean hideMessage) {
protected void setHideMessage(boolean hideMessage) {
this.hideMessage = hideMessage;
}

protected Boolean getShowActionBar() {
protected boolean getShowActionBar() {
return showActionBar;
}

protected void setShowActionBar(Boolean showActionBar) {
protected void setShowActionBar(boolean showActionBar) {
this.showActionBar = showActionBar;
}

protected Boolean getShowAnnouncement() {
protected boolean getShowAnnouncement() {
return showAnnouncement;
}

protected void setShowAnnouncement(Boolean showAnnouncement) {
protected void setShowAnnouncement(boolean showAnnouncement) {
this.showAnnouncement = showAnnouncement;
}

Expand Down Expand Up @@ -182,11 +184,11 @@ protected void setCustomSound(SoundEvent customSound) {
this.customSound = customSound;
}

protected String getValidLocations() {
protected EnumSet<Location> getValidLocations() {
return validLocations;
}

protected void setValidLocations(String validLocations) {
protected void setValidLocations(EnumSet<Location> validLocations) {
this.validLocations = validLocations;
}

Expand All @@ -195,7 +197,7 @@ protected void setValidLocations(String validLocations) {
* @param inputString the chat message to check if fits
* @return if the inputs are all true and the outputs should be performed
*/
protected Boolean isMatch(String inputString) {
protected boolean isMatch(String inputString) {
//enabled
if (!enabled) return false;

Expand Down Expand Up @@ -227,36 +229,10 @@ protected Boolean isMatch(String inputString) {
}
}

//location
if (validLocations.isBlank()) { //if no locations do not check
return true;
}

String cleanedMapLocation = Utils.getMap().toLowerCase().replace(" ", "");
Boolean isLocationValid = null;
for (String validLocation : validLocations.replace(" ", "").toLowerCase().split(",")) {//the locations are split by "," and start with ! if not locations
if (validLocation == null) continue;
if (validLocation.startsWith("!")) {//not location
if (Objects.equals(validLocation.substring(1), cleanedMapLocation)) {
isLocationValid = false;
break;
} else {
isLocationValid = true;
}
} else {
if (Objects.equals(validLocation, cleanedMapLocation)) { //normal location
isLocationValid = true;
break;
}
}
}

//if location is not in the list at all and is a not a "!" location or and is a normal location
if (isLocationValid != null && isLocationValid) {
return true;
}

return false;
// As a special case, if there are no valid locations all locations are valid.
// This exists because it doesn't make sense to remove all valid locations, you should disable the chat rule if you want to do that.
// This way, we can also default to an empty set for validLocations.
return validLocations.isEmpty() || validLocations.contains(Utils.getLocation());
}
}

Expand Down
Loading

0 comments on commit de6bd29

Please sign in to comment.