Search in sources :

Example 1 with ServerPlayerEntity

use of net.minecraft.entity.player.ServerPlayerEntity in project Overloaded by CJ-MC-Mods.

the class ItemRailGun method handleFireMessage.

public void handleFireMessage(@Nonnull ServerPlayerEntity player, @Nonnull RailGunFireMessage message) {
    ItemStack itemStack = player.getItemInHand(message.hand);
    if (itemStack.getItem() != this) {
        return;
    }
    LazyOptional<IEnergyStorage> opEnergy = itemStack.getCapability(ENERGY);
    if (!opEnergy.isPresent()) {
        Overloaded.logger.warn("RailGun has no Energy Capability? NBT: " + itemStack.getTag());
        return;
    }
    IEnergyStorage energy = opEnergy.orElseThrow(() -> new RuntimeException("Impossible Condition"));
    LazyOptional<IGenericDataStorage> opSettingCapability = itemStack.getCapability(GENERIC_DATA_STORAGE);
    if (!opSettingCapability.isPresent()) {
        Overloaded.logger.warn("RailGun has no GenericData Capability? NBT: " + itemStack.getTag());
        return;
    }
    IGenericDataStorage settingCapability = opSettingCapability.orElseThrow(() -> new RuntimeException("Impossible Condition"));
    settingCapability.suggestUpdate();
    int energyRequired = settingCapability.getIntegerMap().getOrDefault(RAILGUN_POWER_KEY, OverloadedConfig.INSTANCE.railGun.minEnergy);
    if (energy.getEnergyStored() < energyRequired) {
        player.displayClientMessage(new StringTextComponent("Not enough power to fire."), true);
        return;
    }
    int energyExtracted = energy.extractEnergy(energyRequired, false);
    @Nullable Entity entity = player.level.getEntity(message.id);
    if (entity == null || !entity.isAlive()) {
        return;
    } else if (player.distanceTo(entity) > OverloadedConfig.INSTANCE.rayGun.maxRange) {
        player.displayClientMessage(new StringTextComponent("Target out of range."), true);
    } else if (entity.hurt(DamageSource.playerAttack(player), (float) (OverloadedConfig.INSTANCE.railGun.damagePerRF * energyExtracted))) {
        Vector3d knockback = message.moveVector.scale(energyExtracted * OverloadedConfig.INSTANCE.railGun.knockbackPerRF);
        entity.push(knockback.x, knockback.y, knockback.z);
    }
}
Also used : IGenericDataStorage(com.cjm721.overloaded.storage.IGenericDataStorage) ClientPlayerEntity(net.minecraft.client.entity.player.ClientPlayerEntity) Entity(net.minecraft.entity.Entity) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) Vector3d(net.minecraft.util.math.vector.Vector3d) IEnergyStorage(net.minecraftforge.energy.IEnergyStorage) StringTextComponent(net.minecraft.util.text.StringTextComponent) ItemStack(net.minecraft.item.ItemStack) Nullable(javax.annotation.Nullable)

Example 2 with ServerPlayerEntity

use of net.minecraft.entity.player.ServerPlayerEntity in project Overloaded by CJ-MC-Mods.

the class InstantFurnaceContainer method broadcastChanges.

@Override
public void broadcastChanges() {
    int j;
    for (j = 0; j < this.slots.size(); ++j) {
        ItemStack itemstack = this.slots.get(j).getItem();
        itemstack = itemstack.isEmpty() ? ItemStack.EMPTY : itemstack.copy();
        this.lastSlots.set(j, itemstack);
        for (IContainerListener icontainerlistener : this.containerListeners) {
            icontainerlistener.slotChanged(this, j, itemstack);
        }
    }
    ContainerDataMessage message = new ContainerDataMessage(this.containerId);
    for (j = 0; j < this.dataSlots.size(); ++j) {
        IntReferenceHolder intreferenceholder = this.dataSlots.get(j);
        if (intreferenceholder.checkAndClearUpdateFlag()) {
            message.addData(j, intreferenceholder.get());
        }
    }
    if (!message.getData().isEmpty()) {
        for (IContainerListener listener : this.containerListeners) {
            if (listener instanceof ServerPlayerEntity) {
                Overloaded.proxy.networkWrapper.send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity) listener), message);
            }
        }
    }
}
Also used : ContainerDataMessage(com.cjm721.overloaded.network.packets.ContainerDataMessage) IntReferenceHolder(net.minecraft.util.IntReferenceHolder) IContainerListener(net.minecraft.inventory.container.IContainerListener) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) ItemStack(net.minecraft.item.ItemStack)

Example 3 with ServerPlayerEntity

use of net.minecraft.entity.player.ServerPlayerEntity in project Overloaded by CJ-MC-Mods.

the class KeyBindPressedHandler method accept.

@Override
public void accept(KeyBindPressedMessage message, Supplier<NetworkEvent.Context> ctx) {
    ServerPlayerEntity player = ctx.get().getSender();
    switch(message.getBind()) {
        case NO_CLIP:
            ctx.get().enqueueWork(() -> {
                boolean result = ArmorEventHandler.toggleNoClip(player);
                Overloaded.proxy.networkWrapper.send(PacketDistributor.PLAYER.with(() -> player), new NoClipStatusMessage(result));
            });
            break;
    }
    ctx.get().setPacketHandled(true);
}
Also used : ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) NoClipStatusMessage(com.cjm721.overloaded.network.packets.NoClipStatusMessage)

Example 4 with ServerPlayerEntity

use of net.minecraft.entity.player.ServerPlayerEntity in project AgriCraft by AgriCraft.

the class SeedAnalyzerContainer method onContainerClosed.

@Override
public void onContainerClosed(@Nonnull PlayerEntity player) {
    if (player instanceof ServerPlayerEntity) {
        ItemStack seedStack = this.seedHandler.extractItem(0, 64, false);
        if (!seedStack.isEmpty()) {
            this.seedAnalyzer.insertSeed(seedStack);
        }
        ItemStack journalStack = this.journalHandler.extractItem(0, 64, false);
        if (!journalStack.isEmpty()) {
            this.seedAnalyzer.insertJournal(journalStack);
        }
    }
}
Also used : ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) ItemStack(net.minecraft.item.ItemStack)

Example 5 with ServerPlayerEntity

use of net.minecraft.entity.player.ServerPlayerEntity in project Bookshelf by Darkhax-Minecraft.

the class InventoryUtils method keepDamageableItems.

/**
 * An extension of the IRecipe getRemainingItems method which attempts to keep items that
 * have durability. Instead of being consumed these items will attempt to have their
 * durability decreased. Things like unbreaking and unbreakable nbt settings are considered
 * depending on the input arguments.
 *
 * @param inv The inventory doing the crafting.
 * @param keptItems The list of items being kept.
 * @param ignoreUnbreaking Whether or not unbreaking enchantments should be ignored.
 * @param damageAmount The amount of damage to set on the item.
 * @return The list of items being kept.
 */
public static NonNullList<ItemStack> keepDamageableItems(CraftingInventory inv, NonNullList<ItemStack> keptItems, boolean ignoreUnbreaking, int damageAmount) {
    for (int i = 0; i < keptItems.size(); i++) {
        final ItemStack stack = inv.getItem(i);
        // Checks if the item has durability or has the unbreaking tag.
        if (stack.getItem().canBeDepleted() || stack.hasTag() && stack.getTag().getBoolean("Unbreakable")) {
            @Nullable final PlayerEntity player = InventoryUtils.getCraftingPlayer(inv);
            final Random random = player != null ? player.getRandom() : Bookshelf.RANDOM;
            final ItemStack retainedStack = stack.copy();
            // Sometimes you may want to ignore/bypass the unbreaking enchantment.
            if (ignoreUnbreaking) {
                // other item damaging mechanics.
                if (retainedStack.isDamageableItem()) {
                    retainedStack.setDamageValue(retainedStack.getDamageValue() + damageAmount);
                }
            } else {
                // Attempts to damage the item, taking things like the unbreaking
                // enchantment into consideration.
                retainedStack.hurt(damageAmount, random, player instanceof ServerPlayerEntity ? (ServerPlayerEntity) player : null);
            }
            keptItems.set(i, retainedStack);
        }
    }
    return keptItems;
}
Also used : Random(java.util.Random) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) ItemStack(net.minecraft.item.ItemStack) Nullable(javax.annotation.Nullable) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity)

Aggregations

ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)16 ItemStack (net.minecraft.item.ItemStack)8 PlayerEntity (net.minecraft.entity.player.PlayerEntity)4 MinecraftServer (net.minecraft.server.MinecraftServer)3 ResourceLocation (net.minecraft.util.ResourceLocation)3 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)3 Nullable (javax.annotation.Nullable)2 PlayerInventory (net.minecraft.entity.player.PlayerInventory)2 Container (net.minecraft.inventory.container.Container)2 BlockPos (net.minecraft.util.math.BlockPos)2 Vector3d (net.minecraft.util.math.vector.Vector3d)2 StringTextComponent (net.minecraft.util.text.StringTextComponent)2 TranslationTextComponent (net.minecraft.util.text.TranslationTextComponent)2 NetworkEvent (net.minecraftforge.fml.network.NetworkEvent)2 IGuiButtonSensitive (com.bluepowermod.client.gui.IGuiButtonSensitive)1 ContainerDataMessage (com.cjm721.overloaded.network.packets.ContainerDataMessage)1 NoClipStatusMessage (com.cjm721.overloaded.network.packets.NoClipStatusMessage)1 IGenericDataStorage (com.cjm721.overloaded.storage.IGenericDataStorage)1 JsonObject (com.google.gson.JsonObject)1 SeedAnalyzerContainer (com.infinityraider.agricraft.plugins.agrigui.analyzer.SeedAnalyzerContainer)1