Skip to content

Commit

Permalink
ender helmet added #15
Browse files Browse the repository at this point in the history
  • Loading branch information
cech12 committed Nov 9, 2020
1 parent 796c823 commit a0ab5d4
Show file tree
Hide file tree
Showing 17 changed files with 331 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
org.gradle.jvmargs=-Xmx3G
org.gradle.daemon=false

mod_version=1.8.5
mod_version=1.9.0
minecraft_version=1.16.2+

forge_version=1.16.2-33.0.5
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/cech12/usefulhats/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public class Config {
public static final ConfigType.Double CHOPPING_HAT_SPEED_WITH_EFFICIENCY_4 = new ConfigType.Double(1.0);
public static final ConfigType.Double CHOPPING_HAT_SPEED_WITH_EFFICIENCY_5 = new ConfigType.Double(1.5);

public static final ConfigType.Boolean ENDER_HELMET_ENABLED = new ConfigType.Boolean(true);
public static final ConfigType.Boolean ENDER_HELMET_DAMAGE_ENABLED = new ConfigType.Boolean(true);
public static final ConfigType.Integer ENDER_HELMET_DURABILITY = new ConfigType.Integer(80);
public static final ConfigType.Boolean ENDER_HELMET_INTERDIMENSIONAL_ENABLED = new ConfigType.Boolean(true);

public static final ConfigType.Boolean HALO_ENABLED = new ConfigType.Boolean(true);
public static final ConfigType.Boolean HALO_DAMAGE_ENABLED = new ConfigType.Boolean(true);
public static final ConfigType.Integer HALO_DURABILITY = new ConfigType.Integer(600);
Expand Down Expand Up @@ -119,6 +124,9 @@ public class Config {
CHOPPING_HAT_ENABLED.configObj = common
.comment("Whether or not Chopping Hat should be enabled.")
.define("choppingHatEnabled", CHOPPING_HAT_ENABLED.getDefaultValue());
ENDER_HELMET_ENABLED.configObj = common
.comment("Whether or not Ender Helmet should be enabled.")
.define("enderHelmetEnabled", ENDER_HELMET_ENABLED.getDefaultValue());
HALO_ENABLED.configObj = common
.comment("Whether or not Halo should be enabled.")
.define("haloEnabled", HALO_ENABLED.getDefaultValue());
Expand Down Expand Up @@ -207,6 +215,18 @@ public class Config {
.defineInRange("choppingHatSpeedWithEfficiency5", CHOPPING_HAT_SPEED_WITH_EFFICIENCY_5.getDefaultValue(), 0.0, 5.0);
common.pop();

common.push("Ender Helmet");
ENDER_HELMET_DAMAGE_ENABLED.configObj = common
.comment("Whether or not damaging of Ender Helmet should be enabled.")
.define("enderHelmetDamageEnabled", ENDER_HELMET_DAMAGE_ENABLED.getDefaultValue());
ENDER_HELMET_DURABILITY.configObj = common
.comment("Ender Helmet durability.")
.defineInRange("enderHelmetDurability", ENDER_HELMET_DURABILITY.getDefaultValue(), 1, 10000);
ENDER_HELMET_INTERDIMENSIONAL_ENABLED.configObj = common
.comment("Whether or not interdimensional teleporting with the Ender Helmet should be enabled.")
.define("enderHelmetInterdimensionalEnabled", ENDER_HELMET_INTERDIMENSIONAL_ENABLED.getDefaultValue());
common.pop();

common.push("Halo");
HALO_DAMAGE_ENABLED.configObj = common
.comment("Whether or not damaging of Halo should be enabled.")
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/cech12/usefulhats/init/ModItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent;
import net.minecraftforge.event.entity.player.ItemFishedEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
Expand All @@ -38,6 +39,7 @@ public class ModItems {
new AquanautHelmetItem(),
new BunnyEarsItem(),
new ChoppingHatItem(),
new EnderHelmetItem(),
new HaloItem(),
new LuckyHatItem(),
new MiningHatItem(),
Expand Down Expand Up @@ -82,6 +84,7 @@ public static void addEventListeners() {
MinecraftForge.EVENT_BUS.addListener(ModItems::onLivingEquipmentChangeEvent);
MinecraftForge.EVENT_BUS.addListener(ModItems::onLivingSetAttackTargetEvent);
MinecraftForge.EVENT_BUS.addListener(ModItems::onLivingUseItemEvent);
MinecraftForge.EVENT_BUS.addListener(ModItems::onRightClickItemEvent);
//curios events
if (CuriosMod.isLoaded()) {
MinecraftForge.EVENT_BUS.addListener(ModItems::onCuriosEquipmentChangeEvent);
Expand Down Expand Up @@ -218,6 +221,17 @@ private static void onLivingSetAttackTargetEvent(LivingSetAttackTargetEvent even
}
}

private static void onRightClickItemEvent(PlayerInteractEvent.RightClickItem event) {
PlayerEntity player = event.getPlayer();
for (ItemStack headSlotItemStack : UsefulHatsUtils.getEquippedHatItemStacks(player)) {
for (Item item : ModItems.items) {
if (item instanceof IRightClickListener && item == headSlotItemStack.getItem()) {
((IRightClickListener) item).onRightClickItemEvent(event, headSlotItemStack);
}
}
}
}

@OnlyIn(Dist.CLIENT)
private static void onRenderGameOverlayEvent(RenderGameOverlayEvent.Pre event) {
if (!event.isCanceled() && event.getType() == RenderGameOverlayEvent.ElementType.HELMET) {
Expand Down
183 changes: 183 additions & 0 deletions src/main/java/cech12/usefulhats/item/EnderHelmetItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package cech12.usefulhats.item;

import cech12.usefulhats.UsefulHatsUtils;
import cech12.usefulhats.config.Config;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.play.server.STitlePacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.stats.Stats;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;

public class EnderHelmetItem extends AbstractHatItem implements IRightClickListener {

public static final String TELEPORT_POSITION_ID = "TeleportPosition";

public EnderHelmetItem() {
super("ender_helmet", HatArmorMaterial.ENDER, rawColorFromRGB(43, 203, 175), Config.ENDER_HELMET_ENABLED, Config.ENDER_HELMET_DAMAGE_ENABLED);
}

@Override
@OnlyIn(Dist.CLIENT)
public void addInformation(@Nonnull ItemStack stack, @Nullable World worldIn, @Nonnull List<ITextComponent> tooltip, @Nonnull ITooltipFlag flagIn) {
this.addTextLineToTooltip(tooltip, new TranslationTextComponent("item.usefulhats.ender_helmet.desc.define_teleport").mergeStyle(TextFormatting.BLUE));
if (hasPosition(stack)) {
super.addInformation(stack, worldIn, tooltip, flagIn);
BlockPos pos = getPosition(stack);
this.addTextLineToTooltip(tooltip, new TranslationTextComponent("item.usefulhats.ender_helmet.desc.teleport").mergeStyle(TextFormatting.BLUE));
this.addTextLineToTooltip(tooltip, new TranslationTextComponent("item.usefulhats.ender_helmet.desc.teleport_position", pos.getX(), pos.getY(), pos.getZ()).mergeStyle(TextFormatting.BLUE));
if (worldIn == null || !isRightDimension(worldIn, stack)) {
this.addTextLineToTooltip(tooltip, new StringTextComponent(getDimensionString(stack)).mergeStyle(TextFormatting.BLUE));
}
}
}

private static void setPosition(@Nonnull ItemStack stack, @Nonnull World world, @Nonnull PlayerEntity player) {
CompoundNBT nbt = stack.getOrCreateTag().copy();
CompoundNBT positionNBT = new CompoundNBT();
BlockPos pos = player.getPosition();
positionNBT.putInt("X", pos.getX());
positionNBT.putInt("Y", pos.getY());
positionNBT.putInt("Z", pos.getZ());
positionNBT.putString("dimKey", world.func_234923_W_().getRegistryName().toString()); //dimension registry key
positionNBT.putString("dimValue", world.func_234923_W_().func_240901_a_().toString()); //dimension name
nbt.put(TELEPORT_POSITION_ID, positionNBT);
stack.setTag(nbt);
}

private static boolean hasPosition(@Nonnull ItemStack stack) {
return stack.getOrCreateTag().contains(TELEPORT_POSITION_ID);
}

private static BlockPos getPosition(@Nonnull ItemStack stack) {
if (hasPosition(stack)) {
CompoundNBT nbt = stack.getOrCreateTag();
CompoundNBT positionNBT = nbt.getCompound(TELEPORT_POSITION_ID);
return new BlockPos(positionNBT.getInt("X"), positionNBT.getInt("Y"), positionNBT.getInt("Z"));
}
return null;
}

@OnlyIn(Dist.CLIENT)
private String getDimensionString(@Nonnull ItemStack stack) {
if (hasPosition(stack)) {
return stack.getOrCreateTag().getCompound(TELEPORT_POSITION_ID).getString("dimValue");
}
return "?";
}

private static boolean equalsWorldAndNBT(World world, CompoundNBT positionNBT) {
return world.func_234923_W_().getRegistryName().toString().equals(positionNBT.getString("dimKey"))
&& world.func_234923_W_().func_240901_a_().toString().equals(positionNBT.getString("dimValue"));
}

private ServerWorld getWorld(@Nonnull MinecraftServer server, @Nonnull ItemStack stack) {
if (hasPosition(stack)) {
CompoundNBT positionNBT = stack.getOrCreateTag().getCompound(TELEPORT_POSITION_ID);
for (ServerWorld world : server.getWorlds()) {
if (equalsWorldAndNBT(world, positionNBT)) {
return world;
}
}
}
return null;
}

private static boolean isRightDimension(@Nonnull World world, @Nonnull ItemStack stack) {
CompoundNBT nbt = stack.getOrCreateTag();
if (hasPosition(stack)) {
CompoundNBT positionNBT = nbt.getCompound(TELEPORT_POSITION_ID);
return equalsWorldAndNBT(world, positionNBT);
}
return false;
}

private static boolean canTeleportToPosition(@Nonnull World world, @Nonnull BlockPos pos) {
return !world.getBlockState(pos).isSolid()
&& !world.getBlockState(pos.up()).isSolid();
}

@Nonnull
@Override
public ActionResult<ItemStack> onItemRightClick(@Nonnull World worldIn, @Nonnull PlayerEntity playerIn, @Nonnull Hand handIn) {
ItemStack stack = playerIn.getHeldItem(handIn);
if (playerIn.isSneaking() && !stack.isEmpty()) { //shift right click
if (!worldIn.isRemote) {
//save position on item stack
setPosition(stack, worldIn, playerIn);
//inform player about saved position
((ServerPlayerEntity) playerIn).connection.sendPacket(new STitlePacket(STitlePacket.Type.ACTIONBAR, new TranslationTextComponent("item.usefulhats.ender_helmet.message.position_saved"), 10, 60, 10));
}
return ActionResult.func_233538_a_(stack, worldIn.isRemote());
}
return super.onItemRightClick(worldIn, playerIn, handIn);
}

@Override
public void onRightClickItemEvent(PlayerInteractEvent.RightClickItem event, ItemStack headSlotItemStack) {
PlayerEntity player = event.getPlayer();
if (!UsefulHatsUtils.getEquippedHatItemStacks(player).contains(headSlotItemStack)) return; //only one worn stack of this item should add its effect
World world = event.getWorld();
ItemStack usedStack = event.getItemStack();
//only do teleportation when using an ender pearl and the hat item has a stored position
if (usedStack.getItem() == Items.ENDER_PEARL && hasPosition(headSlotItemStack)) {
player.swingArm(event.getHand());

if (!world.isRemote) {
//check for correct dimension
if (Config.ENDER_HELMET_INTERDIMENSIONAL_ENABLED.getValue() || isRightDimension(world, headSlotItemStack)) {
ServerWorld destinationWorld = getWorld(player.getServer(), headSlotItemStack);
BlockPos destinationPos = getPosition(headSlotItemStack);
//check for correct position
if (destinationPos != null && destinationWorld != null && canTeleportToPosition(destinationWorld, destinationPos)) {
//set cooldown for ender pearls
player.getCooldownTracker().setCooldown(usedStack.getItem(), 20);
//teleport player
player.fallDistance = 0;
player.playSound(SoundEvents.ENTITY_ENDERMAN_TELEPORT, SoundCategory.PLAYERS, 1F, 1F);
if (player.world != destinationWorld) {
((ServerPlayerEntity) player).teleport(destinationWorld, destinationPos.getX() + 0.5, destinationPos.getY(), destinationPos.getZ() + 0.5, player.rotationYaw, player.rotationPitch);
} else {
player.setPositionAndUpdate(destinationPos.getX() + 0.5, destinationPos.getY(), destinationPos.getZ() + 0.5);
}
player.playSound(SoundEvents.ENTITY_ENDERMAN_TELEPORT, SoundCategory.PLAYERS, 1F, 1F);
//remove one ender pearl
player.addStat(Stats.ITEM_USED.get(usedStack.getItem()));
if (!player.abilities.isCreativeMode) {
usedStack.shrink(1);
}
//damage hat item
this.damageHatItemByOne(headSlotItemStack, player);
} else {
((ServerPlayerEntity) player).connection.sendPacket(new STitlePacket(STitlePacket.Type.ACTIONBAR, new TranslationTextComponent("item.usefulhats.ender_helmet.message.position_obstructed"), 10, 60, 10));
}
} else {
((ServerPlayerEntity) player).connection.sendPacket(new STitlePacket(STitlePacket.Type.ACTIONBAR, new TranslationTextComponent("item.usefulhats.ender_helmet.message.wrong_dimension"), 10, 60, 10));
}
}
//cancel other right click operations
event.setCanceled(true);
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/cech12/usefulhats/item/HatArmorMaterial.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public enum HatArmorMaterial implements IArmorMaterial {
CHOPPING("usefulhats:chopping", Config.CHOPPING_HAT_DURABILITY, 15, SoundEvents.ITEM_ARMOR_EQUIP_LEATHER, () -> {
return Ingredient.fromItems(Items.RABBIT_HIDE);
}),
ENDER("usefulhats:ender", Config.ENDER_HELMET_DURABILITY, 15, SoundEvents.ITEM_ARMOR_EQUIP_IRON, () -> {
return Ingredient.fromItems(Items.ENDER_PEARL);
}),
HALO("usefulhats:halo", Config.HALO_DURABILITY, 15, SoundEvents.ITEM_ARMOR_EQUIP_GOLD, () -> {
return Ingredient.fromItems(Items.GLOWSTONE_DUST);
}),
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/cech12/usefulhats/item/IRightClickListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cech12.usefulhats.item;

import net.minecraft.item.ItemStack;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;

public interface IRightClickListener {

void onRightClickItemEvent(PlayerInteractEvent.RightClickItem event, ItemStack headSlotItemStack);

}
8 changes: 8 additions & 0 deletions src/main/resources/assets/usefulhats/lang/de_de.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
"item.usefulhats.chopping_hat": "Holzfällermütze",
"item.usefulhats.chopping_hat.desc.chopping_speed": "Mit einer Axt Holz hacken ist §2%s%%§9 schneller. (mit Verzauberungen und Tränken kombinierbar)",

"item.usefulhats.ender_helmet": "Enderhelm",
"item.usefulhats.ender_helmet.desc.define_teleport": "Shift-Rechtsklick um eine neue Teleport-Position zu definieren.",
"item.usefulhats.ender_helmet.desc.teleport": "Benutze eine Enderperle um dich an die definierte Position zu teleportieren.",
"item.usefulhats.ender_helmet.desc.teleport_position": "Position: X: %s, Y: %s, Z: %s",
"item.usefulhats.ender_helmet.message.position_saved": "Position gespeichert.",
"item.usefulhats.ender_helmet.message.position_obstructed": "Ziel blockiert.",
"item.usefulhats.ender_helmet.message.wrong_dimension": "Falsche Dimension.",

"item.usefulhats.halo": "Heiligenschein",
"item.usefulhats.halo.desc.no_attack": "Kein Gegner greift dich an (außer Bossmonster).",
"item.usefulhats.halo.desc.beware_of_nether": "Aber hüte dich vor dem §4Nether§c.",
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/assets/usefulhats/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
"item.usefulhats.chopping_hat": "Chopping Hat",
"item.usefulhats.chopping_hat.desc.chopping_speed": "Chopping with an axe is §2%s%%§9 faster. (stackable with enchantments and potions)",

"item.usefulhats.ender_helmet": "Ender Helmet",
"item.usefulhats.ender_helmet.desc.define_teleport": "Sneak right click to define a new teleport position.",
"item.usefulhats.ender_helmet.desc.teleport": "Use an Ender Pearl to teleport to the defined position.",
"item.usefulhats.ender_helmet.desc.teleport_position": "Position: X: %s, Y: %s, Z: %s",
"item.usefulhats.ender_helmet.message.position_saved": "Position saved.",
"item.usefulhats.ender_helmet.message.position_obstructed": "Destination obstructed.",
"item.usefulhats.ender_helmet.message.wrong_dimension": "Wrong dimension.",

"item.usefulhats.halo": "Halo",
"item.usefulhats.halo.desc.no_attack": "No mob except bosses attacks you.",
"item.usefulhats.halo.desc.beware_of_nether": "But beware of the §4Nether§c.",
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/assets/usefulhats/lang/es_es.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
"item.usefulhats.chopping_hat": "Sombrero de leñador",
"item.usefulhats.chopping_hat.desc.chopping_speed": "Cortar con un hacha es §2%s%%§9 más rápido. (se puede combinar con encantos y pociones)",

"item.usefulhats.ender_helmet": "Ender Helmet",
"item.usefulhats.ender_helmet.desc.define_teleport": "Sneak right click to define a new teleport position.",
"item.usefulhats.ender_helmet.desc.teleport": "Use an Ender Pearl to teleport to the defined position.",
"item.usefulhats.ender_helmet.desc.teleport_position": "Position: X: %s, Y: %s, Z: %s",
"item.usefulhats.ender_helmet.message.position_saved": "Position saved.",
"item.usefulhats.ender_helmet.message.position_obstructed": "Destination obstructed.",
"item.usefulhats.ender_helmet.message.wrong_dimension": "Wrong dimension.",

"item.usefulhats.halo": "Aureola",
"item.usefulhats.halo.desc.no_attack": "Ningunas criaturas te atacan excepto jefes.",
"item.usefulhats.halo.desc.beware_of_nether": "Pero cuidado con el §4Nether§c.",
Expand Down
10 changes: 9 additions & 1 deletion src/main/resources/assets/usefulhats/lang/pt_br.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@

"item.usefulhats.chopping_hat": "Chapéu do Lenhador",
"item.usefulhats.chopping_hat.desc.chopping_speed": "Cortar com um machado é §2%s%%§9 mais rápido. (Somase a encantamentos e poções)",


"item.usefulhats.ender_helmet": "Ender Helmet",
"item.usefulhats.ender_helmet.desc.define_teleport": "Sneak right click to define a new teleport position.",
"item.usefulhats.ender_helmet.desc.teleport": "Use an Ender Pearl to teleport to the defined position.",
"item.usefulhats.ender_helmet.desc.teleport_position": "Position: X: %s, Y: %s, Z: %s",
"item.usefulhats.ender_helmet.message.position_saved": "Position saved.",
"item.usefulhats.ender_helmet.message.position_obstructed": "Destination obstructed.",
"item.usefulhats.ender_helmet.message.wrong_dimension": "Wrong dimension.",

"item.usefulhats.halo": "Auréola",
"item.usefulhats.halo.desc.no_attack": "Nenhuma mob ataca você (Exceto chefes).",
"item.usefulhats.halo.desc.beware_of_nether": "Mas cuidado com o §4Nether§c.",
Expand Down
Loading

0 comments on commit a0ab5d4

Please sign in to comment.