Update the way flight works, add night vision enchantment

This commit is contained in:
zontreck 2024-01-12 00:31:06 -07:00
parent b0cb340f30
commit 99503453c2
13 changed files with 340 additions and 96 deletions

View file

@ -16,6 +16,7 @@ import dev.zontreck.libzontreck.profiles.Profile;
import dev.zontreck.libzontreck.profiles.UserProfileNotYetExistsException;
import dev.zontreck.libzontreck.util.ChatHelpers;
import dev.zontreck.libzontreck.vectors.Vector3;
import dev.zontreck.otemod.effects.ModEffects;
import dev.zontreck.otemod.implementation.CreativeModeTabs;
import dev.zontreck.otemod.implementation.InventoryBackup;
import dev.zontreck.otemod.implementation.Messages;
@ -133,6 +134,7 @@ public class OTEMod
ModEnchantments.register(bus);
ModEntityTypes.register(bus);
ModRecipes.register(bus);
ModEffects.register(bus);
//MenuInitializer.register(bus);

View file

@ -0,0 +1,101 @@
package dev.zontreck.otemod.effects;
import dev.zontreck.libzontreck.LibZontreck;
import dev.zontreck.libzontreck.util.ChatHelpers;
import dev.zontreck.libzontreck.util.ItemUtils;
import dev.zontreck.libzontreck.util.ServerUtilities;
import dev.zontreck.otemod.enchantments.ModEnchantments;
import dev.zontreck.otemod.implementation.Messages;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeMap;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.player.Abilities;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.fml.LogicalSide;
import org.jetbrains.annotations.Nullable;
public class FlightEffect extends MobEffect {
protected FlightEffect(MobEffectCategory pCategory, int pColor) {
super(pCategory, pColor);
}
@Override
public void applyEffectTick(LivingEntity pLivingEntity, int pAmplifier) {
super.applyEffectTick(pLivingEntity, pAmplifier);
if(LibZontreck.CURRENT_SIDE == LogicalSide.CLIENT) return;
if(pLivingEntity instanceof Player)
{
if(LibZontreck.CURRENT_SIDE == LogicalSide.SERVER)
{
ServerPlayer player = ServerUtilities.getPlayerByID(pLivingEntity.getStringUUID());
recheck(player);
}
}
}
@Override
public boolean isDurationEffectTick(int pDuration, int pAmplifier) {
return true;
}
@Override
public void applyInstantenousEffect(@Nullable Entity pSource, @Nullable Entity pIndirectSource, LivingEntity pLivingEntity, int pAmplifier, double pHealth) {
if(LibZontreck.CURRENT_SIDE == LogicalSide.CLIENT) return;
if(pLivingEntity instanceof Player)
{
ServerPlayer player = ServerUtilities.getPlayerByID(pLivingEntity.getStringUUID());
recheck(player);
}
super.applyInstantenousEffect(pSource, pIndirectSource, pLivingEntity, pAmplifier, pHealth);
}
private static void recheck(ServerPlayer sp)
{
if(sp.gameMode.isCreative())return; // Don't mess with the creative mode attributes
ItemStack feet = sp.getItemBySlot(EquipmentSlot.FEET);
boolean hasFlight = false;
if(ItemUtils.getEnchantmentLevel(ModEnchantments.FLIGHT_ENCHANTMENT.get(), feet)>0)hasFlight=true;
if(!hasFlight)
{
sp.removeEffect(ModEffects.FLIGHT.get());
}
Abilities playerAbilities = sp.getAbilities();
if(hasFlight)
{
if(playerAbilities.mayfly == false)
{
playerAbilities.mayfly=true;
sp.onUpdateAbilities();
ChatHelpers.broadcastTo(sp, ChatHelpers.macro(Messages.FLIGHT_GIVEN), sp.server);
}
}else {
if(playerAbilities.mayfly)
{
playerAbilities.mayfly=false;
playerAbilities.flying=false;
sp.onUpdateAbilities();
ChatHelpers.broadcastTo(sp, ChatHelpers.macro(Messages.FLIGHT_REMOVED), sp.server);
}
}
}
}

View file

@ -0,0 +1,23 @@
package dev.zontreck.otemod.effects;
import dev.zontreck.otemod.OTEMod;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
public class ModEffects
{
public static final DeferredRegister<MobEffect> REGISTRY = DeferredRegister.create(ForgeRegistries.MOB_EFFECTS, OTEMod.MOD_ID);
public static final RegistryObject<MobEffect> FLIGHT = REGISTRY.register("flight", ()->new FlightEffect(MobEffectCategory.BENEFICIAL, 0xFF0000FF));
public static void register(IEventBus bus)
{
REGISTRY.register(bus);
}
}

View file

@ -0,0 +1,33 @@
package dev.zontreck.otemod.enchantments;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentCategory;
public class BorrowedProtectionEnchantment extends Enchantment
{
protected BorrowedProtectionEnchantment(Rarity pRarity, EnchantmentCategory pCategory, EquipmentSlot[] pApplicableSlots) {
super(pRarity, pCategory, pApplicableSlots);
}
@Override
public boolean isCurse() {
return false;
}
@Override
public boolean isTradeable() {
return true;
}
@Override
public boolean isDiscoverable() {
return true;
}
@Override
public boolean isTreasureOnly() {
return false;
}
}

View file

@ -2,9 +2,13 @@ package dev.zontreck.otemod.enchantments;
import dev.zontreck.libzontreck.util.ItemUtils;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.configs.OTEServerConfig;
import dev.zontreck.otemod.effects.ModEffects;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.level.ServerPlayerGameMode;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Abilities;
import net.minecraft.world.entity.player.Player;
@ -14,71 +18,19 @@ import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentCategory;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.item.enchantment.SoulSpeedEnchantment;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.living.LivingEquipmentChangeEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.fml.common.Mod;
import java.util.concurrent.atomic.AtomicInteger;
@Mod.EventBusSubscriber(modid = OTEMod.MOD_ID)
public class FlightEnchantment extends Enchantment
{
@Mod.EventBusSubscriber(modid = OTEMod.MOD_ID, bus=Mod.EventBusSubscriber.Bus.FORGE)
public static class EventHandler{
@SubscribeEvent
public static void onLivingUpdate(LivingEquipmentChangeEvent ev)
{
if(ev.getEntity() instanceof Player)
{
if(ev.getEntity().level().isClientSide)return;
ServerPlayer sp = (ServerPlayer)ev.getEntity();
recheck(sp);
}
}
private static void recheck(ServerPlayer sp)
{
if(sp.gameMode.isCreative())return; // Don't mess with the creative mode attributes
ItemStack feet = sp.getItemBySlot(EquipmentSlot.FEET);
boolean hasFlight = false;
if(ItemUtils.getEnchantmentLevel(ModEnchantments.FLIGHT_ENCHANTMENT.get(), feet)>0)hasFlight=true;
Abilities playerAbilities = sp.getAbilities();
if(playerAbilities.mayfly == false)
{
if(hasFlight){
playerAbilities.mayfly=true;
sp.onUpdateAbilities();
}
}else {
if(!hasFlight){
playerAbilities.mayfly=false;
playerAbilities.flying=false;
sp.onUpdateAbilities();
}
}
}
@SubscribeEvent
public static void onGameModeChange(PlayerEvent.PlayerChangeGameModeEvent ev)
{
if(ev.getEntity().level().isClientSide)return;
recheck((ServerPlayer)ev.getEntity());
}
@SubscribeEvent
public static void onArmorBreak(LivingEquipmentChangeEvent ev)
{
}
}
public FlightEnchantment(EquipmentSlot... slots)
{
@ -120,4 +72,46 @@ public class FlightEnchantment extends Enchantment
{
return true;
}
public static AtomicInteger TICKS = new AtomicInteger(0);
@SubscribeEvent
public static void onEnchantmentTick(TickEvent.PlayerTickEvent event)
{
if(event.side == LogicalSide.CLIENT) return;
if(TICKS.getAndIncrement() >= (5*20))
{
TICKS.set(0);
if(OTEServerConfig.DEBUG.get())
{
OTEMod.LOGGER.info("> Flight Enchantment Tick <");
}
if(event.phase == TickEvent.Phase.END)
{
ServerPlayer sp = (ServerPlayer) event.player;
ItemStack feet = sp.getItemBySlot(EquipmentSlot.FEET);
boolean hasFlight = false;
if(ItemUtils.getEnchantmentLevel(ModEnchantments.FLIGHT_ENCHANTMENT.get(), feet)>0)hasFlight=true;
if(hasFlight)
{
MobEffectInstance inst = new MobEffectInstance(ModEffects.FLIGHT.get(), -1, 0, false, false, true);
event.player.addEffect(inst);
}
}
}
}
}

View file

@ -3,6 +3,7 @@ package dev.zontreck.otemod.enchantments;
import dev.zontreck.otemod.OTEMod;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentCategory;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
@ -17,6 +18,10 @@ public class ModEnchantments {
public static final RegistryObject<Enchantment> FLIGHT_ENCHANTMENT = REGISTERS.register("player_flight", ()->new FlightEnchantment(EquipmentSlot.FEET));
public static final RegistryObject<Enchantment> BORROWED_PROTECTION = REGISTERS.register("borrowed_protection", ()->new BorrowedProtectionEnchantment(Enchantment.Rarity.UNCOMMON, EnchantmentCategory.ARMOR, ARMOR_SLOTS));
public static final RegistryObject<Enchantment> NIGHT_VISION_ENCHANT = REGISTERS.register("night_vision", ()->new NightVisionEnchantment(EquipmentSlot.HEAD));
public static void register(IEventBus bus){
REGISTERS.register(bus);
}

View file

@ -0,0 +1,95 @@
package dev.zontreck.otemod.enchantments;
import dev.zontreck.libzontreck.util.ItemUtils;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.configs.OTEServerConfig;
import dev.zontreck.otemod.effects.ModEffects;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentCategory;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.fml.common.Mod;
import java.util.concurrent.atomic.AtomicInteger;
@Mod.EventBusSubscriber(modid = OTEMod.MOD_ID)
public class NightVisionEnchantment extends Enchantment
{
public NightVisionEnchantment(EquipmentSlot... slots)
{
super(Rarity.VERY_RARE, EnchantmentCategory.ARMOR_HEAD, slots);
}
@Override
public int getMaxLevel()
{
return 1;
}
@Override
public boolean isTreasureOnly(){
return true;
}
@Override
public boolean isTradeable()
{
return true;
}
// Not a bug. Flight is meant to be a permanent upgrade to a item. It is considered a curse due to unstable behavior. Flight will eat up durability and forge energy
// Flight should NOT be able to be removed via the grindstone
@Override
public boolean isCurse()
{
return false;
}
public static AtomicInteger TICKS = new AtomicInteger(0);
@SubscribeEvent
public static void onEnchantmentTick(TickEvent.PlayerTickEvent event)
{
if(event.side == LogicalSide.CLIENT) return;
if(TICKS.getAndIncrement() >= (2*20))
{
TICKS.set(0);
if(OTEServerConfig.DEBUG.get())
{
OTEMod.LOGGER.info("> NVision Enchantment Tick <");
}
if(event.phase == TickEvent.Phase.END)
{
ServerPlayer sp = (ServerPlayer) event.player;
ItemStack feet = sp.getItemBySlot(EquipmentSlot.HEAD);
boolean hasNV = false;
if(ItemUtils.getEnchantmentLevel(ModEnchantments.NIGHT_VISION_ENCHANT.get(), feet)>0)hasNV=true;
if(hasNV)
{
MobEffectInstance inst = new MobEffectInstance(MobEffects.NIGHT_VISION, 60*20, 4, false, false, true);
event.player.addEffect(inst);
}
}
}
}
}

View file

@ -11,6 +11,9 @@ public class Messages {
public static final String STARTER_FAILURE_PERMISSIONS;
public static final String STARTER_KIT_GIVEN;
public static final String FLIGHT_GIVEN;
public static final String FLIGHT_REMOVED;
static{
OTE_PREFIX = "!Dark_Gray![!Dark_Purple!OTE!Dark_Gray!] ";
@ -23,5 +26,7 @@ public class Messages {
STARTER_FAILURE_CONSOLE = OTE_PREFIX + "!Dark_Red!This command can only be executed from within the game";
STARTER_FAILURE_PERMISSIONS = OTE_PREFIX + "!Dark_Red!This command can only be executed by server operators";
STARTER_KIT_GIVEN = OTE_PREFIX + "!Dark_Purple!You have been given a starter kit. Welcome to the server.";
FLIGHT_GIVEN = OTE_PREFIX + "!Dark_Green!You start to feel lighter than a feather";
FLIGHT_REMOVED = OTE_PREFIX + "!Dark_Red!You have a sinking feeling you are no longer lighter than a feather.";
}
}

View file

@ -73,6 +73,12 @@
"enchantment.otemod.player_flight": "Flight",
"enchantment.otemod.mob_egging.desc": "Chance of mob spawn eggs to drop",
"enchantment.otemod.player_flight.desc": "Allows you to fly!",
"enchantment.otemod.borrowed_protection": "Borrowed Protection",
"enchantment.otemod.borrowed_protection.desc": "Borrows protection from any nearby player.",
"enchantment.otemod.night_vision": "Night Vision",
"effect.otemod.flight": "Flight",
"dev.zontreck.otemod.msgs.only_player": "§cOnly players are allowed to execute this command",

Binary file not shown.

After

Width:  |  Height:  |  Size: 309 B

View file

@ -5,5 +5,6 @@
},
"input": {
"item": "otemod:encased_singularity"
}
},
"time": 60
}