s2c packet for current mana amount, mana display in xp bar (prototype)
This commit is contained in:
parent
5b15d7d55a
commit
314bcb4032
@ -44,6 +44,6 @@ mod_version=0.0.1
|
||||
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
|
||||
mod_group_id=com.jenny
|
||||
# The authors of the mod. This is a simple text string that is used for display purposes in the mod list.
|
||||
mod_authors=
|
||||
mod_authors=xJenny69
|
||||
# The description of the mod. This is a simple multiline text string that is used for display purposes in the mod list.
|
||||
mod_description=
|
||||
|
@ -3,6 +3,7 @@ package com.jenny.magic;
|
||||
import com.jenny.magic.blocks.blocks;
|
||||
import com.jenny.magic.blocks.conditions.conditions;
|
||||
import com.jenny.magic.blocks.entities.blockEntities;
|
||||
import com.jenny.magic.blocks.networking.networking;
|
||||
import com.jenny.magic.config.ConfigServer;
|
||||
import com.jenny.magic.entities.entities;
|
||||
import com.jenny.magic.items.items;
|
||||
@ -47,6 +48,7 @@ public class Magic {
|
||||
@SubscribeEvent
|
||||
public static void onCommonSetup(FMLCommonSetupEvent event) {
|
||||
conditions.register();
|
||||
networking.register();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,6 @@
|
||||
package com.jenny.magic.blocks.mana;
|
||||
|
||||
public class ManaClient {
|
||||
public static int mana = 100;
|
||||
public static long lastUse = 0;
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.jenny.magic.blocks.networking;
|
||||
|
||||
import com.jenny.magic.blocks.networking.packets.ManaAmountS2C;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraftforge.network.NetworkDirection;
|
||||
import net.minecraftforge.network.NetworkRegistry;
|
||||
import net.minecraftforge.network.PacketDistributor;
|
||||
import net.minecraftforge.network.simple.SimpleChannel;
|
||||
|
||||
import static com.jenny.magic.Magic.MODID;
|
||||
|
||||
public class networking {
|
||||
private static SimpleChannel INSTANCE;
|
||||
private static int packetID = 0;
|
||||
|
||||
private static int id() {
|
||||
return packetID;
|
||||
}
|
||||
|
||||
public static void register() {
|
||||
SimpleChannel net = NetworkRegistry.ChannelBuilder
|
||||
.named(new ResourceLocation(MODID, "messages"))
|
||||
.networkProtocolVersion(() -> "1.0")
|
||||
.clientAcceptedVersions(s -> true)
|
||||
.serverAcceptedVersions(s -> true)
|
||||
.simpleChannel();
|
||||
INSTANCE = net;
|
||||
|
||||
net.messageBuilder(ManaAmountS2C.class, id(), NetworkDirection.PLAY_TO_CLIENT)
|
||||
.decoder(ManaAmountS2C::new)
|
||||
.encoder(ManaAmountS2C::toBytes)
|
||||
.consumerMainThread(ManaAmountS2C::handle)
|
||||
.add();
|
||||
}
|
||||
|
||||
public static <MSG> void sendToServer(MSG message) {
|
||||
INSTANCE.sendToServer(message);
|
||||
}
|
||||
|
||||
public static <MSG> void sendToPlayer(MSG message, ServerPlayer player) {
|
||||
INSTANCE.send(PacketDistributor.PLAYER.with(() -> player), message);
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.jenny.magic.blocks.networking.packets;
|
||||
|
||||
import com.jenny.magic.blocks.mana.ManaClient;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraftforge.network.NetworkEvent;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ManaAmountS2C {
|
||||
private final int mana;
|
||||
|
||||
public ManaAmountS2C(int mana) {
|
||||
this.mana = mana;
|
||||
}
|
||||
|
||||
public ManaAmountS2C(FriendlyByteBuf buf) {
|
||||
this.mana = buf.getByte(2);
|
||||
}
|
||||
|
||||
public void toBytes(FriendlyByteBuf buf) {
|
||||
buf.writeByte(0);
|
||||
buf.writeByte(this.mana);
|
||||
}
|
||||
|
||||
public boolean handle(Supplier<NetworkEvent.Context> supplier) {
|
||||
NetworkEvent.Context context = supplier.get();
|
||||
context.enqueueWork(() -> {
|
||||
ManaClient.mana = this.mana;
|
||||
System.out.println("test187 " + this.mana);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
@ -1,9 +1,12 @@
|
||||
package com.jenny.magic.items;
|
||||
|
||||
import com.jenny.magic.blocks.networking.networking;
|
||||
import com.jenny.magic.blocks.networking.packets.ManaAmountS2C;
|
||||
import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.core.particles.ParticleOptions;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.MutableComponent;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.util.RandomSource;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
@ -53,4 +56,8 @@ abstract class BaseItem extends Item {
|
||||
INWARD,
|
||||
OUTWARD
|
||||
}
|
||||
|
||||
protected void manaUpdate(ServerPlayer player) {
|
||||
networking.sendToPlayer(new ManaAmountS2C(5), player);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
package com.jenny.magic.items;
|
||||
|
||||
import com.jenny.magic.blocks.mana.ManaClient;
|
||||
import com.jenny.magic.entities.BaseWandProjectile;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.world.InteractionResultHolder;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
@ -19,6 +21,13 @@ abstract class BaseWand extends BaseItem {
|
||||
BaseWandProjectile projectile = newProjectile(pLevel);
|
||||
projectile.shootFromRotation(pPlayer, pPlayer.getXRot(), pPlayer.getYRot(), 0.0F, 2.0F);
|
||||
pLevel.addFreshEntity(projectile);
|
||||
|
||||
if (!pLevel.isClientSide) {
|
||||
manaUpdate((ServerPlayer) pPlayer);
|
||||
} else {
|
||||
ManaClient.lastUse = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
return InteractionResultHolder.success(itemstack);
|
||||
}
|
||||
|
||||
|
@ -5,27 +5,31 @@ import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.MutableComponent;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.world.InteractionResult;
|
||||
import net.minecraft.world.InteractionResultHolder;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.TooltipFlag;
|
||||
import net.minecraft.world.item.context.UseOnContext;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.biome.Biome;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static com.jenny.magic.Magic.MODID;
|
||||
|
||||
public class TeleportScrollBiome extends BaseItem {
|
||||
private final Biome biome;
|
||||
private final ResourceKey<Biome> biome;
|
||||
private final ChatFormatting color;
|
||||
|
||||
public TeleportScrollBiome(Properties pProperties, Biome biome, ChatFormatting color) {
|
||||
public TeleportScrollBiome(Properties pProperties, ResourceKey<Biome> biome, ChatFormatting color) {
|
||||
super(pProperties);
|
||||
this.biome = biome;
|
||||
this.color = color;
|
||||
@ -48,7 +52,7 @@ public class TeleportScrollBiome extends BaseItem {
|
||||
Predicate<Holder<Biome>> predicate = new Predicate<Holder<Biome>>() {
|
||||
@Override
|
||||
public boolean test(@NotNull Holder<Biome> biomeHolder) {
|
||||
return Objects.equals(biomeHolder.get().toString(), biome.toString());
|
||||
return biomeHolder.is(biome);
|
||||
}
|
||||
};
|
||||
Pair<BlockPos, Holder<Biome>> pair = ((ServerLevel) player.level()).findClosestBiome3d(predicate, player.blockPosition(), 69000, 32, 64);
|
||||
@ -61,6 +65,14 @@ public class TeleportScrollBiome extends BaseItem {
|
||||
itemStack.shrink(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendHoverText(@NotNull ItemStack pStack, @Nullable Level pLevel, @NotNull List<Component> pTooltipComponents, @NotNull TooltipFlag pIsAdvanced) {
|
||||
String key = String.format("tooltip.%s.scroll_teleport_biome", MODID);
|
||||
MutableComponent toolTip = Component.translatable(key);
|
||||
toolTip = toolTip.append(Component.translatable("biome." + biome.location().toLanguageKey()).withStyle(color));
|
||||
pTooltipComponents.add(toolTip.withStyle(ChatFormatting.DARK_BLUE));
|
||||
}
|
||||
|
||||
protected void MessageBiomeNotFound(Player player) {
|
||||
player.sendSystemMessage(Component.translatable(String.format("message.%s.scroll_teleport_biome.not_found", MODID)));
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
package com.jenny.magic.items;
|
||||
|
||||
import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.level.biome.Biomes;
|
||||
import net.minecraftforge.eventbus.api.IEventBus;
|
||||
import net.minecraftforge.registries.DeferredRegister;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
@ -14,8 +16,8 @@ public class items {
|
||||
public static final RegistryObject<Item> SCROLL_TELEPORT = ITEMS.register("scroll_teleport", () -> new TeleportScrollPersistent(new Item.Properties().stacksTo(1)));
|
||||
public static final RegistryObject<Item> SCROLL_TELEPORT_BRITTLE = ITEMS.register("scroll_teleport_brittle", () -> new TeleportScrollConsumable(new Item.Properties().stacksTo(16)));
|
||||
public static final RegistryObject<Item> SCROLL_TELEPORT_RANDOM = ITEMS.register("scroll_teleport_random", () -> new TeleportScrollRandom(new Item.Properties().stacksTo(1)));
|
||||
public static final RegistryObject<Item> SCROLL_TELEPORT_RANDOM_BIOME = ITEMS.register("scroll_teleport_random_biome", () -> new TeleportScrollRandomBiome(new Item.Properties().stacksTo(16)));
|
||||
|
||||
//public static final RegistryObject<Item> SCROLL_TELEPORT_RANDOM_BIOME = ITEMS.register("scroll_teleport_random_biome", () -> new TeleportScrollRandomBiome(new Item.Properties().stacksTo(16)));
|
||||
public static final RegistryObject<Item> SCROLL_TELEPORT_BIOME_PLAINS = ITEMS.register("scroll_teleport_biome_plains", () -> new TeleportScrollBiome(new Item.Properties().stacksTo(16), Biomes.PLAINS, ChatFormatting.GREEN));
|
||||
|
||||
public static void register(IEventBus bus) {
|
||||
ITEMS.register(bus);
|
||||
|
61
src/main/java/com/jenny/magic/mixins/ExperienceToMana.java
Normal file
61
src/main/java/com/jenny/magic/mixins/ExperienceToMana.java
Normal file
@ -0,0 +1,61 @@
|
||||
package com.jenny.magic.mixins;
|
||||
|
||||
import com.jenny.magic.blocks.mana.ManaClient;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.Gui;
|
||||
import net.minecraft.client.gui.GuiGraphics;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.Unique;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
@Mixin(Gui.class)
|
||||
public class ExperienceToMana {
|
||||
@Shadow
|
||||
protected int screenHeight;
|
||||
@Unique
|
||||
private float experienceProgress;
|
||||
@Unique
|
||||
private int experienceLevel;
|
||||
@Unique
|
||||
private ResourceLocation guiIconsLocation = new ResourceLocation("textures/gui/icons_mana.png");
|
||||
|
||||
@Unique
|
||||
private Gui self() {
|
||||
return (Gui) (Object) this;
|
||||
}
|
||||
|
||||
@Unique
|
||||
private boolean manaMode() {
|
||||
return System.currentTimeMillis() - ManaClient.lastUse < 1000;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @author xJenny69
|
||||
* @reason display mana instead of xp bar
|
||||
*/
|
||||
@Inject(method = "renderExperienceBar", at = @At("HEAD"))
|
||||
public void renderExperienceBarHead(GuiGraphics pGuiGraphics, int pX, CallbackInfo ci) {
|
||||
if (manaMode()) {
|
||||
experienceLevel = Minecraft.getInstance().player.experienceLevel;
|
||||
experienceProgress = Minecraft.getInstance().player.experienceProgress;
|
||||
|
||||
Minecraft.getInstance().player.experienceLevel = ManaClient.mana;
|
||||
Minecraft.getInstance().player.experienceProgress = (float) ManaClient.mana / 100;
|
||||
}
|
||||
}
|
||||
|
||||
@Inject(method = "renderExperienceBar", at = @At("TAIL"))
|
||||
public void renderExperienceBarTail(GuiGraphics pGuiGraphics, int pX, CallbackInfo ci) {
|
||||
if (manaMode()) {
|
||||
int l = this.screenHeight - 32 + 3;
|
||||
pGuiGraphics.blit(guiIconsLocation, pX, l, 0, 64, 182, 5);
|
||||
Minecraft.getInstance().player.experienceLevel = experienceLevel;
|
||||
Minecraft.getInstance().player.experienceProgress = experienceProgress;
|
||||
}
|
||||
}
|
||||
}
|
@ -5,8 +5,10 @@
|
||||
"tooltip.magic.scroll_teleport.unset": "No location set",
|
||||
"tooltip.magic.scroll_teleport.set": "Teleports to: ",
|
||||
"tooltip.magic.scroll_teleport_random": "Using it a second time teleports you back",
|
||||
"tooltip.magic.scroll_teleport_random.back": "Will teleport you back to: ",
|
||||
"tooltip.magic.scroll_teleport_random.back": "Will teleport you back to ",
|
||||
"tooltip.magic.scroll_teleport_biome": "teleports you to the Biome ",
|
||||
"message.magic.scroll_teleport.set": "Location set.",
|
||||
"message.magic.scroll_teleport.dim": "You're not in the same dimension.",
|
||||
"message.magic.scroll_teleport.dist": "You're too far away"
|
||||
"message.magic.scroll_teleport.dist": "You're too far away",
|
||||
"message.magic.scroll_teleport_biome.not_found": "The biome could not be located, try again elsewhere."
|
||||
}
|
BIN
src/main/resources/assets/minecraft/textures/gui/icons_mana.png
Normal file
BIN
src/main/resources/assets/minecraft/textures/gui/icons_mana.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.4 KiB |
@ -7,6 +7,7 @@
|
||||
"mixins": [
|
||||
],
|
||||
"client": [
|
||||
"ExperienceToMana"
|
||||
],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
|
Loading…
x
Reference in New Issue
Block a user