-
-
Notifications
You must be signed in to change notification settings - Fork 504
/
ItemMekaTool.java
371 lines (340 loc) · 19 KB
/
ItemMekaTool.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package mekanism.common.item.gear;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableMultimap.Builder;
import com.google.common.collect.Multimap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import mekanism.api.Action;
import mekanism.api.energy.IEnergyContainer;
import mekanism.api.gear.ICustomModule;
import mekanism.api.gear.IModule;
import mekanism.api.inventory.AutomationType;
import mekanism.api.math.FloatingLong;
import mekanism.api.text.EnumColor;
import mekanism.client.key.MekKeyHandler;
import mekanism.client.key.MekanismKeyHandler;
import mekanism.common.Mekanism;
import mekanism.common.MekanismLang;
import mekanism.common.block.BlockBounding;
import mekanism.common.config.MekanismConfig;
import mekanism.common.content.gear.IModuleContainerItem;
import mekanism.common.content.gear.Module;
import mekanism.common.content.gear.mekatool.ModuleAttackAmplificationUnit;
import mekanism.common.content.gear.mekatool.ModuleExcavationEscalationUnit;
import mekanism.common.content.gear.mekatool.ModuleShearingUnit;
import mekanism.common.content.gear.mekatool.ModuleTeleportationUnit;
import mekanism.common.content.gear.mekatool.ModuleVeinMiningUnit;
import mekanism.common.content.gear.shared.ModuleEnergyUnit;
import mekanism.common.item.ItemEnergized;
import mekanism.common.item.interfaces.IModeItem;
import mekanism.common.network.to_client.PacketPortalFX;
import mekanism.common.registries.MekanismModules;
import mekanism.common.tags.MekanismTags;
import mekanism.common.util.MekanismUtils;
import mekanism.common.util.StorageUtils;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.FlowingFluidBlock;
import net.minecraft.block.TripWireBlock;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.ai.attributes.Attribute;
import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.entity.ai.attributes.AttributeModifier.Operation;
import net.minecraft.entity.ai.attributes.Attributes;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemStack.TooltipDisplayFlags;
import net.minecraft.item.ItemUseContext;
import net.minecraft.item.Rarity;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.ActionResult;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.DamageSource;
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.math.BlockRayTraceResult;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.ToolType;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.Constants.BlockFlags;
import net.minecraftforge.fluids.IFluidBlock;
public class ItemMekaTool extends ItemEnergized implements IModuleContainerItem, IModeItem {
private final Multimap<Attribute, AttributeModifier> attributes;
public ItemMekaTool(Properties properties) {
super(MekanismConfig.gear.mekaToolBaseChargeRate, MekanismConfig.gear.mekaToolBaseEnergyCapacity, properties.rarity(Rarity.EPIC).setNoRepair());
Builder<Attribute, AttributeModifier> builder = ImmutableMultimap.builder();
builder.put(Attributes.ATTACK_SPEED, new AttributeModifier(BASE_ATTACK_SPEED_UUID, "Weapon modifier", -2.4D, Operation.ADDITION));
this.attributes = builder.build();
}
@Override
public boolean isCorrectToolForDrops(@Nonnull BlockState state) {
//Allow harvesting everything, things that are unbreakable are caught elsewhere
return true;
}
@Override
@OnlyIn(Dist.CLIENT)
public void appendHoverText(@Nonnull ItemStack stack, World world, @Nonnull List<ITextComponent> tooltip, @Nonnull ITooltipFlag flag) {
if (MekKeyHandler.isKeyPressed(MekanismKeyHandler.detailsKey)) {
addModuleDetails(stack, tooltip);
} else {
StorageUtils.addStoredEnergy(stack, tooltip, true);
tooltip.add(MekanismLang.HOLD_FOR_MODULES.translateColored(EnumColor.GRAY, EnumColor.INDIGO, MekanismKeyHandler.detailsKey.getTranslatedKeyMessage()));
}
}
@Nonnull
@Override
public Set<ToolType> getToolTypes(@Nonnull ItemStack stack) {
//TODO: If addons end up wanting to add custom tool types expose a way to do so via the custom module's implementation
// but for now just have special handling for shearing here
if (isModuleEnabled(stack, MekanismModules.SHEARING_UNIT)) {
Set<ToolType> types = new HashSet<>(super.getToolTypes(stack));
types.add(ModuleShearingUnit.SHEARS_TOOL_TYPE);
return types;
}
return super.getToolTypes(stack);
}
@Override
public boolean isFoil(@Nonnull ItemStack stack) {
return !stack.isEmpty() && super.isFoil(stack) && IModuleContainerItem.hasOtherEnchants(stack);
}
@Nonnull
@Override
public ActionResultType useOn(ItemUseContext context) {
for (Module<?> module : getModules(context.getItemInHand())) {
if (module.isEnabled()) {
ActionResultType result = onModuleUse(module, context);
if (result != ActionResultType.PASS) {
return result;
}
}
}
return super.useOn(context);
}
private <MODULE extends ICustomModule<MODULE>> ActionResultType onModuleUse(IModule<MODULE> module, ItemUseContext context) {
return module.getCustomInstance().onItemUse(module, context);
}
@Nonnull
@Override
public ActionResultType interactLivingEntity(@Nonnull ItemStack stack, @Nonnull PlayerEntity player, @Nonnull LivingEntity entity, @Nonnull Hand hand) {
for (Module<?> module : getModules(stack)) {
if (module.isEnabled()) {
ActionResultType result = onModuleInteract(module, player, entity, hand);
if (result != ActionResultType.PASS) {
return result;
}
}
}
return super.interactLivingEntity(stack, player, entity, hand);
}
private <MODULE extends ICustomModule<MODULE>> ActionResultType onModuleInteract(IModule<MODULE> module, @Nonnull PlayerEntity player, @Nonnull LivingEntity entity,
@Nonnull Hand hand) {
return module.getCustomInstance().onInteract(module, player, entity, hand);
}
@Override
public float getDestroySpeed(@Nonnull ItemStack stack, @Nonnull BlockState state) {
IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(stack, 0);
if (energyContainer == null) {
return 0;
}
//Use raw hardness to get a best guess of if it is zero or not
FloatingLong energyRequired = getDestroyEnergy(stack, state.destroySpeed, isModuleEnabled(stack, MekanismModules.SILK_TOUCH_UNIT));
FloatingLong energyAvailable = energyContainer.extract(energyRequired, Action.SIMULATE, AutomationType.MANUAL);
if (energyAvailable.smallerThan(energyRequired)) {
//If we can't extract all the energy we need to break it go at base speed reduced by how much we actually have available
return MekanismConfig.gear.mekaToolBaseEfficiency.get() * energyAvailable.divide(energyRequired).floatValue();
}
IModule<ModuleExcavationEscalationUnit> module = getModule(stack, MekanismModules.EXCAVATION_ESCALATION_UNIT);
return module == null || !module.isEnabled() ? MekanismConfig.gear.mekaToolBaseEfficiency.get() : module.getCustomInstance().getEfficiency();
}
@Override
public boolean mineBlock(@Nonnull ItemStack stack, @Nonnull World world, @Nonnull BlockState state, @Nonnull BlockPos pos, @Nonnull LivingEntity entityliving) {
IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(stack, 0);
if (energyContainer != null) {
FloatingLong energyRequired = getDestroyEnergy(stack, state.getDestroySpeed(world, pos), isModuleEnabled(stack, MekanismModules.SILK_TOUCH_UNIT));
FloatingLong extractedEnergy = energyContainer.extract(energyRequired, Action.EXECUTE, AutomationType.MANUAL);
if (extractedEnergy.equals(energyRequired) || entityliving instanceof PlayerEntity && ((PlayerEntity) entityliving).isCreative()) {
//Only disarm tripwires if we had all the energy we tried to use (or are creative). Otherwise treat it as if we may have failed to disarm it
if (state.is(Blocks.TRIPWIRE) && !state.getValue(TripWireBlock.DISARMED) && isModuleEnabled(stack, MekanismModules.SHEARING_UNIT)) {
world.setBlock(pos, state.setValue(TripWireBlock.DISARMED, true), BlockFlags.NO_RERENDER);
}
}
}
return true;
}
@Override
public boolean hurtEnemy(@Nonnull ItemStack stack, @Nonnull LivingEntity target, @Nonnull LivingEntity attacker) {
IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(stack, 0);
FloatingLong energy = energyContainer == null ? FloatingLong.ZERO : energyContainer.getEnergy();
FloatingLong energyCost = FloatingLong.ZERO;
int minDamage = MekanismConfig.gear.mekaToolBaseDamage.get(), maxDamage = minDamage;
IModule<ModuleAttackAmplificationUnit> attackAmplificationUnit = getModule(stack, MekanismModules.ATTACK_AMPLIFICATION_UNIT);
if (attackAmplificationUnit != null && attackAmplificationUnit.isEnabled()) {
maxDamage = attackAmplificationUnit.getCustomInstance().getDamage();
if (maxDamage > minDamage) {
energyCost = MekanismConfig.gear.mekaToolEnergyUsageWeapon.get().multiply((maxDamage - minDamage) / 4F);
}
minDamage = Math.min(minDamage, maxDamage);
}
int damageDifference = maxDamage - minDamage;
//If we don't have enough power use it at a reduced power level
double percent = 1;
if (energy.smallerThan(energyCost)) {
percent = energy.divideToLevel(energyCost);
}
float damage = (float) (minDamage + damageDifference * percent);
if (attacker instanceof PlayerEntity) {
target.hurt(DamageSource.playerAttack((PlayerEntity) attacker), damage);
} else {
target.hurt(DamageSource.mobAttack(attacker), damage);
}
if (energyContainer != null && !energy.isZero()) {
energyContainer.extract(energyCost, Action.EXECUTE, AutomationType.MANUAL);
}
return false;
}
@Override
public boolean onBlockStartBreak(ItemStack stack, BlockPos pos, PlayerEntity player) {
if (player.level.isClientSide || player.isCreative()) {
return super.onBlockStartBreak(stack, pos, player);
}
IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(stack, 0);
if (energyContainer != null) {
World world = player.level;
BlockState state = world.getBlockState(pos);
boolean silk = isModuleEnabled(stack, MekanismModules.SILK_TOUCH_UNIT);
FloatingLong energyRequired = getDestroyEnergy(stack, state.getDestroySpeed(world, pos), silk);
if (energyContainer.extract(energyRequired, Action.SIMULATE, AutomationType.MANUAL).greaterOrEqual(energyRequired)) {
IModule<ModuleVeinMiningUnit> veinMiningUnit = getModule(stack, MekanismModules.VEIN_MINING_UNIT);
//Even though we now handle breaking bounding blocks properly, don't allow vein mining them
if (veinMiningUnit != null && veinMiningUnit.isEnabled() && !(state.getBlock() instanceof BlockBounding)) {
boolean isOre = state.is(MekanismTags.Blocks.ATOMIC_DISASSEMBLER_ORE);
//If it is extended or should be treated as an ore
if (isOre || veinMiningUnit.getCustomInstance().isExtended()) {
//Don't include bonus energy required by efficiency modules when calculating energy of vein mining targets
FloatingLong baseDestroyEnergy = getDestroyEnergy(silk);
Set<BlockPos> found = ModuleVeinMiningUnit.findPositions(state, pos, world, isOre ? -1 : veinMiningUnit.getCustomInstance().getExcavationRange());
MekanismUtils.veinMineArea(energyContainer, world, pos, (ServerPlayerEntity) player, stack, this, found,
isModuleEnabled(stack, MekanismModules.SHEARING_UNIT), hardness -> getDestroyEnergy(baseDestroyEnergy, hardness),
distance -> 0.5 * Math.pow(distance, isOre ? 1.5 : 2), state);
}
}
}
}
return super.onBlockStartBreak(stack, pos, player);
}
private FloatingLong getDestroyEnergy(boolean silk) {
return silk ? MekanismConfig.gear.mekaToolEnergyUsageSilk.get() : MekanismConfig.gear.mekaToolEnergyUsage.get();
}
private FloatingLong getDestroyEnergy(ItemStack itemStack, float hardness, boolean silk) {
return getDestroyEnergy(getDestroyEnergy(itemStack, silk), hardness);
}
private FloatingLong getDestroyEnergy(FloatingLong baseDestroyEnergy, float hardness) {
return hardness == 0 ? baseDestroyEnergy.divide(2) : baseDestroyEnergy;
}
private FloatingLong getDestroyEnergy(ItemStack itemStack, boolean silk) {
FloatingLong destroyEnergy = getDestroyEnergy(silk);
IModule<ModuleExcavationEscalationUnit> module = getModule(itemStack, MekanismModules.EXCAVATION_ESCALATION_UNIT);
float efficiency = module == null || !module.isEnabled() ? MekanismConfig.gear.mekaToolBaseEfficiency.get() : module.getCustomInstance().getEfficiency();
return destroyEnergy.multiply(efficiency);
}
@Nonnull
@Override
public Multimap<Attribute, AttributeModifier> getAttributeModifiers(@Nonnull EquipmentSlotType slot, @Nonnull ItemStack stack) {
return slot == EquipmentSlotType.MAINHAND ? attributes : super.getAttributeModifiers(slot, stack);
}
@Nonnull
@Override
public ActionResult<ItemStack> use(World world, PlayerEntity player, @Nonnull Hand hand) {
ItemStack stack = player.getItemInHand(hand);
if (!world.isClientSide()) {
IModule<ModuleTeleportationUnit> module = getModule(stack, MekanismModules.TELEPORTATION_UNIT);
if (module != null && module.isEnabled()) {
BlockRayTraceResult result = MekanismUtils.rayTrace(player, MekanismConfig.gear.mekaToolMaxTeleportReach.get());
//If we don't require a block target or are not a miss, allow teleporting
if (!module.getCustomInstance().requiresBlockTarget() || result.getType() != RayTraceResult.Type.MISS) {
BlockPos pos = result.getBlockPos();
// make sure we fit
if (isValidDestinationBlock(world, pos.above()) && isValidDestinationBlock(world, pos.above(2))) {
double distance = player.distanceToSqr(pos.getX(), pos.getY(), pos.getZ());
if (distance < 5) {
return new ActionResult<>(ActionResultType.PASS, stack);
}
IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(stack, 0);
FloatingLong energyNeeded = MekanismConfig.gear.mekaToolEnergyUsageTeleport.get().multiply(distance / 10D);
if (energyContainer == null || energyContainer.getEnergy().smallerThan(energyNeeded)) {
return new ActionResult<>(ActionResultType.FAIL, stack);
}
energyContainer.extract(energyNeeded, Action.EXECUTE, AutomationType.MANUAL);
if (player.isPassenger()) {
player.stopRiding();
}
player.teleportTo(pos.getX() + 0.5, pos.getY() + 1.5, pos.getZ() + 0.5);
player.fallDistance = 0.0F;
Mekanism.packetHandler.sendToAllTracking(new PacketPortalFX(pos.above()), world, pos);
world.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.ENDERMAN_TELEPORT, SoundCategory.PLAYERS, 1.0F, 1.0F);
return new ActionResult<>(ActionResultType.SUCCESS, stack);
}
}
}
}
return new ActionResult<>(ActionResultType.PASS, stack);
}
private boolean isValidDestinationBlock(World world, BlockPos pos) {
BlockState blockState = world.getBlockState(pos);
//Allow teleporting into air or fluids
return blockState.isAir(world, pos) || blockState.getBlock() instanceof FlowingFluidBlock || blockState.getBlock() instanceof IFluidBlock;
}
@Override
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundNBT nbt) {
stack.hideTooltipPart(TooltipDisplayFlags.MODIFIERS);
return super.initCapabilities(stack, nbt);
}
@Override
public boolean isEnchantable(@Nonnull ItemStack stack) {
return false;
}
@Override
public boolean isBookEnchantable(ItemStack stack, ItemStack book) {
return false;
}
@Override
public boolean canApplyAtEnchantingTable(ItemStack stack, Enchantment enchantment) {
return false;
}
@Override
public boolean supportsSlotType(ItemStack stack, @Nonnull EquipmentSlotType slotType) {
return IModeItem.super.supportsSlotType(stack, slotType) && getModules(stack).stream().anyMatch(Module::handlesModeChange);
}
@Override
public void changeMode(@Nonnull PlayerEntity player, @Nonnull ItemStack stack, int shift, boolean displayChangeMessage) {
for (Module<?> module : getModules(stack)) {
if (module.handlesModeChange()) {
module.changeMode(player, stack, shift, displayChangeMessage);
return;
}
}
}
@Override
protected FloatingLong getMaxEnergy(ItemStack stack) {
IModule<ModuleEnergyUnit> module = getModule(stack, MekanismModules.ENERGY_UNIT);
return module == null ? MekanismConfig.gear.mekaToolBaseEnergyCapacity.get() : module.getCustomInstance().getEnergyCapacity(module);
}
@Override
protected FloatingLong getChargeRate(ItemStack stack) {
IModule<ModuleEnergyUnit> module = getModule(stack, MekanismModules.ENERGY_UNIT);
return module == null ? MekanismConfig.gear.mekaToolBaseChargeRate.get() : module.getCustomInstance().getChargeRate(module);
}
}