Skip to content

Commit

Permalink
1.8.0
Browse files Browse the repository at this point in the history
1.8.0 Major refactor
  • Loading branch information
PotatoPresident authored Apr 23, 2021
2 parents 0c0c34f + 147a50d commit 4c6f4a1
Show file tree
Hide file tree
Showing 27 changed files with 362 additions and 236 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Automatically build the project and run any configured tests for every push
# and submitted pull request. This can help catch issues that only occur on
# certain platforms or Java versions, and provides a first line of defence
# against bad commits.

name: build
on: [pull_request, push]

jobs:
build:
strategy:
matrix:
# Use these Java versions
java: [
1.8, # Minimum supported by Minecraft
11, # Current Java LTS
15 # Latest version
]
# and run on both Linux and Windows
os: [ubuntu-20.04, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- name: checkout repository
uses: actions/checkout@v2
- name: validate gradle wrapper
uses: gradle/wrapper-validation-action@v1
- name: setup jdk ${{ matrix.java }}
uses: actions/setup-java@v1
with:
java-version: ${{ matrix.java }}
- name: make gradle wrapper executable
if: ${{ runner.os != 'Windows' }}
run: chmod +x ./gradlew
- name: build
run: ./gradlew build
- name: capture build artifacts
if: ${{ runner.os == 'Linux' && matrix.java == '11' }} # Only upload artifacts built from LTS java on one OS
uses: actions/upload-artifact@v2
with:
name: Artifacts
path: build/libs/
33 changes: 14 additions & 19 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
plugins {
id 'fabric-loom' version '0.5-SNAPSHOT'
id 'fabric-loom' version '0.6-SNAPSHOT'
id 'maven-publish'
id ("com.wynprice.cursemaven") version "2.1.5"
}

sourceCompatibility = JavaVersion.VERSION_1_8
Expand All @@ -12,7 +11,8 @@ version = project.mod_version + "-" + project.minecraft_version + "+"
group = project.maven_group

repositories {
jcenter()
maven { url "https://maven.terraformersmc.com/"}
maven { url "https://maven.shedaniel.me/" }
}

dependencies {
Expand All @@ -21,27 +21,22 @@ dependencies {
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
// Fabric API
Set<String> apiModules = [
"fabric-api-base",
"fabric-key-binding-api-v1",
"fabric-lifecycle-events-v1"
]

// PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs.
// You may need to force-disable transitiveness on them.

modApi "me.shedaniel.cloth:config-2:4.7.0-unstable", {
exclude group: "net.fabricmc.fabric-api"
apiModules.forEach {
modImplementation include(fabricApi.module(it, project.fabric_version))
}
include "me.shedaniel.cloth:config-2:4.7.0-unstable"

modApi "me.sargunvohra.mcmods:autoconfig1u:3.2.0-unstable", {
exclude group: "net.fabricmc.fabric-api"
}
include "me.sargunvohra.mcmods:autoconfig1u:3.2.0-unstable"
modApi include("me.shedaniel.cloth:cloth-config-lite-fabric:1.0.+")

modImplementation "io.github.prospector:modmenu:1.14.6+build.31", {
exclude group: "net.fabricmc.fabric-api"
modImplementation "com.terraformersmc:modmenu:${project.modmenu_version}", {
exclude module: "fabric-api"
}

modRuntime "curse.maven:auth-me:3055415"
}

processResources {
Expand Down
12 changes: 6 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
org.gradle.jvmargs=-Xmx1G
# Fabric Properties
# check these on https://modmuss50.me/fabric.html
minecraft_version=1.16.2
yarn_mappings=1.16.2+build.19
loader_version=0.9.1+build.205
minecraft_version=1.16.5
yarn_mappings=1.16.5+build.5
loader_version=0.11.2
# Mod Properties
mod_version=1.7.1
mod_version=1.8.0
maven_group=us.potatoboy
archives_base_name=PetOwner
# Dependencies
# check this on https://modmuss50.me/fabric.html
fabric_version=0.18.0+build.397-1.16
cloth_api_version=config-2:4.5.6
fabric_version=0.31.0+1.16
modmenu_version=1.16.8
20 changes: 0 additions & 20 deletions src/main/java/us/potatoboy/petowner/client/ModMenuIntegration.java

This file was deleted.

184 changes: 101 additions & 83 deletions src/main/java/us/potatoboy/petowner/client/PetOwnerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,101 +4,119 @@
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.mojang.authlib.GameProfile;
import me.sargunvohra.mcmods.autoconfig1u.AutoConfig;
import me.sargunvohra.mcmods.autoconfig1u.serializer.JanksonConfigSerializer;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.event.player.UseEntityCallback;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.options.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.entity.Entity;
import net.minecraft.entity.passive.FoxEntity;
import net.minecraft.entity.passive.HorseBaseEntity;
import net.minecraft.entity.passive.TameableEntity;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import us.potatoboy.petowner.client.config.PetOwnerConfig;
import us.potatoboy.petowner.mixin.FoxTrustedAccessor;

import java.io.File;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

@Environment(EnvType.CLIENT)
public class PetOwnerClient implements ClientModInitializer {
private static final LoadingCache<UUID, Optional<String>> usernameCache = CacheBuilder
.newBuilder()
.expireAfterWrite(6, TimeUnit.HOURS)
.build(new CacheLoader<UUID, Optional<String>>() {
@Override
public Optional<String> load(UUID key) throws Exception {
CompletableFuture.runAsync(() -> {
GameProfile playerProfile = new GameProfile(key, null);
playerProfile = MinecraftClient.getInstance().getSessionService().fillProfileProperties(playerProfile, false);
usernameCache.put(key, Optional.ofNullable(playerProfile.getName()));
});

return Optional.of("Waiting...");
}
});

@Override
public void onInitializeClient() {
AutoConfig.register(PetOwnerConfig.class, JanksonConfigSerializer::new);
PetOwnerConfig config = AutoConfig.getConfigHolder(PetOwnerConfig.class).getConfig();

UseEntityCallback.EVENT.register(((playerEntity, world, hand, entity, entityHitResult) -> {
if (!config.click) return ActionResult.PASS;

if (!hand.equals(Hand.MAIN_HAND)) return ActionResult.PASS;
if (config.requireEmptyHand) {
if (!playerEntity.getMainHandStack().isEmpty()) return ActionResult.PASS;
}

for (UUID ownerId : getOwnerIds(entity))
{
if (ownerId == null) continue;
if (playerEntity.getUuid().equals(ownerId)) continue;

Optional<String> username = PetOwnerClient.getNameFromId(ownerId);

if (username.isPresent()) {
playerEntity.sendMessage(new TranslatableText("text.petowner.message.owner", username.get()), false);
} else {
playerEntity.sendMessage(new TranslatableText("text.petowner.message.error"), false);
}
}

return ActionResult.PASS;
}));
}

public static Optional<String> getNameFromId(UUID uuid) {
return usernameCache.getUnchecked(uuid);
}

public static List<UUID> getOwnerIds(Entity entity) {
if (entity instanceof TameableEntity) {
TameableEntity tameableEntity = (TameableEntity) entity;

if (tameableEntity.isTamed()) {
return Collections.singletonList(tameableEntity.getOwnerUuid());
}
}

if (entity instanceof HorseBaseEntity) {
HorseBaseEntity horseBaseEntity = (HorseBaseEntity) entity;

if (horseBaseEntity.isTame()) {
return Collections.singletonList(horseBaseEntity.getOwnerUuid());
}
}

if (entity instanceof FoxEntity) {
FoxEntity foxEntity = (FoxEntity) entity;
return ((FoxTrustedAccessor)foxEntity).getTrusedIds();
}

return new ArrayList<>();
}
public static final Logger LOGGER = LogManager.getLogger("PetOwner");

public static boolean enabled = true;
public static KeyBinding keyBinding;
public static PetOwnerConfig config;

private static final LoadingCache<UUID, Optional<String>> usernameCache = CacheBuilder
.newBuilder()
.expireAfterWrite(6, TimeUnit.HOURS)
.build(new CacheLoader<UUID, Optional<String>>() {
@Override
public Optional<String> load(UUID key) {
CompletableFuture.runAsync(() -> {
GameProfile playerProfile = new GameProfile(key, null);
playerProfile = MinecraftClient.getInstance().getSessionService().fillProfileProperties(playerProfile, false);
usernameCache.put(key, Optional.ofNullable(playerProfile.getName()));
});

return Optional.of("Waiting...");
}
});

@Override
public void onInitializeClient() {
config = PetOwnerConfig.loadConfig(new File(FabricLoader.getInstance().getConfigDir().toFile(), "petowner.json"));

keyBinding = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.petowner.hide",
InputUtil.UNKNOWN_KEY.getCode(),
"category.petowner.title"
));

ClientTickEvents.END_CLIENT_TICK.register(minecraftClient -> {
if (keyBinding.isUnbound()) return;

if (config.keybindMode == KeybindMode.HOLD) {
enabled = keyBinding.isPressed();

if (keyBinding.isPressed() || keyBinding.wasPressed()) {
if (minecraftClient.player != null && config.showKeybindMessage) {
minecraftClient.player.sendMessage(new TranslatableText(enabled ? "text.petowner.message.enabled" : "text.petowner.message.disabled"), true);
}
}
} else {
//Toggle mode
while (keyBinding.wasPressed()) {
enabled = !enabled;
if (minecraftClient.player != null && config.showKeybindMessage) {
minecraftClient.player.sendMessage(new TranslatableText(enabled ? "text.petowner.message.enabled" : "text.petowner.message.disabled"), true);
}
}
}
});
}

public static Optional<String> getNameFromId(UUID uuid) {
return usernameCache.getUnchecked(uuid);
}

public static List<UUID> getOwnerIds(Entity entity) {
if (entity instanceof TameableEntity) {
TameableEntity tameableEntity = (TameableEntity) entity;

if (tameableEntity.isTamed()) {
return Collections.singletonList(tameableEntity.getOwnerUuid());
}
}

if (entity instanceof HorseBaseEntity) {
HorseBaseEntity horseBaseEntity = (HorseBaseEntity) entity;

if (horseBaseEntity.isTame()) {
return Collections.singletonList(horseBaseEntity.getOwnerUuid());
}
}

if (entity instanceof FoxEntity) {
FoxEntity foxEntity = (FoxEntity) entity;
return ((FoxTrustedAccessor) foxEntity).getTrustedIds();
}

return new ArrayList<>();
}

public static void saveConfig () {
config.saveConfig(new File(FabricLoader.getInstance().getConfigDir().toFile(), "petowner.json"));
}

public enum KeybindMode {
TOGGLE,
HOLD
}
}
17 changes: 0 additions & 17 deletions src/main/java/us/potatoboy/petowner/client/PetOwnerConfig.java

This file was deleted.

Loading

0 comments on commit 4c6f4a1

Please sign in to comment.