Base work for ticket based syphoning
To be used for providing a history. This commit breaks any usage of the network events. Heads up @TeamDMan @Arcaratus
This commit is contained in:
parent
c8e42e3288
commit
47b88b95b0
5 changed files with 203 additions and 104 deletions
|
@ -1,24 +1,27 @@
|
|||
package WayofTime.bloodmagic.core.data;
|
||||
|
||||
import WayofTime.bloodmagic.util.BMLog;
|
||||
import WayofTime.bloodmagic.util.DamageSourceBloodMagic;
|
||||
import WayofTime.bloodmagic.event.AddToNetworkEvent;
|
||||
import WayofTime.bloodmagic.event.SoulNetworkEvent;
|
||||
import WayofTime.bloodmagic.util.BMLog;
|
||||
import WayofTime.bloodmagic.util.BooleanResult;
|
||||
import WayofTime.bloodmagic.util.DamageSourceBloodMagic;
|
||||
import WayofTime.bloodmagic.util.helper.PlayerHelper;
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.EvictingQueue;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.init.MobEffects;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.potion.PotionEffect;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.common.util.INBTSerializable;
|
||||
import net.minecraftforge.fml.common.FMLCommonHandler;
|
||||
import net.minecraftforge.fml.common.eventhandler.Event;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.UUID;
|
||||
|
||||
public class SoulNetwork implements INBTSerializable<NBTTagCompound> {
|
||||
|
||||
private final Queue<SoulTicket> ticketHistory;
|
||||
private BMWorldSavedData parent;
|
||||
private EntityPlayer cachedPlayer;
|
||||
private UUID playerId;
|
||||
|
@ -27,72 +30,93 @@ public class SoulNetwork implements INBTSerializable<NBTTagCompound> {
|
|||
|
||||
private SoulNetwork() {
|
||||
// No-op - For creation via NBT only
|
||||
ticketHistory = EvictingQueue.create(16);
|
||||
}
|
||||
|
||||
public int add(int toAdd, int maximum) {
|
||||
AddToNetworkEvent event = new AddToNetworkEvent(playerId.toString(), toAdd, maximum);
|
||||
|
||||
public int add(SoulTicket ticket, int maximum) {
|
||||
SoulNetworkEvent.Fill event = new SoulNetworkEvent.Fill(this, ticket, maximum);
|
||||
if (MinecraftForge.EVENT_BUS.post(event))
|
||||
return 0;
|
||||
|
||||
if (FMLCommonHandler.instance().getMinecraftServerInstance() == null)
|
||||
return 0;
|
||||
|
||||
int currEss = getCurrentEssence();
|
||||
|
||||
if (currEss >= event.maximum)
|
||||
if (currEss >= event.getMaximum())
|
||||
return 0;
|
||||
|
||||
int newEss = Math.min(event.maximum, currEss + event.addedAmount);
|
||||
if (event.getResult() != Event.Result.DENY)
|
||||
setCurrentEssence(newEss);
|
||||
int newEss = Math.min(event.getMaximum(), currEss + event.getTicket().getAmount());
|
||||
setCurrentEssence(newEss);
|
||||
|
||||
ticketHistory.add(ticket);
|
||||
return newEss - currEss;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated - Please use {@link #add(int, int)}
|
||||
* @deprecated For future proofing, use {@link #add(SoulTicket, int)} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public int add(int toAdd, int maximum) {
|
||||
return add(new SoulTicket(toAdd), maximum);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link #add(SoulTicket, int)} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public int addLifeEssence(int toAdd, int maximum) {
|
||||
return add(toAdd, maximum);
|
||||
}
|
||||
|
||||
public int syphon(int syphon) {
|
||||
public int syphon(SoulTicket ticket) {
|
||||
return syphon(ticket, false);
|
||||
}
|
||||
|
||||
public int syphon(SoulTicket ticket, boolean skipEvent) {
|
||||
SoulNetworkEvent.Syphon event = new SoulNetworkEvent.Syphon(this, ticket);
|
||||
if (!skipEvent && MinecraftForge.EVENT_BUS.post(event))
|
||||
return 0;
|
||||
|
||||
int syphon = event.getTicket().getAmount();
|
||||
if (getCurrentEssence() >= syphon) {
|
||||
setCurrentEssence(getCurrentEssence() - syphon);
|
||||
ticketHistory.add(ticket);
|
||||
return syphon;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public boolean syphonAndDamage(EntityPlayer user, int toSyphon) {
|
||||
if (user != null) {
|
||||
if (user.getEntityWorld().isRemote)
|
||||
return false;
|
||||
/**
|
||||
* @deprecated For future proofing, use {@link #syphon(SoulTicket)} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public int syphon(int amount) {
|
||||
return syphon(new SoulTicket(amount));
|
||||
}
|
||||
|
||||
if (!Strings.isNullOrEmpty(playerId.toString())) {
|
||||
SoulNetworkEvent.ItemDrainNetworkEvent event = new SoulNetworkEvent.ItemDrainNetworkEvent(user, playerId, null, toSyphon);
|
||||
public BooleanResult<Integer> syphonAndDamage(EntityPlayer user, SoulTicket ticket) {
|
||||
if (user.getEntityWorld().isRemote)
|
||||
return BooleanResult.newResult(false, 0);
|
||||
|
||||
if (MinecraftForge.EVENT_BUS.post(event))
|
||||
return false;
|
||||
SoulNetworkEvent.Syphon.User event = new SoulNetworkEvent.Syphon.User(this, ticket, user);
|
||||
|
||||
int drainAmount = syphon(event.syphon);
|
||||
if (MinecraftForge.EVENT_BUS.post(event))
|
||||
return BooleanResult.newResult(false, 0);
|
||||
|
||||
if (drainAmount <= 0 || event.shouldDamage)
|
||||
hurtPlayer(user, event.syphon);
|
||||
int drainAmount = syphon(event.getTicket(), true);
|
||||
|
||||
return event.getResult() != Event.Result.DENY;
|
||||
}
|
||||
if (drainAmount <= 0 || event.shouldDamage())
|
||||
hurtPlayer(user, event.getTicket().getAmount());
|
||||
|
||||
int amount = syphon(toSyphon);
|
||||
hurtPlayer(user, toSyphon - amount);
|
||||
ticketHistory.add(ticket);
|
||||
return BooleanResult.newResult(true, event.getTicket().getAmount());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
/**
|
||||
* @deprecated Use {@link #syphonAndDamage(EntityPlayer, SoulTicket)} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public boolean syphonAndDamage(EntityPlayer user, int amount) {
|
||||
return syphonAndDamage(user, new SoulTicket(amount)).isSuccess();
|
||||
}
|
||||
|
||||
public void causeNausea() {
|
||||
|
@ -180,6 +204,10 @@ public class SoulNetwork implements INBTSerializable<NBTTagCompound> {
|
|||
return this;
|
||||
}
|
||||
|
||||
public List<SoulTicket> getTicketHistory() {
|
||||
return ImmutableList.copyOf(ticketHistory);
|
||||
}
|
||||
|
||||
// INBTSerializable
|
||||
|
||||
@Override
|
||||
|
|
33
src/main/java/WayofTime/bloodmagic/core/data/SoulTicket.java
Normal file
33
src/main/java/WayofTime/bloodmagic/core/data/SoulTicket.java
Normal file
|
@ -0,0 +1,33 @@
|
|||
package WayofTime.bloodmagic.core.data;
|
||||
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.util.text.TextComponentString;
|
||||
|
||||
public class SoulTicket {
|
||||
|
||||
private static final ITextComponent EMPTY = new TextComponentString("");
|
||||
|
||||
private final ITextComponent description;
|
||||
private final int amount;
|
||||
|
||||
public SoulTicket(ITextComponent description, int amount) {
|
||||
this.description = description;
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public SoulTicket(int amount) {
|
||||
this(EMPTY, amount);
|
||||
}
|
||||
|
||||
public boolean isSyphon() {
|
||||
return amount < 0;
|
||||
}
|
||||
|
||||
public ITextComponent getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public int getAmount() {
|
||||
return amount;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue