Skip to content

Commit

Permalink
1.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellivers committed Apr 21, 2024
1 parent d78d252 commit f9e07fa
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 47 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Pettable

A Minecraft Fabric mod that allows you to pet most passive mobs, and some others. Currently for 1.20.1+.
A Minecraft Fabric mod that allows you to pet most passive mobs, and some others. Current version is 1.4.1 for Minecraft 1.20.4.
22 changes: 10 additions & 12 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.20.1
yarn_mappings=1.20.1+build.10
loader_version=0.14.22
minecraft_version=1.20.4
yarn_mappings=1.20.4+build.3
loader_version=0.15.10

#Fabric api
fabric_version=0.97.0+1.20.4

# Mod Properties
mod_version = 1.4.0
maven_group = com.example
archives_base_name = pettable

# Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api
fabric_version=0.87.0+1.20.1
# Mod Properties
mod_version = 1.4.1
maven_group = com.example
archives_base_name = pettable

modmenu_version=7.1.0
modmenu_version=7.1.0
1 change: 0 additions & 1 deletion src/main/java/net/ellivers/pettable/Pettable.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import net.minecraft.registry.tag.TagKey;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package net.ellivers.pettable.client;

import net.ellivers.pettable.config.ModConfig;
import net.ellivers.pettable.mixin.InGameHudAccessor;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.hud.InGameHud;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.sound.SoundManager;
import net.minecraft.client.util.InputUtil;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.mob.*;
Expand All @@ -20,7 +20,6 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.EntityHitResult;
Expand Down Expand Up @@ -131,6 +130,8 @@ private void successfullyPet(Entity entity) {
PlayerEntity playerEntity = MinecraftClient.getInstance().player;
if (ModConfig.heal_owner && entity instanceof TameableEntity && ((TameableEntity) entity).isOwner(playerEntity)) {
pettingEffects(playerEntity.getEntityWorld(), playerEntity);
InGameHud hud = MinecraftClient.getInstance().inGameHud;
((InGameHudAccessor) hud).setHeartJumpEndTick(hud.getTicks() + 10);
}
if (shouldShowEffects(entity)) pettingEffects(entity.getEntityWorld(), entity);
playerEntity.swingHand(playerEntity.getActiveHand());
Expand Down
62 changes: 34 additions & 28 deletions src/main/java/net/ellivers/pettable/config/MidnightConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.ellivers.pettable.config;

import com.google.common.collect.Lists;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
Expand All @@ -18,12 +19,14 @@
import net.minecraft.client.gui.tab.TabManager;
import net.minecraft.client.gui.tooltip.Tooltip;
import net.minecraft.client.gui.widget.*;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.screen.ScreenTexts;
import net.minecraft.text.OrderedText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;

import java.awt.Color;
import java.lang.annotation.ElementType;
Expand All @@ -41,7 +44,7 @@
import java.util.function.Predicate;
import java.util.regex.Pattern;

/** MidnightConfig v2.5.0 by TeamMidnightDust & Motschen
/** MidnightConfig v2.5.1 by TeamMidnightDust & Motschen
* Single class config library - feel free to copy!
* Based on <a href="https://github.com/Minenash/TinyConfig">...</a>
* Credits to Minenash */
Expand All @@ -54,7 +57,7 @@ public abstract class MidnightConfig {

private static final List<EntryInfo> entries = new ArrayList<>();

protected static class EntryInfo {
public static class EntryInfo {
Field field;
Object widget;
int width;
Expand All @@ -77,12 +80,13 @@ protected static class EntryInfo {
private static final Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).excludeFieldsWithModifiers(Modifier.PRIVATE).addSerializationExclusionStrategy(new HiddenAnnotationExclusionStrategy()).setPrettyPrinting().create();

public static void init(String modid, Class<? extends MidnightConfig> config) {
path = FabricLoader.getInstance().getConfigDir().resolve(modid + ".json");
FabricLoader loader = FabricLoader.getInstance();
path = loader.getConfigDir().resolve(modid + ".json");
configClass.put(modid, config);

for (Field field : config.getFields()) {
EntryInfo info = new EntryInfo();
if ((field.isAnnotationPresent(Entry.class) || field.isAnnotationPresent(Comment.class)) && !field.isAnnotationPresent(Server.class) && !field.isAnnotationPresent(Hidden.class) && FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT)
if ((field.isAnnotationPresent(Entry.class) || field.isAnnotationPresent(Comment.class)) && !field.isAnnotationPresent(Server.class) && !field.isAnnotationPresent(Hidden.class) && loader.getEnvironmentType() == EnvType.CLIENT)
initClient(modid, field, info);
if (field.isAnnotationPresent(Comment.class)) info.centered = field.getAnnotation(Comment.class).centered();
if (field.isAnnotationPresent(Entry.class))
Expand Down Expand Up @@ -110,7 +114,7 @@ private static void initClient(String modid, Field field, EntryInfo info) {
info.id = modid;

if (e != null) {
if (!e.name().equals("")) info.name = Text.translatable(e.name());
if (!e.name().isEmpty()) info.name = Text.translatable(e.name());
if (type == int.class) textField(info, Integer::parseInt, INTEGER_ONLY, (int) e.min(), (int) e.max(), true);
else if (type == float.class) textField(info, Float::parseFloat, DECIMAL_ONLY, (float) e.min(), (float) e.max(), false);
else if (type == double.class) textField(info, Double::parseDouble, DECIMAL_ONLY, e.min(), e.max(), false);
Expand All @@ -134,7 +138,8 @@ else if (type == boolean.class) {
entries.add(info);
}
public static Tooltip getTooltip(EntryInfo info) {
return Tooltip.of(info.error != null ? info.error : I18n.hasTranslation(info.id + ".midnightconfig."+info.field.getName()+".tooltip") ? Text.translatable(info.id + ".midnightconfig."+info.field.getName()+".tooltip") : Text.empty());
String key = info.id + ".midnightconfig."+info.field.getName()+".tooltip";
return Tooltip.of(info.error != null ? info.error : I18n.hasTranslation(key) ? Text.translatable(key) : Text.empty());
}

private static void textField(EntryInfo info, Function<String,Number> f, Pattern pattern, double min, double max, boolean cast) {
Expand Down Expand Up @@ -190,7 +195,7 @@ public void writeChanges(String modid) {
if (!Files.exists(path)) Files.createFile(path);
Files.write(path, gson.toJson(getClass(modid)).getBytes());
} catch (Exception e) {
e.printStackTrace();
e.fillInStackTrace();
}
}
@Environment(EnvType.CLIENT)
Expand Down Expand Up @@ -257,8 +262,6 @@ public void updateResetButtons() {
for (ButtonEntry entry : this.list.children()) {
if (entry.buttons != null && entry.buttons.size() > 1 && entry.buttons.get(1) instanceof ButtonWidget button) {
button.active = !Objects.equals(entry.info.value.toString(), entry.info.defaultValue.toString());
if (button.active) button.setTooltip(Tooltip.of(Text.translatable("controls.reset").formatted(Formatting.RED)));
else button.setTooltip(Tooltip.of(Text.empty()));
}
}
}
Expand Down Expand Up @@ -302,7 +305,7 @@ public void init() {
Objects.requireNonNull(client).setScreen(parent);
}).dimensions(this.width / 2 + 4, this.height - 28, 150, 20).build());

this.list = new MidnightConfigListWidget(this.client, this.width, this.height, 32, this.height - 32, 25);
this.list = new MidnightConfigListWidget(this.client, this.width, this.height - 64, 32, 25);
if (this.client != null && this.client.world != null) this.list.setRenderBackground(false);
this.addSelectableChild(this.list);

Expand All @@ -313,13 +316,13 @@ public void fillList() {
for (EntryInfo info : entries) {
if (info.id.equals(modid) && (info.tab == null || info.tab == tabManager.getCurrentTab())) {
Text name = Objects.requireNonNullElseGet(info.name, () -> Text.translatable(translationPrefix + info.field.getName()));
ButtonWidget resetButton = ButtonWidget.builder(Text.translatable("controls.reset"), button -> {
TextIconButtonWidget resetButton = TextIconButtonWidget.builder(Text.translatable("controls.reset"), (button -> {
info.value = info.defaultValue;
info.tempValue = info.defaultValue.toString();
info.index = 0;
list.clear();
fillList();
}).dimensions(width - 205, 0, 40, 20).build();
}), true).texture(new Identifier("midnightlib","icon/reset"), 12, 12).dimension(40, 20).build();
resetButton.setPosition(width - 205, 0);

if (info.widget instanceof Map.Entry) {
Expand Down Expand Up @@ -365,8 +368,7 @@ public void fillList() {
})).dimensions(width - 185, 0, 20, 20).build();
try {
colorButton.setMessage(Text.literal("⬛").setStyle(Style.EMPTY.withColor(Color.decode(info.tempValue).getRGB())));
} catch (Exception ignored) {
}
} catch (Exception ignored) {}
info.colorButton = colorButton;
colorButton.active = false;
this.list.addButton(List.of(widget, resetButton, colorButton), name, info);
Expand All @@ -381,47 +383,51 @@ public void fillList() {
}
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context,mouseX,mouseY,delta);
if (client != null && client.world != null) super.renderInGameBackground(context);
this.list.render(context, mouseX, mouseY, delta);
super.render(context,mouseX,mouseY,delta);

if (tabs.size() < 2) context.drawCenteredTextWithShadow(textRenderer, title, width / 2, 15, 0xFFFFFF);
}
@Override public void renderBackground(DrawContext c, int x, int y, float d) {}
}
@Environment(EnvType.CLIENT)
public static class MidnightConfigListWidget extends ElementListWidget<ButtonEntry> {
TextRenderer textRenderer;

public MidnightConfigListWidget(MinecraftClient minecraftClient, int i, int j, int k, int l, int m) {
super(minecraftClient, i, j, k, l, m);
this.centerListVertically = false;
textRenderer = minecraftClient.textRenderer;
public MidnightConfigListWidget(MinecraftClient client, int width, int height, int y, int itemHeight) {
super(client, width, height, y, itemHeight);
}
@Override
public int getScrollbarPositionX() { return this.width -7; }

public void addButton(List<ClickableWidget> buttons, Text text, EntryInfo info) {
this.addEntry(new ButtonEntry(buttons, text, info));
}
public void clear() {
this.clearEntries();
}
public void clear() { this.clearEntries(); }
@Override
public int getRowWidth() { return 10000; }
@Override
protected void renderDecorations(DrawContext c, int mouseX, int mouseY) {
c.setShaderColor(0.25F, 0.25F, 0.25F, 1.0F);
c.drawTexture(Screen.OPTIONS_BACKGROUND_TEXTURE, this.getX(), 0, 0.0F, 0.0F, this.width, this.getY(), 32, 32);
c.drawTexture(Screen.OPTIONS_BACKGROUND_TEXTURE, this.getX(), this.getBottom(), 0.0F, 0.0F, this.width, this.height, 32, 32);
c.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
if (client == null || client.world == null) return;
c.fillGradient(RenderLayer.getGuiOverlay(), this.getX(), this.getY(), this.getRight(), this.getY() + 4, -16777216, 0, 0);
c.fillGradient(RenderLayer.getGuiOverlay(), this.getX(), this.getBottom() - 4, this.getRight(), this.getBottom(), 0, -16777216, 0);
}
}
public static class ButtonEntry extends ElementListWidget.Entry<ButtonEntry> {
private static final TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;
public final List<ClickableWidget> buttons;
private final Text text;
public final EntryInfo info;
private final List<ClickableWidget> children = new ArrayList<>();
public static final Map<ClickableWidget, Text> buttonsWithText = new HashMap<>();

private ButtonEntry(List<ClickableWidget> buttons, Text text, EntryInfo info) {
if (!buttons.isEmpty()) buttonsWithText.put(buttons.get(0),text);
this.buttons = buttons;
this.text = text;
this.info = info;
children.addAll(buttons);
}
public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) {
buttons.forEach(b -> { b.setY(y); b.render(context, mouseX, mouseY, tickDelta); });
Expand All @@ -436,8 +442,8 @@ public void render(DrawContext context, int index, int y, int x, int entryWidth,
}
}
}
public List<? extends Element> children() {return children;}
public List<? extends Selectable> selectableChildren() {return children;}
public List<? extends Element> children() {return Lists.newArrayList(buttons);}
public List<? extends Selectable> selectableChildren() {return Lists.newArrayList(buttons);}
}
public static class MidnightSliderWidget extends SliderWidget {
private final EntryInfo info; private final Entry e;
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/net/ellivers/pettable/mixin/InGameHudAccessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.ellivers.pettable.mixin;

import net.minecraft.client.gui.hud.InGameHud;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(InGameHud.class)
public interface InGameHudAccessor {
@Accessor("heartJumpEndTick")
void setHeartJumpEndTick(long value);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"schemaVersion": 1,
"id": "pettable",
"version": "1.4.0",
"version": "1.4.1",

"name": "Pettable",
"description": "Pet all friendly creatures!",
Expand All @@ -28,11 +28,12 @@
]
},
"mixins": [
"pettable.mixins.json"
],

"depends": {
"fabricloader": ">=0.14.21",
"minecraft": ">=1.20.1",
"minecraft": ">=1.20.4",
"fabric": "*"
}
}
14 changes: 14 additions & 0 deletions src/main/resources/pettable.mixins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"required": true,
"minVersion": "0.8",
"package": "net.ellivers.pettable.mixin",
"compatibilityLevel": "JAVA_8",
"mixins": [
],
"client": [
"InGameHudAccessor"
],
"injectors": {
"defaultRequire": 1
}
}

0 comments on commit f9e07fa

Please sign in to comment.