Last active
August 23, 2018 21:51
-
-
Save Caellian/0c18c9b0a723cbd809f51b098bdc636f to your computer and use it in GitHub Desktop.
Minecraft electricity example using FlowAPI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * MIT License | |
| * Copyright 2018 Tin Svagelj (a.k.a. Caellian) <tin.svagelj.email@gmail.com> | |
| * | |
| * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | |
| * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the | |
| * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | |
| * permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
| * | |
| * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the | |
| * Software. | |
| * | |
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | |
| * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
| * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
| * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| */ | |
| package hr.caellian.flow.mcmod; | |
| import hr.caellian.flow.data.Flux; | |
| import hr.caellian.flow.data.FluxType; | |
| import hr.caellian.flow.data.Property; | |
| import hr.caellian.flow.util.PropertyMap; | |
| public class Electricity implements FluxType<Electricity.ElectricityFlux> { | |
| public static final Electricity INSTANCE = new Electricity(); | |
| public static final String ID = "mod:electricity"; | |
| public static final String PROPERTY_POTENTIAL = "mod:electricity:potential"; | |
| private Electricity() {} | |
| @Override | |
| public String getID() { | |
| return ID; | |
| } | |
| @Override | |
| public ElectricityFlux createUnit(Property... properties) { | |
| int potential = 0; | |
| for (Property property : properties) { | |
| if (property.getID().equals(PROPERTY_POTENTIAL)) { | |
| potential = (int) (property).get(); | |
| break; | |
| } | |
| } | |
| return new ElectricityFlux(potential); | |
| } | |
| public class ElectricityFlux implements Flux<Electricity> { | |
| private PropertyMap properties = new PropertyMap(); | |
| ElectricityFlux(int potential) { | |
| addProperty(new Property<>(PROPERTY_POTENTIAL, potential)); | |
| } | |
| @Override | |
| public Electricity getType() { | |
| return Electricity.INSTANCE; | |
| } | |
| @Override | |
| public Flux<Electricity> add(Flux<FluxType> other) { | |
| if (other.getID().equals(this.getID())) { | |
| Property<Integer> otherPotential = other.getProperties().getProperty(PROPERTY_POTENTIAL); | |
| properties.<Integer>getProperty(PROPERTY_POTENTIAL).apply( | |
| potential -> potential + otherPotential.get()); | |
| otherPotential.set(0); | |
| return this; | |
| } | |
| return null; | |
| } | |
| @Override | |
| public Flux<Electricity> take(Property[] extract) { | |
| int amount = 0; | |
| for (Property property : extract) { | |
| if (property.getID().equals(PROPERTY_POTENTIAL)) { | |
| amount = (int) (property).get(); | |
| break; | |
| } | |
| } | |
| if (amount >= properties.<Integer>getPropertyValue(PROPERTY_POTENTIAL)) { | |
| return this; | |
| } else { | |
| properties.getProperty(PROPERTY_POTENTIAL) | |
| .set(properties.<Integer>getPropertyValue(PROPERTY_POTENTIAL) - amount); | |
| return new ElectricityFlux(amount); | |
| } | |
| } | |
| @Override | |
| public PropertyMap getModifiableProperties() { | |
| return properties; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment