Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,8 @@ public boolean isMinecraftInitialized() {
public abstract String getCurrentGameVersion();

public abstract boolean checkResourcePackMetaValid(String s) throws Exception;

public Path getResourcePackDir() {
return resourcePacks.toPath();
}
}
6 changes: 3 additions & 3 deletions common/src/main/java/com/adamcalculator/dynamicpack/Mod.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import java.util.Set;

public class Mod {
public static final String VERSION_NAME_MOD = "1.0.16";
public static final String VERSION_NAME_MOD = "1.0.17";
public static final String VERSION_NAME_BRANCH = "mc1.20";
public static final String VERSION_NAME = VERSION_NAME_MOD + "-" + VERSION_NAME_BRANCH;
public static final long VERSION_BUILD = 16;
public static final long VERSION_BUILD = 17;
public static final String MOD_ID = "dynamicpack";


Expand Down Expand Up @@ -129,6 +129,6 @@ public static boolean isDebugLogs() {
}

public static boolean isDebugMessageOnWorldJoin() {
return true; // TODO: Disable
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.Renderable;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.narration.NarratableEntry;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.locale.Language;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.FormattedText;
Expand Down Expand Up @@ -60,4 +62,16 @@ public static void drawTexture(GuiGraphics context, ResourceLocation texture, in
RenderSystem.setShaderTexture(0, texture);
context.blit(texture, x, y, u, v, width, height, textureWidth, textureHeight);
}

public static void renderBackground(Screen screen, Object context, int mouseX, int mouseY, float delta) {
screen.renderBackground((GuiGraphics) context);
}

public static void drawString(Object context, Font font, Component component, int i, int i1, int i2) {
((GuiGraphics) context).drawString(font, component, i, i1, i2);
}

public static void drawCenteredString(Object context, Font font, Component title, int i, int i1, int i2) {
((GuiGraphics) context).drawCenteredString(font, title, i, i1, i2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package com.adamcalculator.dynamicpack.client;

import com.adamcalculator.dynamicpack.pack.BaseContent;
import com.adamcalculator.dynamicpack.pack.DynamicRepoRemote;
import com.adamcalculator.dynamicpack.pack.OverrideType;
import com.adamcalculator.dynamicpack.pack.Pack;
import com.adamcalculator.dynamicpack.util.Out;
import com.google.common.collect.ImmutableList;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.ContainerObjectSelectionList;
import net.minecraft.client.gui.components.Tooltip;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.narration.NarratableEntry;
import net.minecraft.network.chat.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.function.Consumer;

public class ContentsList extends ContainerObjectSelectionList<ContentsList.ContentEntry> {
private final ContentsScreen parent;
private final Pack pack;
private final Consumer<Boolean> resyncOnExit;
private final HashMap<BaseContent, OverrideType> preChangeStates = new HashMap<>();
private final List<ContentEntry> entries = new ArrayList<>();

public ContentsList(ContentsScreen parent, Minecraft minecraft, Pack pack, Consumer<Boolean> resyncOnExit) {
super(minecraft, parent.width, parent.height, 20, parent.height - 32, 40);
this.parent = parent;
this.pack = pack;
this.resyncOnExit = resyncOnExit;


for (BaseContent knownContent : ((DynamicRepoRemote) pack.getRemote()).getKnownContents()) {
preChangeStates.put(knownContent, knownContent.getOverride());
var v = new BaseContentEntry(knownContent);
entries.add(v);
this.addEntry(v);
}
}

public boolean isChanges() {
boolean t = false;

for (BaseContent knownContent : preChangeStates.keySet()) {
if (preChangeStates.get(knownContent) != knownContent.getOverride()) {
t = true;
break;
}
}
return t;
}

public void onAfterChange() {
boolean isChanges = isChanges();
resyncOnExit.accept(isChanges);
}

public void reset() {
for (BaseContent knownContent : preChangeStates.keySet()) {
OverrideType overrideType = preChangeStates.get(knownContent);
try {
knownContent.setOverrideType(overrideType);
} catch (Exception e) {
Out.error("Error while reset changes", e);
}
}


for (ContentEntry entry : entries) {
entry.refresh();
}

onAfterChange();
}

public class BaseContentEntry extends ContentEntry {
private final BaseContent content;
private final Button stateButton;

BaseContentEntry(BaseContent knownContent) {
this.content = knownContent;

this.stateButton = createStateButton();
stateButton.active = !content.isRequired();
if (!stateButton.active) {
this.stateButton.setTooltip(Tooltip.create(Component.translatable("dynamicpack.screen.pack_contents.state.tooltip_disabled")));
}
}

private Button createStateButton() {
return Button.builder(Component.translatable("dynamicpack.screen.pack_contents.state", currentState()), (button) -> {
try {
content.nextOverride();
} catch (Exception e) {
Out.error("Error while switch content override", e);
}
onAfterChange();
refresh();
}).bounds(0, 0, 140, 20).build();
}


@Override
public void refresh() {
stateButton.setMessage(Component.translatable("dynamicpack.screen.pack_contents.state", currentState()));
}

private Component currentState() {
String s = switch (content.getOverride()) {
case TRUE -> "dynamicpack.screen.pack_contents.state.true";
case FALSE -> "dynamicpack.screen.pack_contents.state.false";
case NOT_SET -> {
if (content.getWithDefaultState()) {
yield "dynamicpack.screen.pack_contents.state.default.true";
} else {
yield "dynamicpack.screen.pack_contents.state.default.false";
}
}
};
return Component.translatable(s);
}

public void render(GuiGraphics context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) {
String txt = content.getId();
String name = content.getName();
if (name != null) {
txt = name;
}
Component text = Component.literal(txt);
context.drawString(ContentsList.this.minecraft.font, text, (x - 70), y+10, 16777215, false);
this.stateButton.setX(x+entryWidth-140);
this.stateButton.setY(y);
this.stateButton.render(context, mouseX, mouseY, tickDelta);
}

public List<? extends GuiEventListener> children() {
return ImmutableList.of(this.stateButton);
}

public List<? extends NarratableEntry> narratables() {
return ImmutableList.of(this.stateButton);
}

}

public abstract static class ContentEntry extends Entry<ContentEntry> {
public abstract void refresh();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.adamcalculator.dynamicpack.client;

import com.adamcalculator.dynamicpack.DynamicPackMod;
import com.adamcalculator.dynamicpack.pack.Pack;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.Tooltip;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.NotNull;

import java.util.function.Consumer;

public class ContentsScreen extends Screen {
private final Screen parent;
private final Pack pack;
private ContentsList contentsList;
private final Consumer<Pack> onPackReSync = pack -> Compat.runAtUI(this::onClose);
private boolean syncOnExit = false;
private Button doneButton;
private Button resetButton;

protected ContentsScreen(Screen parent, Pack pack) {
super(Component.translatable("dynamicpack.screen.pack_contents.title"));
this.parent = parent;
this.pack = pack;
this.minecraft = Minecraft.getInstance();
this.pack.addDestroyListener(onPackReSync);
}

@Override
public void onClose() {
this.minecraft.setScreen(this.parent);
pack.removeDestroyListener(onPackReSync);
if (syncOnExit) {
DynamicPackMod.INSTANCE.startManuallySync();
}
}

@Override
public boolean shouldCloseOnEsc() {
return !syncOnExit;
}

@Override
public void render(@NotNull GuiGraphics context, int mouseX, int mouseY, float delta) {
Compat.renderBackground(this, context, mouseX, mouseY, delta);
contentsList.render(context, mouseX, mouseY, delta);
Compat.drawCenteredString(context, this.font, this.title, this.width / 2, 8, 16777215);

super.render(context, mouseX, mouseY, delta);
}

@Override
protected void init() {
super.init();
this.contentsList = new ContentsList(this, this.minecraft, pack, (b) -> {
syncOnExit = b;
updateDoneButton(b);
});
this.addWidget(this.contentsList);

this.addRenderableWidget(doneButton = Compat.createButton(CommonComponents.GUI_DONE, this::onClose, 150, 20, this.width / 2 - 155 + 160, this.height - 29));
this.addRenderableWidget(resetButton = Compat.createButton(Component.translatable("controls.reset"), this::reset, 150, 20, this.width / 2 - 155, this.height - 29));
resetButton.visible = false;
}

private void updateDoneButton(boolean syncOnExit) {
if (syncOnExit) {
doneButton.setMessage(Component.translatable("dynamicpack.screen.pack_contents.apply").withStyle(ChatFormatting.BOLD, ChatFormatting.GOLD));
doneButton.setTooltip(Tooltip.create(Component.translatable("dynamicpack.screen.pack_contents.apply.tooltip")));
} else {
doneButton.setMessage(CommonComponents.GUI_DONE);
doneButton.setTooltip(null);
}
resetButton.visible = syncOnExit;
}

private void reset() {
contentsList.reset();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.adamcalculator.dynamicpack.client;

import com.adamcalculator.dynamicpack.DynamicPackMod;
import com.adamcalculator.dynamicpack.pack.DynamicRepoRemote;
import com.adamcalculator.dynamicpack.pack.Pack;
import com.adamcalculator.dynamicpack.sync.SyncingTask;
import com.adamcalculator.dynamicpack.util.TranslatableException;
Expand All @@ -23,6 +24,7 @@ public class DynamicPackScreen extends Screen {
private final MutableComponent screenDescText;
private Button syncButton;
private final Consumer<Pack> destroyListener = this::setPack;
private Button contentsButton;

public DynamicPackScreen(Screen parent, Pack pack) {
super(Component.literal(pack.getName()).withStyle(ChatFormatting.BOLD));
Expand All @@ -43,13 +45,14 @@ private void setPack(Pack pack) {

@Override
public void render(@NotNull GuiGraphics context, int mouseX, int mouseY, float delta) {
renderBackground(context);
Compat.renderBackground(this, context, mouseX, mouseY, delta);
syncButton.active = !SyncingTask.isSyncing;
contentsButton.active = !SyncingTask.isSyncing;
int h = 20;
context.drawString(this.font, this.title, 20, 8, 16777215);
context.drawString(this.font, screenDescText, 20, 20 + h, 16777215);
context.drawString(this.font, Component.translatable("dynamicpack.screen.pack.remote_type", pack.getRemoteType()), 20, 36 + h, 16777215);
context.drawString(this.font, Component.translatable("dynamicpack.screen.pack.latestUpdated", pack.getLatestUpdated() < 0 ? "-" : new Date(pack.getLatestUpdated() * 1000)), 20, 52 + h, 16777215);
Compat.drawString(context, this.font, this.title, 20, 8, 16777215);
Compat.drawString(context, this.font, screenDescText, 20, 20 + h, 16777215);
Compat.drawString(context, this.font, Component.translatable("dynamicpack.screen.pack.remote_type", pack.getRemoteType()), 20, 36 + h, 16777215);
Compat.drawString(context, this.font, Component.translatable("dynamicpack.screen.pack.latestUpdated", pack.getLatestUpdated() < 0 ? "-" : new Date(pack.getLatestUpdated() * 1000)), 20, 52 + h, 16777215);

if (pack.getLatestException() != null) {
Compat.drawWrappedString(context, Component.translatable("dynamicpack.screen.pack.latestException", TranslatableException.getComponentFromException(pack.getLatestException())).getString(512), 20, 78 + h, 500, 99, 0xff2222);
Expand All @@ -74,6 +77,10 @@ protected void init() {
));

addRenderableWidget(Compat.createButton(CommonComponents.GUI_DONE, this::onClose, 150, 20, this.width / 2 + 4, this.height - 48));
addRenderableWidget(contentsButton = Compat.createButton(Component.translatable("dynamicpack.screen.pack.dynamic.contents"), () -> {
Minecraft.getInstance().setScreen(new ContentsScreen(this, pack));
}, 150, 20, this.width / 2 + 4-160, this.height - 48));
contentsButton.visible = pack.getRemote() instanceof DynamicRepoRemote;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.adamcalculator.dynamicpack.pack;

public class BaseContent {
private final DynamicRepoRemote parent;
private final String id;
private final boolean required;
private OverrideType overrideType;
private final String name;
private final boolean defaultStatus;

public BaseContent(DynamicRepoRemote parent, String id, boolean required, OverrideType overrideType, String name, boolean defaultStatus) {
this.parent = parent;
this.id = id;
this.required = required;
this.overrideType = overrideType;
this.name = name;
this.defaultStatus = defaultStatus;
}

public String getId() {
return id;
}

public boolean isRequired() {
return required;
}

public void nextOverride() throws Exception {
setOverrideType(overrideType.next());
}

public OverrideType getOverride() {
return overrideType;
}

public boolean getWithDefaultState() {
return defaultStatus;
}

public String getName() {
return name;
}

public void setOverrideType(OverrideType overrideType) throws Exception {
this.overrideType = overrideType;
parent.setContentOverride(this, overrideType);
}
}
Loading