Some code cleanup and API changes
This commit is contained in:
@@ -10,6 +10,10 @@ import net.minecraft.world.item.ItemStack;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* @author HypherionSA
|
||||
* Helper class to create custom creative tabs from modules
|
||||
*/
|
||||
public class CraterCreativeModeTab implements Supplier<CreativeModeTab> {
|
||||
|
||||
private final ResourceLocation resourceLocation;
|
||||
@@ -69,7 +73,6 @@ public class CraterCreativeModeTab implements Supplier<CreativeModeTab> {
|
||||
public CraterCreativeModeTab build() {
|
||||
return new CraterCreativeModeTab(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -3,6 +3,10 @@ package com.hypherionmc.craterlib.api.event.client;
|
||||
import com.hypherionmc.craterlib.core.event.CraterEvent;
|
||||
import net.minecraft.client.multiplayer.ClientLevel;
|
||||
|
||||
/**
|
||||
* @author HypherionSA
|
||||
* Cross Modloader Client Tick Event.
|
||||
*/
|
||||
public class CraterClientTickEvent extends CraterEvent {
|
||||
|
||||
private final ClientLevel level;
|
||||
|
@@ -4,12 +4,12 @@ import com.hypherionmc.craterlib.core.event.CraterEvent;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.Options;
|
||||
|
||||
public class EarlyInitEvent extends CraterEvent {
|
||||
public class LateInitEvent extends CraterEvent {
|
||||
|
||||
private final Minecraft minecraft;
|
||||
private final Options options;
|
||||
|
||||
public EarlyInitEvent(Minecraft minecraft, Options options) {
|
||||
public LateInitEvent(Minecraft minecraft, Options options) {
|
||||
this.minecraft = minecraft;
|
||||
this.options = options;
|
||||
}
|
@@ -54,7 +54,7 @@ public class ClientRegistry {
|
||||
* @param blockEntityRendererFactory The renderer factory
|
||||
*/
|
||||
public static void registerBlockEntityRenderer(@NotNull BlockEntityType<? extends BlockEntity> blockEntityType, @NotNull BlockEntityRendererProvider blockEntityRendererFactory) {
|
||||
ClientPlatform.CLIENT_HELPER.registerBlockEntityRenderer(blockEntityType, blockEntityRendererFactory);
|
||||
ClientPlatform.INSTANCE.registerBlockEntityRenderer(blockEntityType, blockEntityRendererFactory);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,7 +1,6 @@
|
||||
package com.hypherionmc.craterlib.common.blockentity;
|
||||
|
||||
import com.hypherionmc.craterlib.api.blockentity.caps.CraterCapabilityHandler;
|
||||
import com.hypherionmc.craterlib.core.platform.Platform;
|
||||
import com.hypherionmc.craterlib.core.systems.fluid.CraterFluidTank;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
|
@@ -3,8 +3,8 @@ package com.hypherionmc.craterlib.common.item;
|
||||
import com.hypherionmc.craterlib.api.rendering.DyableBlock;
|
||||
import com.hypherionmc.craterlib.api.rendering.ItemDyable;
|
||||
import com.hypherionmc.craterlib.core.platform.ClientPlatform;
|
||||
import com.hypherionmc.craterlib.core.platform.Platform;
|
||||
import com.hypherionmc.craterlib.core.platform.services.Environment;
|
||||
import com.hypherionmc.craterlib.core.platform.Environment;
|
||||
import com.hypherionmc.craterlib.core.platform.ModloaderEnvironment;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.world.item.BlockItem;
|
||||
@@ -27,8 +27,8 @@ public class BlockItemDyable extends BlockItem implements ItemDyable {
|
||||
public BlockItemDyable(Block block, Properties properties) {
|
||||
super(block, properties);
|
||||
|
||||
if (Platform.LOADER.getEnvironment() == Environment.CLIENT) {
|
||||
ClientPlatform.CLIENT_HELPER.registerItemProperty(this, "color");
|
||||
if (ModloaderEnvironment.INSTANCE.getEnvironment() == Environment.CLIENT) {
|
||||
ClientPlatform.INSTANCE.registerItemProperty(this, "color");
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -62,11 +62,11 @@ public final class CraterEventBus {
|
||||
for (Method m : c.getMethods()) {
|
||||
if (isClass && Modifier.isStatic(m.getModifiers())) {
|
||||
EventMethod em = EventMethod.tryCreateFrom(new AnalyzedMethod(m, c));
|
||||
if (em != null) l.add(em);
|
||||
if ((em != null) && this.hasEventAnnotation(em)) l.add(em);
|
||||
}
|
||||
if (!isClass && !Modifier.isStatic(m.getModifiers())) {
|
||||
EventMethod em = EventMethod.tryCreateFrom(new AnalyzedMethod(m, objectOrClass));
|
||||
if (em != null) l.add(em);
|
||||
if ((em != null) && this.hasEventAnnotation(em)) l.add(em);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -76,6 +76,13 @@ public final class CraterEventBus {
|
||||
return l;
|
||||
}
|
||||
|
||||
private boolean hasEventAnnotation(EventMethod m) {
|
||||
for (Annotation a : m.annotations) {
|
||||
if (a instanceof CraterEventListener) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void registerListener(Consumer<CraterEvent> listener, Class<? extends CraterEvent> eventType) {
|
||||
this.registerListener(listener, eventType, 0);
|
||||
}
|
||||
|
@@ -1,7 +1,7 @@
|
||||
package com.hypherionmc.craterlib.core.network;
|
||||
|
||||
import com.hypherionmc.craterlib.core.platform.ClientPlatform;
|
||||
import com.hypherionmc.craterlib.core.platform.Platform;
|
||||
import com.hypherionmc.craterlib.core.platform.CommonPlatform;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
|
||||
@@ -19,7 +19,7 @@ public interface CraterNetworkHandler {
|
||||
Packet<?> toClientBound(CraterPacket<?> packet);
|
||||
|
||||
default void sendToServer(CraterPacket<?> packet) {
|
||||
ClientPlatform.CLIENT_HELPER.getClientConnection().send(this.toServerBound(packet));
|
||||
ClientPlatform.INSTANCE.getClientConnection().send(this.toServerBound(packet));
|
||||
}
|
||||
|
||||
default void sendTo(CraterPacket<?> packet, ServerPlayer player) {
|
||||
@@ -27,7 +27,7 @@ public interface CraterNetworkHandler {
|
||||
}
|
||||
|
||||
default void sendToAll(CraterPacket<?> packet) {
|
||||
Platform.COMMON_HELPER.getMCServer().getPlayerList().broadcastAll(this.toClientBound(packet));
|
||||
CommonPlatform.INSTANCE.getMCServer().getPlayerList().broadcastAll(this.toClientBound(packet));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,23 +1,39 @@
|
||||
package com.hypherionmc.craterlib.core.platform;
|
||||
|
||||
import com.hypherionmc.craterlib.CraterConstants;
|
||||
import com.hypherionmc.craterlib.core.platform.services.LibClientHelper;
|
||||
import com.hypherionmc.craterlib.common.item.BlockItemDyable;
|
||||
import com.hypherionmc.craterlib.core.systems.reg.RegistryObject;
|
||||
import com.hypherionmc.craterlib.util.ServiceUtil;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.entity.BlockEntityType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @author HypherionSA
|
||||
*/
|
||||
public class ClientPlatform {
|
||||
public interface ClientPlatform {
|
||||
|
||||
public static final LibClientHelper CLIENT_HELPER = load(LibClientHelper.class);
|
||||
public final ClientPlatform INSTANCE = ServiceUtil.load(ClientPlatform.class);
|
||||
|
||||
public static <T> T load(Class<T> clazz) {
|
||||
void registerItemProperty(@NotNull BlockItemDyable item, @NotNull String property);
|
||||
|
||||
void registerCustomRenderTypes(@NotNull Collection<RegistryObject<Block>> blocks);
|
||||
|
||||
Minecraft getClientInstance();
|
||||
|
||||
Player getClientPlayer();
|
||||
|
||||
Level getClientLevel();
|
||||
|
||||
Connection getClientConnection();
|
||||
|
||||
void registerBlockEntityRenderer(@NotNull BlockEntityType<? extends BlockEntity> blockEntityType, @NotNull BlockEntityRendererProvider blockEntityRendererFactory);
|
||||
|
||||
final T loadedService = ServiceLoader.load(clazz)
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new NullPointerException("Failed to load service for " + clazz.getName()));
|
||||
CraterConstants.LOG.debug("Loaded {} for service {}", loadedService, clazz);
|
||||
return loadedService;
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,8 @@
|
||||
package com.hypherionmc.craterlib.core.platform.services;
|
||||
package com.hypherionmc.craterlib.core.platform;
|
||||
|
||||
import com.hypherionmc.craterlib.api.blockentity.caps.CraterCapabilityHandler;
|
||||
import com.hypherionmc.craterlib.core.network.CraterNetworkHandler;
|
||||
import com.hypherionmc.craterlib.util.ServiceUtil;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
@@ -20,12 +21,22 @@ import java.util.function.Consumer;
|
||||
/**
|
||||
* @author HypherionSA
|
||||
*/
|
||||
public interface LibCommonHelper {
|
||||
public interface CommonPlatform {
|
||||
|
||||
CraterNetworkHandler createPacketHandler(String modid);
|
||||
public CommonPlatform INSTANCE = ServiceUtil.load(CommonPlatform.class);
|
||||
|
||||
default CraterNetworkHandler createPacketHandler(String modid) {
|
||||
return this.createPacketHandler(modid, true, true);
|
||||
}
|
||||
|
||||
CraterNetworkHandler createPacketHandler(String modid, boolean requiredClient, boolean requiredServer);
|
||||
|
||||
MinecraftServer getMCServer();
|
||||
|
||||
default void openMenu(ServerPlayer player, MenuProvider menuProvider) {
|
||||
this.openMenu(player, menuProvider, null);
|
||||
}
|
||||
|
||||
void openMenu(ServerPlayer player, MenuProvider menu, @Nullable Consumer<FriendlyByteBuf> initialData);
|
||||
|
||||
<T extends AbstractContainerMenu> MenuType<T> createMenuType(TriFunction<Integer, Inventory, FriendlyByteBuf, T> constructor);
|
@@ -1,8 +1,9 @@
|
||||
package com.hypherionmc.craterlib.core.platform.services;
|
||||
package com.hypherionmc.craterlib.core.platform;
|
||||
|
||||
import com.hypherionmc.craterlib.core.systems.fluid.CraterFluidTank;
|
||||
import com.hypherionmc.craterlib.core.systems.fluid.FluidHolder;
|
||||
import com.hypherionmc.craterlib.core.systems.fluid.ICraterFluidHandler;
|
||||
import com.hypherionmc.craterlib.util.ServiceUtil;
|
||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
@@ -16,7 +17,9 @@ import org.jetbrains.annotations.Nullable;
|
||||
/**
|
||||
* @author HypherionSA
|
||||
*/
|
||||
public interface LibFluidHelper {
|
||||
public interface CraterFluidHelper {
|
||||
|
||||
public CraterFluidHelper INSTANCE = ServiceUtil.load(CraterFluidHelper.class);
|
||||
|
||||
CraterFluidTank createFluidTank(int capacity);
|
||||
|
@@ -0,0 +1,18 @@
|
||||
package com.hypherionmc.craterlib.core.platform;
|
||||
|
||||
/**
|
||||
* @author HypherionSA
|
||||
*/
|
||||
public enum Environment {
|
||||
CLIENT,
|
||||
SERVER,
|
||||
UNKNOWN;
|
||||
|
||||
public boolean isClient() {
|
||||
return this == CLIENT;
|
||||
}
|
||||
|
||||
public boolean isServer() {
|
||||
return this == SERVER;
|
||||
}
|
||||
}
|
@@ -1,4 +1,6 @@
|
||||
package com.hypherionmc.craterlib.core.platform.services;
|
||||
package com.hypherionmc.craterlib.core.platform;
|
||||
|
||||
import com.hypherionmc.craterlib.util.ServiceUtil;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@@ -6,7 +8,9 @@ import java.io.File;
|
||||
* @author HypherionSA
|
||||
* Helper class to provide information about the ModLoader
|
||||
*/
|
||||
public interface ILoaderHelper {
|
||||
public interface ModloaderEnvironment {
|
||||
|
||||
public final ModloaderEnvironment INSTANCE = ServiceUtil.load(ModloaderEnvironment.class);
|
||||
|
||||
boolean isFabric();
|
||||
String getGameVersion();
|
@@ -1,29 +0,0 @@
|
||||
package com.hypherionmc.craterlib.core.platform;
|
||||
|
||||
import com.hypherionmc.craterlib.CraterConstants;
|
||||
import com.hypherionmc.craterlib.core.platform.services.ILoaderHelper;
|
||||
import com.hypherionmc.craterlib.core.platform.services.LibCommonHelper;
|
||||
import com.hypherionmc.craterlib.core.platform.services.LibFluidHelper;
|
||||
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
/**
|
||||
* @author HypherionSA
|
||||
*/
|
||||
public class Platform {
|
||||
|
||||
public static final ILoaderHelper LOADER = load(ILoaderHelper.class);
|
||||
|
||||
public static final LibCommonHelper COMMON_HELPER = load(LibCommonHelper.class);
|
||||
|
||||
public static final LibFluidHelper FLUID_HELPER = load(LibFluidHelper.class);
|
||||
|
||||
public static <T> T load(Class<T> clazz) {
|
||||
|
||||
final T loadedService = ServiceLoader.load(clazz)
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new NullPointerException("Failed to load service for " + clazz.getName()));
|
||||
CraterConstants.LOG.debug("Loaded {} for service {}", loadedService, clazz);
|
||||
return loadedService;
|
||||
}
|
||||
}
|
@@ -1,10 +0,0 @@
|
||||
package com.hypherionmc.craterlib.core.platform.services;
|
||||
|
||||
/**
|
||||
* @author HypherionSA
|
||||
*/
|
||||
public enum Environment {
|
||||
CLIENT,
|
||||
SERVER,
|
||||
UNKNOWN
|
||||
}
|
@@ -1,40 +0,0 @@
|
||||
package com.hypherionmc.craterlib.core.platform.services;
|
||||
|
||||
import com.hypherionmc.craterlib.common.item.BlockItemDyable;
|
||||
import com.hypherionmc.craterlib.core.systems.reg.RegistryObject;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.entity.BlockEntityType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @author HypherionSA
|
||||
*/
|
||||
public interface LibClientHelper {
|
||||
|
||||
void registerItemProperty(@NotNull BlockItemDyable item, @NotNull String property);
|
||||
|
||||
void registerCustomRenderTypes(
|
||||
@NotNull Collection<RegistryObject<Block>> blocks,
|
||||
@NotNull Collection<RegistryObject<Item>> items
|
||||
);
|
||||
|
||||
Minecraft getClientInstance();
|
||||
|
||||
Player getClientPlayer();
|
||||
|
||||
Level getClientLevel();
|
||||
|
||||
Connection getClientConnection();
|
||||
|
||||
void registerBlockEntityRenderer(@NotNull BlockEntityType<? extends BlockEntity> blockEntityType, @NotNull BlockEntityRendererProvider blockEntityRendererFactory);
|
||||
|
||||
}
|
@@ -1,6 +1,6 @@
|
||||
package com.hypherionmc.craterlib.mixin.events.client;
|
||||
|
||||
import com.hypherionmc.craterlib.api.event.client.EarlyInitEvent;
|
||||
import com.hypherionmc.craterlib.api.event.client.LateInitEvent;
|
||||
import com.hypherionmc.craterlib.core.event.CraterEventBus;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.Options;
|
||||
@@ -15,7 +15,7 @@ public class TutorialMixin {
|
||||
|
||||
@Inject(method = "<init>", at = @At("RETURN"))
|
||||
private void injectEarlyInitEvent(Minecraft minecraft, Options options, CallbackInfo ci) {
|
||||
EarlyInitEvent event = new EarlyInitEvent(minecraft, options);
|
||||
LateInitEvent event = new LateInitEvent(minecraft, options);
|
||||
CraterEventBus.INSTANCE.postEvent(event);
|
||||
}
|
||||
|
||||
|
@@ -25,7 +25,7 @@ public class RenderUtils {
|
||||
public static Component getFluidAmount(long amount, long capacity) {
|
||||
amount = amount / 81;
|
||||
capacity = capacity / 81;
|
||||
String text = "" + (int) (((float) amount / capacity) * 100);
|
||||
String text = String.valueOf((int) (((float) amount / capacity) * 100));
|
||||
return amount > 0 ? Component.literal(ChatFormatting.AQUA + text + "%") : Component.literal(text + "%");
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,27 @@
|
||||
package com.hypherionmc.craterlib.util;
|
||||
|
||||
import com.hypherionmc.craterlib.CraterConstants;
|
||||
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
/**
|
||||
* @author HypherionSA
|
||||
* Utility class to handle SPI loading
|
||||
*/
|
||||
public class ServiceUtil {
|
||||
|
||||
/**
|
||||
* Try to load a service
|
||||
* @param clazz The service class type to load
|
||||
* @return The loaded class
|
||||
*/
|
||||
public static <T> T load(Class<T> clazz) {
|
||||
|
||||
final T loadedService = ServiceLoader.load(clazz)
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new NullPointerException("Failed to load service for " + clazz.getName()));
|
||||
CraterConstants.LOG.debug("Loaded {} for service {}", loadedService, clazz);
|
||||
return loadedService;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user