Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FTB-Mods-Issues#81, #41, #28 #637

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import dev.ftb.mods.ftbquests.quest.TeamData;
import dev.ftb.mods.ftbquests.quest.task.DimensionTask;
import dev.ftb.mods.ftbquests.quest.task.KillTask;
import dev.ftb.mods.ftbquests.quest.task.ScoreboardTask;
import dev.ftb.mods.ftbquests.quest.task.Task;
import dev.ftb.mods.ftbquests.util.FTBQuestsInventoryListener;
import dev.ftb.mods.ftbteams.event.PlayerChangedTeamEvent;
Expand All @@ -26,6 +27,7 @@
import net.minecraft.commands.Commands;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.ServerScoreboard;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.Container;
Expand All @@ -38,7 +40,6 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.Level;

import java.util.List;

/**
Expand Down Expand Up @@ -72,6 +73,13 @@ public void init() {

private void serverAboutToStart(MinecraftServer server) {
ServerQuestFile.INSTANCE = new ServerQuestFile(server);
final ServerScoreboard scoreboard = ServerQuestFile.INSTANCE.server.getScoreboard();
scoreboard.addDirtyListener(() -> {
ServerQuestFile file = ServerQuestFile.INSTANCE;
for (ScoreboardTask task : file.collect(ScoreboardTask.class)) {
task.test(file, scoreboard);
}
});
}

private void registerCommands(CommandDispatcher<CommandSourceStack> dispatcher, Commands.CommandSelection selection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ public void onClicked(MouseButton button) {
getGui().openContextMenu(contextMenu2);
}));

contextMenu.add(new ContextMenuItem(new TranslatableComponent("ftbquests.gui.clear_reward_all"), ThemeProperties.DELETE_ICON.get(quest), () -> {
for (Movable movable : questScreen.selectedObjects) {
if (movable instanceof Quest) {
Quest q = (Quest)movable;
q.rewards.clear();
new EditObjectMessage(q).sendToServer();
}
}
}));

contextMenu.add(new ContextMenuItem(new TranslatableComponent("selectServer.delete"), ThemeProperties.DELETE_ICON.get(quest), () -> {
questScreen.selectedObjects.forEach(q -> {
if (q instanceof Quest) {
Expand Down Expand Up @@ -214,11 +224,8 @@ public void addMouseOverText(TooltipList list) {
list.add(new TranslatableComponent("ftbquests.gui.move_tooltip").withStyle(ChatFormatting.DARK_GRAY));
}
});

// contextMenu.add(new ContextMenuItem(new TranslatableComponent("ftbquests.gui.edit_text"), GuiIcons.INFO, () -> TextEditorFrame.open(quest)));

contextMenu.add(ContextMenuItem.SEPARATOR);
questScreen.addObjectMenuItems(contextMenu, getGui(), quest);
// contextMenu.add(new ContextMenuItem(new TranslatableComponent("ftbquests.gui.edit_text"), GuiIcons.INFO, () -> TextEditorFrame.open(quest)));
}

getGui().openContextMenu(contextMenu);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,16 @@ public void toggleSelected(Movable movable) {
}
}


public void addObjectMenuItems(List<ContextMenuItem> contextMenu, Runnable gui, QuestObjectBase object) {
ConfigGroup group = new ConfigGroup(FTBQuests.MOD_ID);
ConfigGroup g = object.createSubGroup(group);
object.getConfig(g);

contextMenu.add(new ContextMenuItem(new TranslatableComponent("selectServer.edit"), ThemeProperties.EDIT_ICON.get(), () -> object.onEditButtonClicked(gui)));
contextMenu.add(ContextMenuItem.SEPARATOR);


if (!g.getValues().isEmpty()) {
List<ContextMenuItem> list = new ArrayList<>();

Expand Down Expand Up @@ -222,8 +227,6 @@ public void drawIcon(PoseStack matrixStack, Theme theme, int x, int y, int w, in
}
}

contextMenu.add(new ContextMenuItem(new TranslatableComponent("selectServer.edit"), ThemeProperties.EDIT_ICON.get(), () -> object.onEditButtonClicked(gui)));

if (object instanceof RandomReward && !QuestObjectBase.isNull(((RandomReward) object).getTable())) {
contextMenu.add(new ContextMenuItem(new TranslatableComponent("ftbquests.reward_table.edit"), ThemeProperties.EDIT_ICON.get(), () -> ((RandomReward) object).getTable().onEditButtonClicked(gui)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ public CursorType getCursor() {

if (quest.minWidth > 0) {
w = Math.max(quest.minWidth, w);
} else if (questScreen.selectedChapter.defaultMinWidth > 0) {
w = Math.max(questScreen.selectedChapter.defaultMinWidth, w);
}

titleField.setPosAndSize(27, 4, w - 54, 8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public final class Chapter extends QuestObject {
public String defaultQuestShape;
public final List<ChapterImage> images;
public boolean defaultHideDependencyLines;
public int defaultMinWidth = 0;

public Chapter(QuestFile f, ChapterGroup g) {
file = f;
Expand Down Expand Up @@ -102,6 +103,8 @@ public void writeData(CompoundTag nbt) {

nbt.put("images", list);
}

nbt.putInt("default_min_width", defaultMinWidth);
}

@Override
Expand Down Expand Up @@ -134,6 +137,8 @@ public void readData(CompoundTag nbt) {
image.readData(imgs.getCompound(i));
images.add(image);
}

defaultMinWidth = nbt.getInt("default_min_width");
}

@Override
Expand All @@ -145,6 +150,7 @@ public void writeNetData(FriendlyByteBuf buffer) {
buffer.writeUtf(defaultQuestShape, Short.MAX_VALUE);
NetUtils.write(buffer, images, (d, img) -> img.writeNetData(d));
buffer.writeBoolean(defaultHideDependencyLines);
buffer.writeInt(defaultMinWidth);
}

@Override
Expand All @@ -160,6 +166,7 @@ public void readNetData(FriendlyByteBuf buffer) {
return image;
});
defaultHideDependencyLines = buffer.readBoolean();
defaultMinWidth = buffer.readInt();
}

public int getIndex() {
Expand Down Expand Up @@ -310,6 +317,7 @@ public void getConfig(ConfigGroup config) {
config.addBool("always_invisible", alwaysInvisible, v -> alwaysInvisible = v, false);
config.addEnum("default_quest_shape", defaultQuestShape.isEmpty() ? "default" : defaultQuestShape, v -> defaultQuestShape = v.equals("default") ? "" : v, QuestShape.idMapWithDefault);
config.addBool("default_hide_dependency_lines", defaultHideDependencyLines, v -> defaultHideDependencyLines = v, false);
config.addInt("default_min_width", defaultMinWidth, v -> defaultMinWidth = v, 0, 0, 3000);
}

@Override
Expand Down
38 changes: 38 additions & 0 deletions common/src/main/java/dev/ftb/mods/ftbquests/quest/Quest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public final class Quest extends QuestObject implements Movable {
public double size;
public boolean optional;
public int minWidth;
public boolean invisible;
public int invisibleUntilTasks;

private Component cachedSubtitle = null;
private Component[] cachedDescription = null;
Expand Down Expand Up @@ -184,6 +186,14 @@ public void writeData(CompoundTag nbt) {
if (minWidth > 0) {
nbt.putInt("min_width", minWidth);
}

if (invisible) {
nbt.putBoolean("invisible", true);
}

if (invisibleUntilTasks > 0) {
nbt.putInt("invisible_until_tasks", invisibleUntilTasks);
}
}

@Override
Expand Down Expand Up @@ -238,6 +248,8 @@ public void readData(CompoundTag nbt) {
size = nbt.contains("size") ? nbt.getDouble("size") : 1D;
optional = nbt.getBoolean("optional");
minWidth = nbt.getInt("min_width");
invisible = nbt.getBoolean("invisible");
invisibleUntilTasks = nbt.getInt("invisible_until_tasks");
}

@Override
Expand All @@ -254,6 +266,8 @@ public void writeNetData(FriendlyByteBuf buffer) {
//flags = Bits.setFlag(flags, 128, hideTextUntilComplete);
flags = Bits.setFlag(flags, 256, optional);
flags = Bits.setFlag(flags, 512, minWidth > 0);
flags = Bits.setFlag(flags, 1024, invisible);
flags = Bits.setFlag(flags, 2048, invisibleUntilTasks > 0);
buffer.writeVarInt(flags);

hide.write(buffer);
Expand Down Expand Up @@ -295,6 +309,10 @@ public void writeNetData(FriendlyByteBuf buffer) {
if (minWidth > 0) {
buffer.writeVarInt(minWidth);
}

if (invisibleUntilTasks > 0) {
buffer.writeVarInt(invisibleUntilTasks);
}
}

@Override
Expand Down Expand Up @@ -338,6 +356,8 @@ public void readNetData(FriendlyByteBuf buffer) {

size = Bits.getFlag(flags, 4) ? buffer.readDouble() : 1D;
minWidth = Bits.getFlag(flags, 512) ? buffer.readVarInt() : 0;
invisible = Bits.getFlag(flags, 1024);
invisibleUntilTasks = Bits.getFlag(flags, 2048) ? buffer.readVarInt() : 0;
}

@Override
Expand Down Expand Up @@ -504,6 +524,8 @@ public void onClicked(MouseButton button, ConfigCallback callback) {
config.addEnum("disable_jei", disableJEI, v -> disableJEI = v, Tristate.NAME_MAP);
config.addBool("optional", optional, v -> optional = v, false);
config.addInt("min_width", minWidth, v -> minWidth = v, 0, 0, 3000);
config.addBool("invisible", invisible, v -> invisible = v, false);
config.addInt("invisible_until_tasks", invisibleUntilTasks, v -> invisibleUntilTasks = v, 0, 0, Integer.MAX_VALUE);
}

public boolean getHideDependencyLines() {
Expand Down Expand Up @@ -548,6 +570,22 @@ public void move(Chapter to, double x, double y) {

@Override
public boolean isVisible(TeamData data) {
if (invisible && !data.isCompleted(this)) {
return false;
}

if (invisibleUntilTasks > 0) {
int completedTasks = 0;
for (Task task : tasks) {
if (data.isCompleted(task)) {
completedTasks++;
}
}
if (invisibleUntilTasks > completedTasks) {
return false;
}
}

if (dependencies.isEmpty()) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ public void load() {
}
}
});

// for inital load, when this is not on disk
if (TeamData.GLOBAL.file != this) {
TeamData.GLOBAL.file = this;
addData(TeamData.GLOBAL, true);
}
} catch (Exception ex) {
ex.printStackTrace();
}
Expand Down
32 changes: 29 additions & 3 deletions common/src/main/java/dev/ftb/mods/ftbquests/quest/TeamData.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@
*/
public class TeamData {
public static int VERSION = 1;
public static final TeamData GLOBAL = new TeamData(new UUID(0L, 0L)) {
@Override
public boolean isGlobal() {
return true;
}
};

private static final byte BOOL_NONE = -1;
private static final byte BOOL_FALSE = 0;
private static final byte BOOL_TRUE = 1;
Expand Down Expand Up @@ -105,13 +112,18 @@ public void save() {
shouldSave = true;
}

public boolean isGlobal() {
return false;
}

@Override
public String toString() {
return name.isEmpty() ? uuid.toString() : name;
}

public long getProgress(long task) {
return taskProgress.get(task);
long globalProgress = isGlobal() ? 0L : TeamData.GLOBAL.getProgress(task);
return globalProgress == 0L ? taskProgress.get(task) : globalProgress;
}

public long getProgress(Task task) {
Expand All @@ -129,6 +141,10 @@ public boolean setStarted(long id, @Nullable Date time) {
return false;
}

if (!isGlobal()) {
TeamData.GLOBAL.setStarted(id, time);
}

if (time == null) {
if (started.remove(id) >= 0L) {
clearCachedProgress();
Expand Down Expand Up @@ -173,7 +189,7 @@ public boolean setCompleted(long id, @Nullable Date time) {
save();

if (file.isServerSide()) {
new ObjectCompletedResetMessage(uuid, id).sendToAll(((ServerQuestFile) file).server);
new ObjectCompletedResetMessage(isGlobal() ? GLOBAL.uuid : uuid, id).sendToAll(((ServerQuestFile) file).server);
}

return true;
Expand All @@ -184,7 +200,7 @@ public boolean setCompleted(long id, @Nullable Date time) {
save();

if (file.isServerSide()) {
new ObjectCompletedMessage(uuid, id).sendToAll(((ServerQuestFile) file).server);
new ObjectCompletedMessage(isGlobal() ? GLOBAL.uuid : uuid, id).sendToAll(((ServerQuestFile) file).server);
}

return true;
Expand Down Expand Up @@ -533,10 +549,16 @@ public int getRelativeProgress(QuestObject object) {
}

public boolean isStarted(QuestObject object) {
if (!isGlobal() && object instanceof Task && ((Task) object).global) {
return TeamData.GLOBAL.isStarted(object);
}
return started.containsKey(object.id);
}

public boolean isCompleted(QuestObject object) {
if (!isGlobal() && object instanceof Task && ((Task) object).global) {
return TeamData.GLOBAL.isCompleted(object);
}
return completed.containsKey(object.id);
}

Expand Down Expand Up @@ -664,6 +686,10 @@ public final void setProgress(Task task, long progress) {
return;
}

if (task.global && !isGlobal()) {
TeamData.GLOBAL.setProgress(task, progress);
}

long maxProgress = task.getMaxProgress();
progress = Math.max(0L, Math.min(progress, maxProgress));
long prevProgress = getProgress(task);
Expand Down
Loading