Fixed issues whilst running on a dedicated server

This commit is contained in:
Sollace 2021-01-21 10:41:28 +02:00
parent f537079b1b
commit afe820b704
7 changed files with 46 additions and 17 deletions

View file

@ -51,7 +51,7 @@ public interface Toxics {
Toxic toxic = new Toxic(action, type, lowerBound, upperbound); Toxic toxic = new Toxic(action, type, lowerBound, upperbound);
ToxicHolder holder = (ToxicHolder)target; ToxicHolder holder = (ToxicHolder)target;
holder.setToxic(toxic); holder.setToxic(toxic);
holder.setFood(Objects.requireNonNull(food, target.getName().toString() + " food")); holder.setFood(Objects.requireNonNull(food, target.getTranslationKey() + " food"));
return toxic; return toxic;
} }

View file

@ -1,26 +1,20 @@
package com.minelittlepony.unicopia.mixin; package com.minelittlepony.unicopia.mixin;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import javax.annotation.Nullable;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor; import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import com.minelittlepony.unicopia.item.toxin.Toxic; import com.minelittlepony.unicopia.item.toxin.Toxic;
import com.minelittlepony.unicopia.item.toxin.ToxicHolder; import com.minelittlepony.unicopia.item.toxin.ToxicHolder;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.LivingEntity; import net.minecraft.entity.LivingEntity;
import net.minecraft.item.FoodComponent; import net.minecraft.item.FoodComponent;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import net.minecraft.world.World; import net.minecraft.world.World;
@Mixin(Item.class) @Mixin(Item.class)
@ -42,11 +36,6 @@ abstract class MixinItem implements ToxicHolder {
return toxic; return toxic;
} }
@Inject(method = "appendTooltip", at = @At("RETURN"))
private void onAppendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context, CallbackInfo into) {
toxic.ifPresent(t -> tooltip.add(t.getTooltip(stack)));
}
@Inject(method = "finishUsing", at = @At("HEAD"), cancellable = true) @Inject(method = "finishUsing", at = @At("HEAD"), cancellable = true)
private void finishUsing(ItemStack stack, World world, LivingEntity entity, CallbackInfoReturnable<ItemStack> info) { private void finishUsing(ItemStack stack, World world, LivingEntity entity, CallbackInfoReturnable<ItemStack> info) {
if (getToxic().isPresent()) { if (getToxic().isPresent()) {

View file

@ -17,8 +17,10 @@ abstract class MixinItems {
index = 1, index = 1,
argsOnly = true) argsOnly = true)
private static Item modifyItem(Item item, Identifier id, Item itemAlso) { private static Item modifyItem(Item item, Identifier id, Item itemAlso) {
if (VanillaOverrides.REGISTRY.containsId(id)) { // Registry#containsId is client-only :thonkjang:
return VanillaOverrides.REGISTRY.get(id); Item replacement = VanillaOverrides.REGISTRY.get(id);
if (replacement != null) {
return replacement;
} }
return item; return item;
} }

View file

@ -0,0 +1,24 @@
package com.minelittlepony.unicopia.mixin.client;
import java.util.List;
import javax.annotation.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import com.minelittlepony.unicopia.item.toxin.ToxicHolder;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import net.minecraft.world.World;
@Mixin(Item.class)
abstract class MixinItem implements ToxicHolder {
@Inject(method = "appendTooltip", at = @At("RETURN"))
private void onAppendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context, CallbackInfo into) {
getToxic().ifPresent(t -> tooltip.add(t.getTooltip(stack)));
}
}

View file

@ -3,9 +3,11 @@ package com.minelittlepony.unicopia.network;
import java.util.function.Function; import java.util.function.Function;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import net.fabricmc.api.EnvType;
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry; import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
import net.fabricmc.fabric.api.network.PacketContext; import net.fabricmc.fabric.api.network.PacketContext;
import net.fabricmc.fabric.api.network.ServerSidePacketRegistry; import net.fabricmc.fabric.api.network.ServerSidePacketRegistry;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.network.PacketByteBuf; import net.minecraft.network.PacketByteBuf;
@ -14,6 +16,7 @@ public interface Channel {
SPacketType<MsgPlayerAbility<?>> CLIENT_PLAYER_ABILITY = clientToServer(new Identifier("unicopia", "player_ability"), MsgPlayerAbility::new); SPacketType<MsgPlayerAbility<?>> CLIENT_PLAYER_ABILITY = clientToServer(new Identifier("unicopia", "player_ability"), MsgPlayerAbility::new);
SPacketType<MsgRequestCapabilities> CLIENT_REQUEST_CAPABILITIES = clientToServer(new Identifier("unicopia", "request_capabilities"), MsgRequestCapabilities::new); SPacketType<MsgRequestCapabilities> CLIENT_REQUEST_CAPABILITIES = clientToServer(new Identifier("unicopia", "request_capabilities"), MsgRequestCapabilities::new);
CPacketType<MsgPlayerCapabilities> SERVER_PLAYER_CAPABILITIES = serverToClient(new Identifier("unicopia", "player_capabilities"), MsgPlayerCapabilities::new); CPacketType<MsgPlayerCapabilities> SERVER_PLAYER_CAPABILITIES = serverToClient(new Identifier("unicopia", "player_capabilities"), MsgPlayerCapabilities::new);
MPacketType<MsgOtherPlayerCapabilities> SERVER_OTHER_PLAYER_CAPABILITIES = serverToClients(new Identifier("unicopia", "other_player_capabilities"), MsgOtherPlayerCapabilities::new); MPacketType<MsgOtherPlayerCapabilities> SERVER_OTHER_PLAYER_CAPABILITIES = serverToClients(new Identifier("unicopia", "other_player_capabilities"), MsgOtherPlayerCapabilities::new);
CPacketType<MsgSpawnProjectile> SERVER_SPAWN_PROJECTILE = serverToClient(new Identifier("unicopia", "projectile_entity"), MsgSpawnProjectile::new); CPacketType<MsgSpawnProjectile> SERVER_SPAWN_PROJECTILE = serverToClient(new Identifier("unicopia", "projectile_entity"), MsgSpawnProjectile::new);
@ -26,12 +29,16 @@ public interface Channel {
} }
static <T extends Packet> MPacketType<T> serverToClients(Identifier id, Function<PacketByteBuf, T> factory) { static <T extends Packet> MPacketType<T> serverToClients(Identifier id, Function<PacketByteBuf, T> factory) {
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) {
ClientSidePacketRegistry.INSTANCE.register(id, (context, buffer) -> factory.apply(buffer).handleOnMain(context)); ClientSidePacketRegistry.INSTANCE.register(id, (context, buffer) -> factory.apply(buffer).handleOnMain(context));
}
return () -> id; return () -> id;
} }
static <T extends Packet> CPacketType<T> serverToClient(Identifier id, Function<PacketByteBuf, T> factory) { static <T extends Packet> CPacketType<T> serverToClient(Identifier id, Function<PacketByteBuf, T> factory) {
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) {
ClientSidePacketRegistry.INSTANCE.register(id, (context, buffer) -> factory.apply(buffer).handleOnMain(context)); ClientSidePacketRegistry.INSTANCE.register(id, (context, buffer) -> factory.apply(buffer).handleOnMain(context));
}
return () -> id; return () -> id;
} }
@ -67,10 +74,16 @@ public interface Channel {
Identifier getId(); Identifier getId();
default void send(T packet) { default void send(T packet) {
if (FabricLoader.getInstance().getEnvironmentType() != EnvType.CLIENT) {
throw new RuntimeException("Client packet send called by the server");
}
ClientSidePacketRegistry.INSTANCE.sendToServer(getId(), packet.toBuffer()); ClientSidePacketRegistry.INSTANCE.sendToServer(getId(), packet.toBuffer());
} }
default net.minecraft.network.Packet<?> toPacket(T packet) { default net.minecraft.network.Packet<?> toPacket(T packet) {
if (FabricLoader.getInstance().getEnvironmentType() != EnvType.CLIENT) {
throw new RuntimeException("Client packet send called by the server");
}
return ClientSidePacketRegistry.INSTANCE.toPacket(getId(), packet.toBuffer()); return ClientSidePacketRegistry.INSTANCE.toPacket(getId(), packet.toBuffer());
} }
} }

View file

@ -17,7 +17,7 @@ public class MsgPlayerAbility<T extends Hit> implements Channel.Packet {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
MsgPlayerAbility(PacketByteBuf buffer) { MsgPlayerAbility(PacketByteBuf buffer) {
power = (Ability<T>) Abilities.REGISTRY.get(new Identifier(buffer.readString())); power = (Ability<T>) Abilities.REGISTRY.get(new Identifier(buffer.readString(32767)));
data = power.getSerializer().fromBuffer(buffer); data = power.getSerializer().fromBuffer(buffer);
} }

View file

@ -27,6 +27,7 @@
"client.MixinEntityRenderDispatcher", "client.MixinEntityRenderDispatcher",
"client.MixinGameRenderer", "client.MixinGameRenderer",
"client.MixinInGameHud", "client.MixinInGameHud",
"client.MixinItem",
"client.MixinItemModels", "client.MixinItemModels",
"client.MixinKeyboardInput", "client.MixinKeyboardInput",
"client.MixinLightmapTextureManager", "client.MixinLightmapTextureManager",