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);
}
}
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);
}
}
}
}
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);
}
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);
}
}
}
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;
}
Aggregations