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

Vampirism web api #1220

Merged
merged 5 commits into from
Aug 9, 2023
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
25 changes: 25 additions & 0 deletions .github/workflows/update_api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Update API

on:
push:
branches:
- ${{ github.event.repository.default_branch }}
paths:
- 'src/main/resources/supporters.json'
- 'src/main/resources/default_remote_config.json'

jobs:
upload:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Upload supporters
uses: wei/curl@v1
with:
args: |
-X 'POST' 'https://api.vampirism.dev/api/v0/supporter/set' -H 'x-api-key: ${{ secrets.VAMPIRISM_API }}' -H 'Content-Type: application/json' --upload-file src/main/resources/supporters.json
- name: Upload settings
uses: wei/curl@v1
with:
args: |
-X 'POST' 'https://api.vampirism.dev/api/v1/config/set' -H 'x-api-key: ${{ secrets.VAMPIRISM_API }}' -H 'Content-Type: application/json' --upload-file src/main/resources/default_remote_config.json
11 changes: 9 additions & 2 deletions src/api/java/de/teamlapen/vampirism/api/VampirismAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import de.teamlapen.vampirism.api.entity.player.vampire.IVampirePlayer;
import de.teamlapen.vampirism.api.entity.player.vampire.IVampireVisionRegistry;
import de.teamlapen.vampirism.api.items.IExtendedBrewingRecipeRegistry;
import de.teamlapen.vampirism.api.settings.ISettingsProvider;
import de.teamlapen.vampirism.api.world.IVampirismWorld;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.player.Player;
Expand All @@ -38,6 +39,7 @@ public class VampirismAPI {
private static IActionManager actionManager;
private static IEntityActionManager entityActionManager;
private static IExtendedBrewingRecipeRegistry extendedBrewingRecipeRegistry;
private static ISettingsProvider settings;

public static ISkillManager skillManager() {
return skillManager;
Expand Down Expand Up @@ -81,6 +83,10 @@ public static IExtendedBrewingRecipeRegistry extendedBrewingRecipeRegistry() {
return extendedBrewingRecipeRegistry;
}

public static ISettingsProvider settings() {
return settings;
}

/**
* Set up the API registries
* FOR INTERNAL USAGE ONLY
Expand All @@ -89,7 +95,7 @@ public static IExtendedBrewingRecipeRegistry extendedBrewingRecipeRegistry() {
*/
@ApiStatus.Internal
public static void setUpRegistries(IFactionRegistry factionRegistryIn, ISundamageRegistry sundamageRegistryIn, IVampirismEntityRegistry entityRegistryIn, IActionManager actionManagerIn, ISkillManager skillManagerIn,
IVampireVisionRegistry vampireVisionRegistryIn, IEntityActionManager entityActionManagerIn, IExtendedBrewingRecipeRegistry extendedBrewingRecipeRegistryIn) {
IVampireVisionRegistry vampireVisionRegistryIn, IEntityActionManager entityActionManagerIn, IExtendedBrewingRecipeRegistry extendedBrewingRecipeRegistryIn, ISettingsProvider settingsIn) {
if (INIT) throw new IllegalStateException("Vampirism API can only be setup once");
factionRegistry = factionRegistryIn;
sundamageRegistry = sundamageRegistryIn;
Expand All @@ -99,6 +105,7 @@ public static void setUpRegistries(IFactionRegistry factionRegistryIn, ISundamag
vampireVisionRegistry = vampireVisionRegistryIn;
entityActionManager = entityActionManagerIn;
extendedBrewingRecipeRegistry = extendedBrewingRecipeRegistryIn;
settings = settingsIn;
INIT = true;
}

Expand All @@ -109,7 +116,7 @@ public static void setUpRegistries(IFactionRegistry factionRegistryIn, ISundamag
@SuppressWarnings("EmptyMethod")
@ApiStatus.Internal
public static void onSetupComplete() {

settings.syncSettingsCache();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package de.teamlapen.vampirism.api.settings;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

/**
* Provides access to the settings api at <a href="https://api.vampirism.dev/api/v1/">api.vampirism.dev</a>
*/
public interface ISettingsProvider {

/**
* @param key settings key
* @return true if the settings value exists and the value is {@code true}
*/
@Contract(pure = true)
boolean isSettingTrue(@NotNull String key);

/**
* @param key settings key
* @return settings value for the given key
*/
@NotNull
@Contract(pure = true)
Optional<String> getSettingsValue(@NotNull String key);

@NotNull
CompletableFuture<Optional<Collection<Supporter>>> getSupportersAsync();

/**
* updates the cache of the settings
*/
void syncSettingsCache();

}
25 changes: 25 additions & 0 deletions src/api/java/de/teamlapen/vampirism/api/settings/Supporter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package de.teamlapen.vampirism.api.settings;

import de.teamlapen.vampirism.api.VReference;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public record Supporter(@NotNull ResourceLocation faction, @NotNull String name, @NotNull String texture, int typeId, @Nullable String bookId) {

public record Old(@NotNull String name, String texture, int type, int status) {
public Supporter toNew(ResourceLocation faction) {
return new Supporter(faction, name, texture, type, null);
}
}
public record OldList(@NotNull String comment, @NotNull List<Old> vampires, @NotNull List<Old> hunters) {
public Collection<Supporter> toNew() {
return Stream.concat(vampires.stream().map(s -> s.toNew(VReference.VAMPIRE_FACTION.getID())), hunters.stream().map(s -> s.toNew(VReference.HUNTER_FACTION.getID()))).collect(Collectors.toList());
}
}
}
1 change: 1 addition & 0 deletions src/lib/java/de/teamlapen/lib/lib/util/VersionChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* Uses an update file format which is an extension to forge one's, so you can use the same file for forge, but have additional information here e.g. download link.
* Requires a versioning system, which is like Vampirism's
*/
@Deprecated
public class VersionChecker implements Runnable {
private final static Logger LOGGER = LogManager.getLogger();
private static final int MAX_HTTP_REDIRECTS = Integer.getInteger("http.maxRedirects", 20);
Expand Down
50 changes: 50 additions & 0 deletions src/lib/java/de/teamlapen/lib/util/QualifiedVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package de.teamlapen.lib.util;

import org.apache.maven.artifact.versioning.DefaultArtifactVersion;

public class QualifiedVersion extends DefaultArtifactVersion {

private final TYPE type;

public QualifiedVersion(String version) {
super(version);
String qualifier = getQualifier();
TYPE type = TYPE.RELEASE;
if (qualifier != null) {
if (qualifier.contains("alpha")) {
type = TYPE.ALPHA;
} else if (qualifier.contains("beta")) {
type = TYPE.BETA;
} else if (qualifier.contains("test")) {
type = TYPE.TEST;
} else if (qualifier.contains("build")) {
type = TYPE.DEV;
}
}
this.type = type;
}

public boolean isTestVersion() {
return type == TYPE.TEST;
}

public boolean isAlphaVersion() {
return type == TYPE.ALPHA;
}

public boolean isBetaVersion() {
return type == TYPE.BETA;
}

public boolean isReleaseVersion() {
return type == TYPE.RELEASE;
}

public boolean isDevVersion() {
return type == TYPE.DEV;
}

enum TYPE {
RELEASE, BETA, ALPHA, TEST, DEV
}
}
16 changes: 0 additions & 16 deletions src/main/java/de/teamlapen/vampirism/GeneralEventHandler.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.teamlapen.vampirism;

import de.teamlapen.lib.lib.util.UtilLib;
import de.teamlapen.lib.lib.util.VersionChecker;
import de.teamlapen.vampirism.api.general.BloodConversionRegistry;
import de.teamlapen.vampirism.config.VampirismConfig;
import de.teamlapen.vampirism.core.ModLootTables;
Expand Down Expand Up @@ -48,24 +47,9 @@ public void onAttachCapabilityWorld(@NotNull AttachCapabilitiesEvent<Level> even

@SubscribeEvent
public void onPlayerLoggedIn(PlayerEvent.@NotNull PlayerLoggedInEvent event) {
VersionChecker.VersionInfo versionInfo = VampirismMod.instance.getVersionInfo();
if (!versionInfo.isChecked()) LOGGER.warn("Version check is not finished yet");

Player player = event.getEntity();
boolean isAdminLikePlayer = !ServerLifecycleHooks.getCurrentServer().isDedicatedServer() || UtilLib.isPlayerOp(player);

if (VampirismConfig.COMMON.versionCheck.get() && versionInfo.isNewVersionAvailable()) {
if (isAdminLikePlayer || player.getRandom().nextInt(5) == 0) {
if (player.getRandom().nextInt(4) == 0) {
VersionChecker.Version newVersion = versionInfo.getNewVersion();
player.sendSystemMessage(Component.translatable("text.vampirism.outdated", versionInfo.getCurrentVersion().name, newVersion.name));
Component download = Component.translatable("text.vampirism.update_message.download").withStyle(style -> style.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, newVersion.getUrl() == null ? versionInfo.getHomePage() : newVersion.getUrl())).withUnderlined(true).applyFormat(ChatFormatting.BLUE));
Component changelog = Component.translatable("text.vampirism.update_message.changelog").withStyle(style -> style.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/vampirism changelog")).withUnderlined(true));
Component modpage = Component.translatable("text.vampirism.update_message.modpage").withStyle(style -> style.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, versionInfo.getHomePage())).withUnderlined(true).applyFormat(ChatFormatting.BLUE));
player.sendSystemMessage(Component.literal("").append(download).append(Component.literal(" ")).append(changelog).append(Component.literal(" ")).append(modpage));
}
}
}
if (isAdminLikePlayer) {
//TODO 1.19
// List<String> mods = IntegrationsNotifier.shouldNotifyAboutIntegrations();
Expand Down
40 changes: 21 additions & 19 deletions src/main/java/de/teamlapen/vampirism/REFERENCE.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,47 @@
package de.teamlapen.vampirism;

import de.teamlapen.lib.util.QualifiedVersion;
import net.minecraft.resources.ResourceLocation;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import net.minecraftforge.fml.ModList;

/**
* Class to store constants and stuff
*/
public class REFERENCE {
// Vampirism
public static final String MODID = "vampirism";
public static final String NAME = "Vampirism";
public static final String MINECRAFT_VERSION = "@MVERSION@";
public static final String FORGE_VERSION_MIN = "14.23.1.2554";
public static final String INTEGRATIONS_MODID = "vampirism_integrations";
public static final QualifiedVersion VERSION = new QualifiedVersion(ModList.get().getModContainerById(MODID).map(s -> s.getModInfo().getVersion().toString()).orElse("1.0.0"));

// changeable values
public static final int HIGHEST_VAMPIRE_LEVEL = 14;
public static final int HIGHEST_HUNTER_LEVEL = 14;
public static final int HIGHEST_VAMPIRE_LORD = 5;
public static final int HIGHEST_HUNTER_LORD = 5;
public static final String FORGE_VERSION = "@FVERSION@";
public static final String VERSION_UPDATE_FILE = "https://maxanier.de/projects/vampirism/versions.json";
public static final String SUPPORTER_FILE = "https://maxanier.de/projects/vampirism/supporters.json";
public static final String CURSEFORGE_LINK = "https://minecraft.curseforge.com/projects/vampirism-become-a-vampire";
public static final String INTEGRATIONS_MODID = "vampirism_integrations";
public static final String INTEGRATIONS_LINK = "https://minecraft.curseforge.com/projects/vampirism-integrations";
public static final String GUIDEAPI_LINK = "https://www.curseforge.com/minecraft/mc-mods/guide-api-village-and-pillage";
public static final int EYE_TYPE_COUNT = 16;
public static final int FANG_TYPE_COUNT = 7;
/**
* Check for vampire garlic damage every n ticks
* Must be higher than 1, due to implementation
*/
public final static int REFRESH_GARLIC_TICKS = 40;
public static final int REFRESH_GARLIC_TICKS = 40;
/**
* Check for vampire sun damage every n ticks
* Must be higher than 2 due to implementation
*/
public final static int REFRESH_SUNDAMAGE_TICKS = 8;
public final static ResourceLocation FACTION_PLAYER_HANDLER_KEY = new ResourceLocation(MODID, "ifactionplayerhandler");
public final static ResourceLocation VAMPIRE_PLAYER_KEY = new ResourceLocation(MODID, "vampire");
public final static ResourceLocation HUNTER_PLAYER_KEY = new ResourceLocation(MODID, "hunter");
public final static ResourceLocation EXTENDED_CREATURE_KEY = new ResourceLocation(MODID, "iextendedcreature");
public final static ResourceLocation WORLD_CAP_KEY = new ResourceLocation(MODID, "world");
public static ArtifactVersion VERSION = new DefaultArtifactVersion("0.0.0");
public static final int REFRESH_SUNDAMAGE_TICKS = 8;

// links
public static final String CURSEFORGE_LINK = "https://minecraft.curseforge.com/projects/vampirism-become-a-vampire";
public static final String GUIDEAPI_LINK = "https://www.curseforge.com/minecraft/mc-mods/guide-api-village-and-pillage";
public static final String INTEGRATIONS_LINK = "https://minecraft.curseforge.com/projects/vampirism-integrations";
public static final String SETTINGS_API = "https://api.vampirism.dev/api/v1";

// fixed values
public static final ResourceLocation FACTION_PLAYER_HANDLER_KEY = new ResourceLocation(MODID, "ifactionplayerhandler");
public static final ResourceLocation VAMPIRE_PLAYER_KEY = new ResourceLocation(MODID, "vampire");
public static final ResourceLocation HUNTER_PLAYER_KEY = new ResourceLocation(MODID, "hunter");
public static final ResourceLocation EXTENDED_CREATURE_KEY = new ResourceLocation(MODID, "iextendedcreature");
public static final ResourceLocation WORLD_CAP_KEY = new ResourceLocation(MODID, "world");
}
27 changes: 4 additions & 23 deletions src/main/java/de/teamlapen/vampirism/VampirismMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import de.teamlapen.lib.lib.network.AbstractPacketDispatcher;
import de.teamlapen.lib.lib.network.ISyncable;
import de.teamlapen.lib.lib.util.IInitListener;
import de.teamlapen.lib.lib.util.VersionChecker;
import de.teamlapen.lib.util.Color;
import de.teamlapen.lib.util.OptifineHandler;
import de.teamlapen.vampirism.api.VReference;
Expand Down Expand Up @@ -43,6 +42,7 @@
import de.teamlapen.vampirism.entity.player.vampire.VampirePlayer;
import de.teamlapen.vampirism.items.VampireRefinementItem;
import de.teamlapen.vampirism.items.crossbow.CrossbowArrowHandler;
import de.teamlapen.vampirism.misc.SettingsProvider;
import de.teamlapen.vampirism.misc.VampirismLogger;
import de.teamlapen.vampirism.modcompat.IMCHandler;
import de.teamlapen.vampirism.modcompat.terrablender.TerraBlenderCompat;
Expand All @@ -67,7 +67,6 @@
import net.minecraftforge.event.server.ServerAboutToStartEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.*;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
Expand All @@ -94,20 +93,12 @@ public class VampirismMod {
public static boolean inDataGen = false;

private final @NotNull RegistryManager registryManager = new RegistryManager();
private VersionChecker.VersionInfo versionInfo;


public VampirismMod() {
instance = this;
checkEnv();

Optional<? extends net.minecraftforge.fml.ModContainer> opt = ModList.get().getModContainerById(REFERENCE.MODID);
if (opt.isPresent()) {
REFERENCE.VERSION = opt.get().getModInfo().getVersion();
} else {
LOGGER.warn("Cannot get version from mod info");
}

IEventBus modbus = FMLJavaModLoadingContext.get().getModEventBus();

modbus.addListener(this::setup);
Expand Down Expand Up @@ -142,11 +133,6 @@ public VampirismMod() {
}
}

public VersionChecker.VersionInfo getVersionInfo() {

return versionInfo;
}

public void onAddReloadListenerEvent(@NotNull AddReloadListenerEvent event) {
SkillTreeManager.getInstance().getSkillTree().initRootSkills();//Load root skills here, so even if data pack reload fail, the root skills are available #622
event.addListener(SkillTreeManager.getInstance());
Expand Down Expand Up @@ -217,7 +203,7 @@ private void loadComplete(final @NotNull FMLLoadCompleteEvent event) {
*/
private void prepareAPI() {

VampirismAPI.setUpRegistries(new FactionRegistry(), new SundamageRegistry(), new VampirismEntityRegistry().setDefaultConvertingHandlerCreator(DefaultConvertingHandler::new), new ActionManager(), new SkillManager(), new VampireVisionRegistry(), new ActionManagerEntity(), new ExtendedBrewingRecipeRegistry());
VampirismAPI.setUpRegistries(new FactionRegistry(), new SundamageRegistry(), new VampirismEntityRegistry().setDefaultConvertingHandlerCreator(DefaultConvertingHandler::new), new ActionManager(), new SkillManager(), new VampireVisionRegistry(), new ActionManagerEntity(), new ExtendedBrewingRecipeRegistry(), new SettingsProvider(REFERENCE.SETTINGS_API));
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> proxy::setupAPIClient);

VReference.VAMPIRE_FACTION = VampirismAPI.factionRegistry()
Expand Down Expand Up @@ -277,23 +263,18 @@ private void setup(final @NotNull FMLCommonSetupEvent event) {
dispatcher.registerPackets();
onInitStep(IInitListener.Step.COMMON_SETUP, event);

if (!VampirismConfig.COMMON.versionCheck.get()) {
versionInfo = new VersionChecker.VersionInfo(REFERENCE.VERSION);
} else {
versionInfo = VersionChecker.executeVersionCheck(REFERENCE.VERSION_UPDATE_FILE, REFERENCE.VERSION, !inDev && VampirismConfig.COMMON.collectStats.get());
}

MinecraftForge.EVENT_BUS.register(new ModPlayerEventHandler());

MinecraftForge.EVENT_BUS.register(new ModEntityEventHandler());
MinecraftForge.EVENT_BUS.addListener(ModLootTables::onLootLoad);

SupporterManager.getInstance().initAsync();
SupporterManager.init();
VampireBookManager.getInstance().init();
ModEntitySelectors.registerSelectors();
event.enqueueWork(TerraBlenderCompat::registerBiomeProviderIfPresentUnsafe);
// VanillaStructureModifications.addVillageStructures(RegistryAccess.EMPTY);

TelemetryCollector.execute();
}

private void setupClient(@NotNull FMLClientSetupEvent event) {
Expand Down
Loading
Loading