Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Ecdcaeb committed Aug 1, 2023
1 parent 97df6e9 commit 6da9b4c
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 69 deletions.
18 changes: 14 additions & 4 deletions src/main/java/committee/nova/plr/elytraBombing/ElytraBombing.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package committee.nova.plr.elytraBombing;

import committee.nova.plr.elytraBombing.common.config.ConfigLoader;
import committee.nova.plr.elytraBombing.common.event.EEBEvents;
import committee.nova.plr.elytraBombing.common.event.EventLoader;
import committee.nova.plr.elytraBombing.common.proxy.CommonProxy;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;

@Mod(modid = ElytraBombing.MOD_ID, name = ElytraBombing.MOD_NAME, version = ElytraBombing.VERSION, acceptedMinecraftVersions = "[1.12,)")
Expand All @@ -13,16 +18,21 @@ public class ElytraBombing {
public static final String MOD_NAME = "Elytra Bombing";
public static final String VERSION = "1.0.0";

@SidedProxy(serverSide = "committee.nova.plr.elytraBombing.common.proxy.CommonProxy", clientSide = "committee.nova.plr.elytraBombing.client.proxy.ClientProxy")
public static CommonProxy proxy;


@EventHandler
public void init(FMLInitializationEvent event) {
proxy.init(event);

new EventLoader();
}

@EventHandler
public void preInit(FMLPreInitializationEvent event) {
proxy.preInit(event);

new ConfigLoader(event);
}
@EventHandler
public void postInit(FMLPostInitializationEvent event) {
MinecraftForge.EVENT_BUS.post(new EEBEvents.RegisterBomb());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package committee.nova.plr.elytraBombing.common.event;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;

public class EEBContext {
//not null
public EntityPlayer player;
//may be null @ACCESS_IGNITER
public ItemStack tntStack;
//not null
public ItemStack igniterStack;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package committee.nova.plr.elytraBombing.common.event;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.eventhandler.Event;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Predicate;


public class EEBEvents
{
public static HashMap<Predicate<ItemStack>, Consumer<EEBContext>> ACCESS_IGNITER=new HashMap<>();
public static List<Predicate<EntityPlayer>> ACCESS_FLYING=new LinkedList<>();
public static Map<Predicate<ItemStack>,Consumer<EEBContext>> ACCESS_TNT=new HashMap<>();

/** @Fire{@link net.minecraftforge.common.MinecraftForge.EVENT_BUS} **/

public static class RegisterBomb extends Event {
@Override
public boolean isCancelable() {
return false;
}
public void registerTNT(Predicate<ItemStack> canBeUsed,Consumer<EEBContext> useResult){
ACCESS_TNT.put(canBeUsed,useResult);
}
public void registerIgniter(Predicate<ItemStack> canBeUsed ,Consumer<EEBContext> useResult){
ACCESS_IGNITER.put(canBeUsed,useResult);
}
public void registerFlying(Predicate<EntityPlayer> returnIsFlying){
ACCESS_FLYING.add(returnIsFlying);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package committee.nova.plr.elytraBombing.common.event;

import committee.nova.plr.elytraBombing.ElytraBombing;
import committee.nova.plr.elytraBombing.common.tools.player.PlayerHandler;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

@Mod.EventBusSubscriber(modid= ElytraBombing.MOD_ID)
public class EEBHandler {
@SubscribeEvent
public static void register(EEBEvents.RegisterBomb registerBomb){
registerBomb.registerTNT(
(stack)->stack.getItem()== Item.getItemFromBlock(Blocks.TNT),
PlayerHandler::preLaunchTnt
);
registerBomb.registerFlying(EntityLivingBase::isElytraFlying);
registerBomb.registerIgniter(
(stack)-> stack.getItem()== Items.FLINT_AND_STEEL || stack.getItem()==Items.FIRE_CHARGE,
PlayerHandler::damageStack
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@

import committee.nova.plr.elytraBombing.common.tools.player.PlayerHandler;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import java.text.MessageFormat;
import java.util.function.Consumer;
import java.util.function.Predicate;

public class EventLoader {
public EventLoader() {
Expand All @@ -22,34 +18,43 @@ public EventLoader() {
@SubscribeEvent
public void onPlayerRightClickItem(PlayerInteractEvent.RightClickItem event) {
final EntityPlayer player = event.getEntityPlayer();
if (!player.isElytraFlying()) {
return;
}
final ItemStack stack = event.getItemStack();
if (stack.isEmpty()) {
return;
}
final Item igniter = stack.getItem();
if (!(igniter == Items.FLINT_AND_STEEL) && !(igniter == Items.FIRE_CHARGE)) {
return;
EEBContext context=new EEBContext();
context.player=player;
//player is not null

if (player==null)return;
if (player.isSpectator())return;

boolean flag=false;
for (Predicate<EntityPlayer> predicate:EEBEvents.ACCESS_FLYING){
if(predicate.test(player)) {
flag=true;
break;
}
}
final ItemStack tnt = PlayerHandler.searchFor(player, new ItemStack(Blocks.TNT).getItem(), 0);
if (tnt.isEmpty()) {
if (!flag)return;
final ItemStack stackIgiter = event.getItemStack();
if (stackIgiter.isEmpty()) {
return;
}
final Item tntItem = tnt.getItem();
if (player.getCooldownTracker().hasCooldown(tntItem)) {
final int cd = (int) (player.getCooldownTracker().getCooldown(tntItem, 0) * 60);
final boolean isPlural = cd > 1;
if (!player.world.isRemote) {
player.sendMessage(new TextComponentString(
MessageFormat.format(new TextComponentTranslation("msg.ebb.cd").getFormattedText(),
cd + "",
isPlural ? new TextComponentTranslation("msg.ebb.unit.plural_suffix").getFormattedText() : "")
));
for (Predicate<ItemStack> accessIngiter: EEBEvents.ACCESS_IGNITER.keySet()){
if (accessIngiter.test(stackIgiter)){
Consumer<EEBContext> consumer= EEBEvents.ACCESS_IGNITER.get(accessIngiter);
//igniter is not null
context.igniterStack=stackIgiter;
consumer.accept(context);
break;
}
return;
}
PlayerHandler.launchTnt(player, stack, tnt);

if (context.igniterStack==null) return;

final Predicate<ItemStack> tnt = PlayerHandler.searchFor(context);
if (tnt==null) return;

Consumer<EEBContext> consumer=EEBEvents.ACCESS_TNT.get(tnt);
//tnt is not null
consumer.accept(context);

}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
package committee.nova.plr.elytraBombing.common.tools.player;

import committee.nova.plr.elytraBombing.common.config.ConfigLoader;
import committee.nova.plr.elytraBombing.common.event.EEBContext;
import committee.nova.plr.elytraBombing.common.event.EEBEvents;
import net.minecraft.entity.item.EntityTNTPrimed;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;

import java.text.MessageFormat;
import java.util.List;
import java.util.function.Predicate;


public class PlayerHandler {
public static ItemStack searchFor(EntityPlayer entity, Item item, int startSlot) {
final List<ItemStack> itemList = entity.inventoryContainer.getInventory();
final int size = itemList.size();
if (startSlot < size) {
ItemStack stackToTest = itemList.get(startSlot);
return stackToTest.getItem() == item ? stackToTest : searchFor(entity, item, startSlot + 1);
} else {
return ItemStack.EMPTY;
public static Predicate<ItemStack> searchFor(EEBContext context) {
final List<ItemStack> itemList = context.player.inventoryContainer.getInventory();
for(ItemStack stack:itemList){
for(Predicate<ItemStack> predicate: EEBEvents.ACCESS_TNT.keySet()){
if (predicate.test(stack)){
context.tntStack=stack;
return predicate;
}
}
}
return null;
}
public static void damageStack(EEBContext context) {
EntityPlayer entity=context.player;
ItemStack stack=context.igniterStack;

public static void damageStack(ItemStack stack, EntityPlayer entity) {
if (!entity.isCreative()) {
if (stack.isItemStackDamageable()) {
if (entity instanceof EntityPlayerMP) {
Expand All @@ -44,7 +55,27 @@ public static void consumeStack(ItemStack stack, EntityPlayer player) {
player.inventory.clearMatchingItems(stack.getItem(), -1, 1, null);
}

public static void launchTnt(EntityPlayer player, ItemStack igniter, ItemStack tntStack) {
public static void preLaunchTnt(EEBContext context){
EntityPlayer player=context.player;
Item tntItem=context.tntStack.getItem();
if (player.getCooldownTracker().hasCooldown(tntItem) && !player.isCreative()) {
final int cd = (int) (player.getCooldownTracker().getCooldown(tntItem, 0) * 60);
final boolean isPlural = cd > 1;
if (!player.world.isRemote) {
player.sendMessage(new TextComponentString(
MessageFormat.format(new TextComponentTranslation("msg.ebb.cd").getFormattedText(),
cd + "",
isPlural ? new TextComponentTranslation("msg.ebb.unit.plural_suffix").getFormattedText() : "")
));
}
return;
}
PlayerHandler.launchTnt(context);
}

public static void launchTnt(EEBContext context) {
EntityPlayer player=context.player;
ItemStack tntStack=context.tntStack;
final World world = player.world;
final Double[] vec = (ConfigLoader.inertia) ? new Double[]{player.motionX, player.motionY, player.motionZ} : new Double[]{0D, 0D, 0D};
final Double[] pos = new Double[]{player.posX, player.posY, player.posZ};
Expand All @@ -55,8 +86,9 @@ public static void launchTnt(EntityPlayer player, ItemStack igniter, ItemStack t
world.spawnEntity(tnt);
}
world.playSound(player, tnt.posX, tnt.posY, tnt.posZ, SoundEvents.ENTITY_TNT_PRIMED, SoundCategory.BLOCKS, 1.0F, 1.0F);
damageStack(igniter, player);
consumeStack(tntStack, player);
player.getCooldownTracker().setCooldown(tntStack.getItem(), ConfigLoader.cd);
if (!player.isCreative()) {
consumeStack(tntStack, player);
player.getCooldownTracker().setCooldown(tntStack.getItem(), ConfigLoader.cd);
}
}
}

0 comments on commit 6da9b4c

Please sign in to comment.