use of com.fredtargaryen.fragileglass.entity.capability.IBreakCapability in project Fragile-Glass by fredtargaryen.
the class EntityDataManager method addCapabilityIfPossible.
public void addCapabilityIfPossible(Entity e, AttachCapabilitiesEvent<Entity> evt) {
// A player would reasonably expect many existing entities to be able to break fragile blocks, but it is
// very unlikely that anyone would go to the trouble of writing out all the config lines for every entity.
// The following code does some type checks and creates BreakerDatas if the entity probably would break a block.
BreakerData breakerData = this.data.get(e.getType());
if (breakerData == null) {
// A breakerdata for this entitytype has not been created yet
if (e instanceof LivingEntity || e instanceof ArrowEntity || e instanceof FireballEntity || e instanceof MinecartEntity || e instanceof FireworkRocketEntity || e instanceof BoatEntity || e instanceof TNTEntity || e instanceof FallingBlockEntity) {
breakerData = new BreakerData(DataReference.MINIMUM_ENTITY_SPEED_SQUARED, DataReference.MAXIMUM_ENTITY_SPEED_SQUARED, new String[] {});
this.data.put(e.getType(), breakerData);
}
}
if (breakerData != null) {
// The entity was predefined (via configs or commands) as being able to break a block,
// or a BreakerData was automatically created above.
ICapabilityProvider iCapProv = new ICapabilityProvider() {
IBreakCapability inst = new IBreakCapability() {
@Override
public void init(Entity e) {
}
@Override
public void update(Entity e) {
}
@Override
public double getSpeedSquared(Entity e) {
Vector3d motion = e.getMotion();
return motion.x * motion.x + motion.y * motion.y + motion.z * motion.z;
}
@Override
public boolean isAbleToBreak(Entity e, double speedSq) {
BreakerData breakerData = EntityDataManager.this.data.get(e.getType());
if (breakerData == null)
return false;
return speedSq >= breakerData.getMinSpeedSquared() && speedSq <= breakerData.getMaxSpeedSquared();
}
@Override
public double getMotionX(Entity e) {
return e.getMotion().x;
}
@Override
public double getMotionY(Entity e) {
return e.getMotion().y;
}
@Override
public double getMotionZ(Entity e) {
return e.getMotion().z;
}
@Override
public byte getNoOfBreaks(Entity e) {
return 1;
}
};
@Nullable
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction facing) {
return capability == FragileGlassBase.BREAKCAP ? LazyOptional.of(() -> (T) inst) : LazyOptional.empty();
}
};
evt.addCapability(DataReference.BREAK_LOCATION, iCapProv);
}
}
Aggregations